The Java Message Service p.42
written by Dave Chappell
	


Listing 1

Properties env = new Properties();
// ... specify the JNDI properties specific to the JNDI SPI being used
...
jndi = new InitialContext(env);
// obtain a connection factory
factory =
    (TopicConnectionFactory)jndi.lookup("TopicConnectionFactory");
// create a connection
connect = factory.createTopicConnection (username, password);


// create a session for publishing, and one for subscriptions
pubSession =



connect.createTopicSession(false,Session.AUTO_ACKNOWLEDGE);
subSession =
    connect.createTopicSession(false,Session.AUTO_ACKNOWLEDGE);


myTopic = (Topic)jndi.lookup("My First Topic");


// create the publisher and the subscriber
publisher = pubSession.createPublisher(myTopic);
subscriber = subSession.createSubscriber(myTopic);


// associate the onMessage() handler with this subscriber
subscriber.setMessageListener(this);


// start the flow of incoming messages
connect.start();


TextMessage textMsg = pubSession.createTextMessage();
textMsg.setText("My first JMS message!);


publisher.publish(
    textMsg,
    javax.jms.DeliveryMode.PERSISTENT,          // delivery mode
    javax.jms.Message.DEFAULT_PRIORITY,    // message priority
    1800000); // Time-to-live (30 minutes)