To Publish the Code :
ConnectionFactory (from InitialContext-JNDI lokup- topicCF)-> Create Connection (publishConnection)-> Create Session(not thread safe- publishSession) -> Create Publisher (publisher)
on destination topic - > create JMS Message from session to send (publishSession.createTextMessage)- > Send the Message from publisher object (publisher.publish)
See code below :
String factoryJndiName = "WSMQTopicConnectionFactory";
String destinationJndiName = "wsmq/topic/ProductManagerTopic";
//JNDI lookup of administered ConnectionFactory object
Context iniCtx = new InitialContext();
TopicConnectionFactory topicCF = (TopicConnectionFactory) iniCtx.lookup(factoryJndiName);
//JNDI lookup of administered destination (i.e. Topic)
Topic topicDestination = (Topic) iniCtx.lookup(destinationJndiName);
//get a connection from the TopicConnectionFactory
TopicConnection publishConnection = topicCF.createTopicConnection();
//get a session from the connection. Session should be accessed by only one thread.
TopicSession publishSession =
publishConnection.createTopicSession(false,TopicSession.AUTO_ACKNOWLEDGE);
//create a publisher from the session
TopicPublisher publisher = publishSession.createPublisher(topicDestination);
//create a JMS message to send
TextMessage message = publishSession.createTextMessage();
message.setText("JMS test message");
//send the message
publisher.publish(message, DeliveryMode.NON_PERSISTENT, 4, 0);
To Subscribe :
ConnectionFactory (from InitialContext-JNDI lokup- topicCF)-> Create Connection (subscribeConnection)-> Create Session(not thread safe- subscribeSession) -> Create Subsciber (subscriber)
on destination topic - > create JMS Message from session to send (publishSession.createTextMessage)- > Send the Message from publisher object (subscriber.receive)
String factoryJndiName = "WSMQTopicConnectionFactory";
String destinationJndiName = "wsmq/topic/ProductManagerTopic";
//JNDI lookup of administered ConnectionFactory object
Context iniCtx = new InitialContext();
TopicConnectionFactory topicCF = (TopicConnectionFactory) iniCtx.lookup(factoryJndiName);
//JNDI lookup of administered destination (i.e. Topic)
Topic topicDestination = (Topic) iniCtx.lookup(destinationJndiName);
//get a connection from the TopicConnectionFactory
TopicConnection subscribeConnection = topicCF.createTopicConnection();
//get a session from the connection
TopicSession subscribeSession =
subscribeConnection.createTopicSession(false,TopicSession.AUTO_ACKNOWLEDGE);
//create a subscriber from the session
TopicSubscriber subscriber = subscribeSession.createsubscriber(topicDestination);
//look for messages every 1 second
while (true) {
Message response = subscriber.receive();
if (response != null && response instanceof TextMessage) {
System.out.println (((TextMessage) response).getText());
}
Thread.sleep(1000);
}
Cheers . ..
Kapil
ConnectionFactory (from InitialContext-JNDI lokup- topicCF)-> Create Connection (publishConnection)-> Create Session(not thread safe- publishSession) -> Create Publisher (publisher)
on destination topic - > create JMS Message from session to send (publishSession.createTextMessage)- > Send the Message from publisher object (publisher.publish)
See code below :
String factoryJndiName = "WSMQTopicConnectionFactory";
String destinationJndiName = "wsmq/topic/ProductManagerTopic";
//JNDI lookup of administered ConnectionFactory object
Context iniCtx = new InitialContext();
TopicConnectionFactory topicCF = (TopicConnectionFactory) iniCtx.lookup(factoryJndiName);
//JNDI lookup of administered destination (i.e. Topic)
Topic topicDestination = (Topic) iniCtx.lookup(destinationJndiName);
//get a connection from the TopicConnectionFactory
TopicConnection publishConnection = topicCF.createTopicConnection();
//get a session from the connection. Session should be accessed by only one thread.
TopicSession publishSession =
publishConnection.createTopicSession(false,TopicSession.AUTO_ACKNOWLEDGE);
//create a publisher from the session
TopicPublisher publisher = publishSession.createPublisher(topicDestination);
//create a JMS message to send
TextMessage message = publishSession.createTextMessage();
message.setText("JMS test message");
//send the message
publisher.publish(message, DeliveryMode.NON_PERSISTENT, 4, 0);
To Subscribe :
ConnectionFactory (from InitialContext-JNDI lokup- topicCF)-> Create Connection (subscribeConnection)-> Create Session(not thread safe- subscribeSession) -> Create Subsciber (subscriber)
on destination topic - > create JMS Message from session to send (publishSession.createTextMessage)- > Send the Message from publisher object (subscriber.receive)
String factoryJndiName = "WSMQTopicConnectionFactory";
String destinationJndiName = "wsmq/topic/ProductManagerTopic";
//JNDI lookup of administered ConnectionFactory object
Context iniCtx = new InitialContext();
TopicConnectionFactory topicCF = (TopicConnectionFactory) iniCtx.lookup(factoryJndiName);
//JNDI lookup of administered destination (i.e. Topic)
Topic topicDestination = (Topic) iniCtx.lookup(destinationJndiName);
//get a connection from the TopicConnectionFactory
TopicConnection subscribeConnection = topicCF.createTopicConnection();
//get a session from the connection
TopicSession subscribeSession =
subscribeConnection.createTopicSession(false,TopicSession.AUTO_ACKNOWLEDGE);
//create a subscriber from the session
TopicSubscriber subscriber = subscribeSession.createsubscriber(topicDestination);
//look for messages every 1 second
while (true) {
Message response = subscriber.receive();
if (response != null && response instanceof TextMessage) {
System.out.println (((TextMessage) response).getText());
}
Thread.sleep(1000);
}
Cheers . ..
Kapil
No comments:
Post a Comment