Message format:
Establishes a message queue between two clients through peer-to-peer transmission through queues.
Note: similar to the queue in the data structure, the first-in-first-out
Topic message. Create a topic in the message middleware. No client can send messages to this topic to accept messages.
Note: broadcast-like methods
Development Process:
Procedure for using JMS
1. Create a JNDI Context
Context init initCtx = new InitialContext (env );
2. Find the Factory class used to create a JMS connection (Connnect Factory)
For topic messages:
Object tmp = initCtx. lookup ("Connnect Factory ");
TopicConnectionFactory tcf = (TopicConnectionFactory) tmp;
For point-to-point messages:
Object tmp = initCtx. lookup ("Connnect Factory ");
QueueConnectionFactory tcf = (QueueConnectionFactory) tmp;
3. Find the Destination object (Destination) of JMS)
For topic messages:
Topic topic = (Topic) iniCtx. lookup ("topic/testTopic ");
For point-to-point messages:
Queue queue (Queue) iniCtx. lookup ("queue/");
4. Create a JMS Connection)
For topic messages:
TopicConnection conn = tcp. createTopicConnection ();
For point-to-point messages:
QueueConnection conn = tcp. createQueueConnection ();
5. Create a JMS session)
For topic messages:
TopicSession session = conn. createTopicSession (fasle, Session. AUTO_ACKNOWLEDGE );
For point-to-point messages:
QueueSession session = conn. createQueueSession (true, 0 );
6. Create a message producer and consumer
Producer:
For topic messages:
TopicPublisher publisher = session. createPublisher (topic );
For point-to-point messages:
QueueSender sender = session. createSender (queue );
Consumer:
For topic messages:
TopicSubscriber subscriber = session. createSubscriber (topic );
For point-to-point messages:
QueueReceiver receiver = session. createReceiver (queue );
7. register the message listener
For topic messages:
TextListener listener = new TextListener ();
Subscriber. setMessageListener (listener );
For point-to-point messages:
TextListener listener = new TextListener ();
Receiver. setMessageListener (listener );
8. Start the JMS connection
Conn. start ();
9. send and receive messages
Send message:
For topic messages:
Publisher. publish (message );
For point-to-point messages:
Sender. send (message );
Receive message:
For topic messages:
Message msg = subscriber. receive ();
For point-to-point messages:
Message msg = receiver. receive (1000 );
10. Stop and close the JMS connection
Conn. stop ();
Session. close ();
Conn. close ();
From: http://www.mldn.cn/articleview/2007-2-6/article_view_1377.htm