Destinations:
A destination is the object a client uses to specify the target of messages it produces and the source of messages it consumes.
In the PTP messaging domain, destinations are called queues.
command to create queue : j2eeadmin -addJmsDestination queue_name queue
In the pub/sub messaging domain, destinations are called topics
command to create topic : j2eeadmin -addJmsDestination topic_name topic
The following line of code performs a JNDI API lookup of the previously created topic MyTopic and assigns it to a Topic object:
Topic myTopic = (Topic) ctx.lookup("MyTopic");
The following line of code looks up a queue named MyQueue and assigns it to a Queue object:
Queue myQueue = (Queue) ctx.lookup("MyQueue");
Connections:
A connection encapsulates a virtual connection with a JMS provider. A connection could represent an open TCP/IP socket between a client and a provider service daemon.
Like connection factories, connections come in two forms, implementing either the QueueConnection or the TopicConnection interface. For example, once you have a QueueConnectionFactory or a opicConnectionFactory object, you can use it to create a connection:
QueueConnection queueConnection = queueConnectionFactory.createQueueConnection();
TopicConnection topicConnection = topicConnectionFactory.createTopicConnection();
When an application completes, you need to close any connections that you have created. Failure to close a connection can cause resources not to be released by the JMS provider. Closing a connection also closes its sessions and their message producers and message consumers.
queueConnection.close();
topicConnection.close();
Before your application can consume messages, you must call the connection’s start method. If you want to stop message delivery temporarily without closing the connection, you call the stop method.
Sessions:
A session is a single-threaded context for producing and consuming messages. You use sessions to create message producers, message consumers, and messages. Sessions serialize the execution of message listeners.
A session provides a transactional context with which to group a set of sends and receives into an atomic unit of work.
Sessions, like connections, come in two forms, implementing either the QueueSession or the TopicSession interface. For example, if you created a Topic-Connection object, you use it to create a TopicSession:
TopicSession topicSession =topicConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
The first argument means that the session is not transacted; the second means that the session automatically acknowledges messages when they have been received successfully.
Similarly, you use a QueueConnection object to create a QueueSession:
QueueSession queueSession =queueConnection.createQueueSession(true, 0);
Here, the first argument means that the session is transacted; the second indicates that message acknowledgment is not specified for transacted sessions.
Message Producers:
A message producer is an object created by a session and is used for sending messages to a destination. The PTP form of a message producer implements the Queue-Sender interface. The pub/sub form implements the TopicPublisher interface.
For example, you use a QueueSession to create a sender for the queue myQueue, and you use a TopicSession to create a publisher for the topic myTopic:
QueueSender queueSender = queueSession.createSender(myQueue);
TopicPublisher topicPublisher = topicSession.createPublisher(myTopic);
You can create an unidentified producer by specifying null as the argument to createSender or createPublisher.With an unidentified producer, you can wait to specify which destination to send the message to until you send or publish a message. Once you have created a message producer, you can use it to send messages.With a Queue-Sender, you use the send method:
queueSender.send(message);
With a TopicPublisher, you use the publish method:
topicPublisher.publish(message);
If you created an unidentified producer, use the overloaded send or publish method
that specifies the destination as the first parameter.
A destination is the object a client uses to specify the target of messages it produces and the source of messages it consumes.
In the PTP messaging domain, destinations are called queues.
command to create queue : j2eeadmin -addJmsDestination queue_name queue
In the pub/sub messaging domain, destinations are called topics
command to create topic : j2eeadmin -addJmsDestination topic_name topic
The following line of code performs a JNDI API lookup of the previously created topic MyTopic and assigns it to a Topic object:
Topic myTopic = (Topic) ctx.lookup("MyTopic");
The following line of code looks up a queue named MyQueue and assigns it to a Queue object:
Queue myQueue = (Queue) ctx.lookup("MyQueue");
Connections:
A connection encapsulates a virtual connection with a JMS provider. A connection could represent an open TCP/IP socket between a client and a provider service daemon.
Like connection factories, connections come in two forms, implementing either the QueueConnection or the TopicConnection interface. For example, once you have a QueueConnectionFactory or a opicConnectionFactory object, you can use it to create a connection:
QueueConnection queueConnection = queueConnectionFactory.createQueueConnection();
TopicConnection topicConnection = topicConnectionFactory.createTopicConnection();
When an application completes, you need to close any connections that you have created. Failure to close a connection can cause resources not to be released by the JMS provider. Closing a connection also closes its sessions and their message producers and message consumers.
queueConnection.close();
topicConnection.close();
Before your application can consume messages, you must call the connection’s start method. If you want to stop message delivery temporarily without closing the connection, you call the stop method.
Sessions:
A session is a single-threaded context for producing and consuming messages. You use sessions to create message producers, message consumers, and messages. Sessions serialize the execution of message listeners.
A session provides a transactional context with which to group a set of sends and receives into an atomic unit of work.
Sessions, like connections, come in two forms, implementing either the QueueSession or the TopicSession interface. For example, if you created a Topic-Connection object, you use it to create a TopicSession:
TopicSession topicSession =topicConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
The first argument means that the session is not transacted; the second means that the session automatically acknowledges messages when they have been received successfully.
Similarly, you use a QueueConnection object to create a QueueSession:
QueueSession queueSession =queueConnection.createQueueSession(true, 0);
Here, the first argument means that the session is transacted; the second indicates that message acknowledgment is not specified for transacted sessions.
Message Producers:
A message producer is an object created by a session and is used for sending messages to a destination. The PTP form of a message producer implements the Queue-Sender interface. The pub/sub form implements the TopicPublisher interface.
For example, you use a QueueSession to create a sender for the queue myQueue, and you use a TopicSession to create a publisher for the topic myTopic:
QueueSender queueSender = queueSession.createSender(myQueue);
TopicPublisher topicPublisher = topicSession.createPublisher(myTopic);
You can create an unidentified producer by specifying null as the argument to createSender or createPublisher.With an unidentified producer, you can wait to specify which destination to send the message to until you send or publish a message. Once you have created a message producer, you can use it to send messages.With a Queue-Sender, you use the send method:
queueSender.send(message);
With a TopicPublisher, you use the publish method:
topicPublisher.publish(message);
If you created an unidentified producer, use the overloaded send or publish method
that specifies the destination as the first parameter.