![]() |
|
For a Java client program to access and use the Message Server to send or receive messages, the client program has to perform the following steps.
The initial context (IC) can be set in many ways.
The simplest way is to pick up and use the default properties from jndi.properties file. The code for using setting IC from jndi.properties is:
Context ctx= new InitialContext();
However, only a single Message Server can use the default jndi.properties. If JMS is running along with a second server (say, Pramati Server), the second server may pick up the initial context from the jndi.properties file. The jndi.properties file will not contain the properties needed by the client application to connect to the Message Server. In this case, the client can access the Message Server using the Properties object.
Properties object hard codes the values of initial context as demonstrated in the code below:
env.put("java.naming.factory.initial",
"com.pramati.naming.client.PramatiClientContextFactory");
env.put("java.naming.provider.url","rmi://<IP>:<port>");
Context ic= new InitialContext(env);
Here IP is the IP address of the Message Server and port is port number of the Message Server naming service port. This sets the initial context, first to the values pointed to by the jndi.properties file, and then overwrites those environment settings that have been written by the code to the values specified in the code.
The following section demonstrates how to create a connection with the connection factory:
Context ic= new InitialContext();
queueConnectionFactory=(QueueConnectionFactory)ic.lookup("JMSQueueConnectionFactory");
queue=(Queue)ic.lookup("JMSQueue");
queueConnection= queueConnectionFactory.createQueueConnection();
Here, JMSQueue and JMSQueueConnectionFactory are names of QueueConnectionFactory and Queue objects that are there on the Message Server. Similarly, a connection could be made to a TopicConnectionFactory on a topic.
The following code shows how to close a queue connection: queueConnection.close();
The closure of the queue connection automatically ends all sessions created on the connection.
A session can be created using the connection factory. The following code demonstrates how a session can be created.
QueueSession session = queueConnection.createQueueSession(false, Session.AUTO_ACKONWLEDGE);
This creates a session of type Queue, to perform all Queue related operations.
A topic Session can be created using the topic connection, as shown below:
TopicSession session = topicConnection.createTopicSession(false, Session.AUTO_ACKONWLEDGE);Using JMS 1.1 API, a common session can be created which supports both the Queue and Topic operations.
Session session = connection.createSession(false, Session.AUTO_ACKONWLEDGE);
The arguments while creating the session are:
Session.AUTO_ACKONWLEDGE indicates that the acknowledgement for each receivedA message can be created using the session using the following command: Message message = session.createMessage();
The message object gets created with the standard header properties. This cannot hold any data that should be sent to the receiver. It can be sent when a header information is sufficient. To send data along with the header information SubClasses of the Message should be used. Following are the different kinds of JMS Messages:
Different kind of message objects can be created from a session using their respective methods. For example, to create a TextMessage, use the following code:
TextMessage message = session.createTextMessage();
message.setText("This is a text message");
The above code creates a TextMessage and sets the text on the message as "This is a text message".
A sender/producer can be created using the session. The producer produces the message and sends it to the server. A sender is associated with the Queue.
QueueSender sender = session.createSender(jmsqueue);
The above code creates a sender who can send messages to the JMSQueue.
To send a message to the JMSQueue, use the following code fragment:
send.sendMessage(message);
A sender should be closed using the close() method: sender.close();
A receiver/consumer can be created using the session. The receiver receives the message from the Server. A receiver is registered with the Queue to receive message on it.
QueueReceiver receiver = session.createReceiver(jmsQueue); Message message = receiver.receive();
This creates a receiver on the jmsQueue and waits for the message until it gets it. The caller gets blocked until there is a message exists for this receiver. The message can be any of the above types that are discussed. To check the type of the message, you can use instanceOf.
For example, to check if the received message is a TextMessage, use the following code snippet:
if(message instanceof TextMessage())
{TextMessage tm = (TextMessage) message.System.out.println(tm.getMessage());}
A close the reciever use the close() method on it. receiver.close();
The following code shows how to close a queue session: queueSession.close();
The topic session can also be closed similarly. It is a good programming habit to close sessions and connections wherever possible so that the Server provides best results under deployment conditions.
Pramati Message Server can also be accessed by the following clients:
| © Pramati Technologies 2007 | Runs on Pramati Server | Feedback | Legal |