0% found this document useful (0 votes)
24 views

JMS Program Technical Flow

The document discusses how clients produce and consume JMS messages using Java code. It provides examples of how to: 1) Lookup or instantiate a ConnectionFactory to create connections and sessions. 2) Use sessions to create message producers and consumers to send and receive messages from destinations like queues and topics. 3) Implement asynchronous message receipt using message listeners. It also provides an overview of JBoss Messaging configuration files and how the message cache manages memory usage.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

JMS Program Technical Flow

The document discusses how clients produce and consume JMS messages using Java code. It provides examples of how to: 1) Lookup or instantiate a ConnectionFactory to create connections and sessions. 2) Use sessions to create message producers and consumers to send and receive messages from destinations like queues and topics. 3) Implement asynchronous message receipt using message listeners. It also provides an overview of JBoss Messaging configuration files and how the message cache manages memory usage.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Producing and Consuming Messages

1> Client to Produce Messages :Use the Java Naming and Directory Interface (JNDI) to find a ConnectionFactory object, or instantiate a ConnectionFactory object directly and set its attributes. A client uses a connection factory, which is an instance of either QueueConnectionFactory (point-to-point) or TopicConnectionFactory (publish/subscribe), to create a connection to a provider. The following snippet of code demonstrates how to use JNDI to find a connection factory object:

Context ctx = new InitialContext(); ConnectionFactory cf1 = (ConnectionFactory)ctx.lookup("jms/QueueConnectionFactory"); ConnectionFactory cf2 = (ConnectionFactory) ctx.lookup("/jms/TopicConnectionFactory");

Alternatively, you can directly instantiate a connection factory as follows: ConnectionFactory connFactory = new com.sun.messaging.ConnectionFactory();

2> Use the ConnectionFactory object to create a Connection object. This can be done as follows:
Connection connection = connFactory.createConnection();

3> Use the Connection object to create one or more Session objects, which provide transactional context with which to group a set of sends and receives into an atomic unit of work. A session can be created as follows:
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE) The createSession() method takes two arguments: the first (false in this case) means that the session is not transacted, and the second means that the session will automatically acknowledge messages when they have been received successfully.

4> Use JNDI to find Destination object(s), or instantiate one directly and configure it by setting its attributes.
A destination object is used by the client to specify the source of messages it consumes and the target of messages it produces. In the point-to-point messaging, destinations are known as queues, and in the publish/subscribe model of messaging, they are known as topics. The following snippet of code demonstrates how to perform a JNDI lookup of a queue named jms/SomeQueue: Destination dest = (Queue) ctx.lookup("jms/SomeQueue");

5> Use a Session and a Destination object to create the needed MessageProducer object, which are used for sending messages to a destination. The code below shows how that is done.
MessageProducer producer = session.createProducer(SomeQueue OR SomeTopic);

6> Once a producer has been created, it can be used to send messages as follows:
producer.send(message);

Client to Consume Messages


The first three steps are the same as above.

1> Use a Session object and a Destination object to create any needed MessageConsumer objects that are used for receiving messages. This can be done as follows:
MessageConsumer consumer = session.createConsumer(SomeQueue or SomeTopic); Once the consumer has been created, it can be used to receive messages. Message delivery, however, doesn't begin until you start the connection created earlier, which can be done by calling the start() method: connection.start(); Message msg = consumer.receive(); A long parameter can be passed to the receive() method to specify a time-out (for example, 3000L for 3 seconds). It is important to note that the receive() method is used to consume a message synchronously. In order to consume a message asynchronously, a message listener is used.

2> If asynchronous communication is desired, instantiate a MessageListener object and register it with a MessageConsumer object. A MessageListener object acts as an asynchronous event handler for messages. The MessageListener interface contains one method, onMessage(), which you implement to receive and process the messages.
In the following snippet of code, the class MyListener implements the MessageListener interface. The message listener is registered with a specific consumer using the setMessageListener(): MessageListener listener = new MyListener(); consumer.setMessageListener(listener);

3> Instruct the Connection object to start delivery of messages by calling the start() method.

JBoss Messaging Configuration


The configuration and service files that make up the JBossMQ system include:

deploy/hsqldb-jdbc-state-service.xml: This configures the JDBC state service for storing state in the embedded Hypersonic database.

deploy/jms/hsqldb-jdbc2-service.xml: This service descriptor configures the DestinationManager, MessageCache, and jdbc2 PersistenceManager for the embedded Hypersonic database.

deploy/jms/jbossmq-destinations-service.xml: This service describes defines default JMS queue and topic destination configurations used by the testsuite unit tests. You can add/remove destinations to this file, or deploy another *-service.xml descriptor with the destination configurations.

Message Cache
Messages created in the server are passed to the MessageCache for memory management. JVM memory usage goes up as messages are added to a destination that does not have any receivers. These messages are held in the main memory until the receiver picks them up. If the MessageCache notices that the JVM memory usage starts passing the defined limits, the MessageCache starts moving those messages from memory to persistent storage on disk. The MessageCache uses a least recently used (LRU) algorithm to determine which messages should go to disk.

You might also like