JMS programming mode

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.