|
RSS Feed - WebSphere MQ Support
|
RSS Feed - Message Broker Support
|
 |
|
How to implement Pub/Sub messaging domain using JMS in MQSer |
« View previous topic :: View next topic » |
Author |
Message
|
eswarnv |
Posted: Thu Jan 31, 2002 10:25 am Post subject: |
|
|
Voyager
Joined: 20 Dec 2001 Posts: 88
|
Hi All,
Can any one suggest me good documentation or tutorial which explains the entire procedure of implementing publish/subscribe messaging style using JMS on MQSeries.
Thanks in Advance
Eswar |
|
Back to top |
|
 |
kolban |
Posted: Thu Jan 31, 2002 1:22 pm Post subject: |
|
|
 Grand Master
Joined: 22 May 2001 Posts: 1072 Location: Fort Worth, TX, USA
|
|
Back to top |
|
 |
abmanesh |
Posted: Fri Feb 01, 2002 8:05 am Post subject: |
|
|
Apprentice
Joined: 06 Nov 2001 Posts: 39
|
The steps involved implementing Publish/Subscribe messaging with MQ/JMS is illustrated with simple sample programs.
SECTION 1.
Define the JMS Administered objects
Create a JNDI context.
The following command was used to create the JNDI context named ‘ psCtx‘ used in our sample programs.
DEF CTX(psCtx)
Change to the context you just created
CHG CTX(psCtx)
Create a TopicConnectionFactory .
The following command creates a TopicConnectionFactory named ‘psTcf ’
referring to the QueueManager ‘ITSOG.QMGR1 ’ on host named ‘ITSOG ’
listening on the default port 1414 .The server connection channel used
for client connection is ‘JMS.SRV.CHNL ’
DEF TCF(psTcf) TRANSPORT(CLIENT) QMANAGER(ITSOG.QMGR1) HOST(ITSOG)
PORT(1414) CHANNEL(JMS.SRV.CHNL) BROKERQMGR(ITSOG.QMGR1)
BROKERCONQ(SYSTEM.BROKER.CONTROL.QUEUE)
BROKERPUBQ(SYSTEM.BROKER.DEFAULT.STREAM)
BROKERSUBQ(SYSTEM.JMS.ND.SUBSCRIBER.QUEUE)
BROKERCCSUBQ(SYSTEM.JMS.ND.CC.SUBSCRIBER.QUEUE)
Define the JNDI topic
The following command creates a Topic named ‘psTopic ’ for the topic
named SampleTopic under the root topic of JmsTest.
DEF T(psTopic)TOPIC(JMSTest/SampleTopic)
SECTION 2.
The program JMSPublisher.java will create
a text message and publish it to a topic 'SampleTopic' which is under the root
topic of JMSTest. We will also illustrate the subscriber program that will
subscribe to the topic 'SampleTopic'. The High level Steps involved in the Publisher are
A. Lookup the JNDI namespace for the TopicConnectionFactory and the To p i c
B. Create a Topic Connection
C. Create a TopicSession
D. Create a TopicPublisher
E. Create a TextMessage
F. Publish the message to the Topic
G. Close and disconnect the connection objects.
JMSPublisher.java
//Step 1 Import the necessary packages
import java.util.*;
import javax.jms.*;
import javax.naming.directory.*;
import javax.naming.*;
public class JMSPublisher {
/**
*The main method
*@param no args
*/
public static void main(String [] args)){
String topicName ="cn=psTopic";
String tcfName ="cn=psTcf";
Context jndiContext =null;
TopicConnectionFactory topicConnectionFactory =null;
TopicConnection topicConnection =null;
TopicSession topicSession =null;
Topic topic =null;
TopicPublisher publisher =null;
TextMessage message =null;
String providerUrl ="ldap://itsog/cn=psCtx,o=itsog,c=uk";
String initialContextFactory ="com.sun.jndi.ldap.LdapCtxFactory";
//Step 2 Set up an Initial context for JNDI lookUp.
try {
Hashtable env =new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY,initialContextFactory);
env.put(Context.PROVIDER_URL ,providerUrl );
jndiContext =new InitialDirContext(env);
//Step 3 Obtain a TopicConnection factory
topicConnectionFactory =
(TopicConnectionFactory)jndiContext.lookup(tcfName);
//Step 4 Create a Topic Connection using the connection factory object
topicConnection =topicConnectionFactory.createTopicConnection();
//Step 5 Start the topic connection.
topicConnection.start();
//Step 6 Obtain a Topic from the JNDI
topic =(Topic)jndiContext.lookup(topicName);
//Step 7 Create a Topic Session from the topic connection
topicSession =topicConnection.createTopicSession(false,
Session.AUTO_ACKNOWLEDGE);
//Step 8 Create a topic publisher for the topic from the session.
publisher =topicSession.createPublisher(topic);
//Step 9 Create a message object
message =topicSession.createTextMessage();
//Step 10 prepare the body of the message
message.setText("This is a Test Message from JMSPublisher Class ")
;
//Step 11 Publish the message.
publisher.publish(message);
//Step 12 Close the connections.
publisher.close();
topicSession.close();
topicConnection.close();
}
catch(Exception e ){
e.printStackTrace();
}
}
}
Simple JMS Subscriber Application.
In this section we will illustrate the steps involved in developing a JMS
Subscriber application with a simple JMS subscriber application.
The high level steps involved in writing a JMS Subscriber application are:
A. Lookup the JNDI namespace for the TopicConnectionFactory and the To p i c
B. Create a Topic Connection
C. Create a TopicSession
D. Create a TopicSubscriber
E. Receive subscription from the Topic
F. Close and disconnect the connection objects.
The program JMSSubscriber.java is a simple subscriber application that will
subscribe to messages from the topic 'SampleTopic' which is under the root
topic of JMSTest. We will be using non durable subscription in the sample.
JMSSubscriber.java
//Step 1 Import the necessary packages.
import java.util.*;
import javax.jms.*;
import javax.naming.directory.*;
import javax.naming.*;
public class JMSSubscriber {
/**
*The main method
*@param no args
*/
public static void main(String [] args)){
String topicName ="cn=psTopic";
String tcfName ="cn=psTcf";
Context jndiContext =null;
TopicConnectionFactory topicConnectionFactory =null;
TopicConnection topicConnection =null;
TopicSession topicSession =null;
Topic topic =null;
TopicSubscriber subscriber =null;
TextMessage message =null;
String providerUrl ="ldap://itsog/cn=psCtx,o=itsog,c=uk";
String initialContextFactory ="com.sun.jndi.ldap.LdapCtxFactory";
//Step 2 Set up Initial Context for JNDI lookup
try{
Hashtable env =new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY,initialContextFactory);
env.put(Context.PROVIDER_URL ,providerUrl );
env.put(Context.REFERRAL,"throw");
jndiContext =new InitialDirContext(env);
//Step 3 Get the TopicConnection factory from the JNDI Namespace
topicConnectionFactory =
(TopicConnectionFactory)jndiContext.lookup(tcfName);
//Step 4 Create a TopicConnection
topicConnection =topicConnectionFactory.createTopicConnection();
//Step 5 Start The topic connection
topicConnection.start();
//Step 6 Create a topic session from the topic connection
topicSession =topicConnection.createTopicSession(false,
Session.AUTO_ACKNOWLEDGE);
//Step 7 Obtain a Topic from the JNDI namespace
topic =(Topic)jndiContext.lookup(topicName);
//Step 8 Create a topic subscriber for the topic.
subscriber =topicSession.createSubscriber(topic);//Non durable
subscriber
//Step 9 Receive Subscription
message =(TextMessage)subscriber.receive();
System.out.println("n ***The Message is "+message.getText());
//Step 10Close the connection and other open resources
subscriber.close();
topicSession.close();
topicConnection.close();
}
catch(Exception e ){
e.printStackTrace();
}
}
}
[ This Message was edited by: abmanesh on 2002-02-01 08:07 ]
[ This Message was edited by: abmanesh on 2002-02-01 08:08 ] |
|
Back to top |
|
 |
|
|
 |
|
Page 1 of 1 |
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|
|
|