Está en la página 1de 8

CREATING A SAMPLE EMS CLIENT APPLICATION

Prerequisites Add the full pathnames for the following jar files to the CLASSPATH o o o jms.jar tibjms.jar tibcrypt.jar (For SSL)

Import the packages o o import javax.jms.*; import javax.naming.*;

Creating a Connection Factory

Thursday, March 22, 2012 Page 1 of 8

A client must connect to a running instance of the EMS server to perform any JMS operations. A connection factory is an object that encapsulates the data used to define a client connection to an EMS server. We are dynamically creating the connection factories in this example. We can also lookup for the connection factories from a data store by means of a naming service like JNDI or a LDAP server Create a string serverUrl, and set it in the form protocol://host:port serverUrl = protocol://host:port

Create a TibjmsConnectionFactory object in client ConnectionFactory factory = new com.tibco.tibjms.TibjmsConnectionFactory(serverUrl);

Setting Connection Attempts, Timeout and Delay Parameters

Default : Client will attempt to connect to the server TWO times with 500 ms delay between each attempt. Can set it through the code in this way.

factory.setConnAttemptCount(10); factory.setConnAttemptDelay(1000); factory.setConnAttemptTimeout(1000);

Connecting to the EMS Server

A connection with the EMS server is defined by the Connection object obtained from a Connection Factory. Connection connection = factory.createConnection(username, password);

Starting, Stopping and Closing a Connection

Before consuming messages, the Message Consumer client must "start" the connection.

Thursday, March 22, 2012 Page 2 of 8

If you wish to temporarily suspend message delivery, you can "stop" the connection. When a client application exits, all open connections must be "closed." Unused open connections are eventually closed, but they do consume resources that could be used for other applications. Closing a connection also closes any sessions created by the connection.

Creating a Session

A Session is a single-threaded context for producing or consuming messages. Message Producers, Message Consumers can be created using session objects. Use fully qualified names while mentioning the Acknowledge modes. Session session = connection.createSession (boolean transacted,int ackmode);

transacted = false signifies that the session is not transacted. Acknowledge modes Ignored when the session is transacted Valid values : Session.AUTO_ACKNOWLEDGE, Session.CLIENT_ACKNOWLEDGE, and Session.DUPS_OK_ACKNOWLEDGE

Setting an Exception Listener

Thursday, March 22, 2012 Page 3 of 8

We can set an exception listener on the connection that gets invoked when a connection breaks or experiences a fault-tolerant switchover. Implement the interface ExceptionListener and implement its method onException. Use the connection objects setExceptionListener to register the exception listener Call Tibjms.setExceptionOnFTSwitch to call the exception handler after a fault-tolerant switchover

public class myConsumer implements ExceptionListener { public void onException(JMSException e) { /* Handle exception */ } connection.setExceptionListener(this); com.tibco.tibjms.Tibjms.setExceptionOnFTSwitch(true); }

Dynamically creating Topics and Queues

Creating a topic Destination topic = session.createTopic(topicName);

Creating a queue Destination queue = session.createQueue(queueName);

Thursday, March 22, 2012 Page 4 of 8

Creating Message Producer

Message Producer for a Topic MessageProducer topicSender = session.createProducer(topic);

Message Producer for a Queue MessageProducer queueSender = session.createProducer(queue);

Configuring a Message Producer

We can define the properties of the messages. To be specific, we can Set Set Set Set Set the producer's default delivery mode. whether message IDs are disabled. whether message timestamps are disabled. the producer's default priority. the default length of time that a produced message should be retained by the message system.

Through code, we can set in this way QueueSender.setDeliveryMode(com.tibco.tibjms.Tibjms.NON_PERSISTENT);

Delivery mode cannot be set by using Message.setJMSDeliveryMode() method. According to the JMS specification, the publisher ignores the value of the JMSDeliveryMode header field when a message is being published.

Creating a Message Receiver

Message Consumer for Queues

Thursday, March 22, 2012 Page 5 of 8

MessageConsumer QueueReceiver = session.createConsumer(queue); Message Consumer for Topic MessageConsumer TopicReceiver = session.createConsumer(topic); Creating a Durable Subscriber for Topic TopicSubscriber subscriber = session.createDurableSubscriber(topic,myDurable);

Creating a Messaging Listener for Asynchronous Message Consumption

For Synchronous Consumption: Call receive() method For Asynchronous Consumption: Implement a MessageListener that serves as an asynchronous event handler for messages A Message Listener implementation has one method, onMessage, that is called by the EMS server when a message arrives on a destination. You implement the onMessage method to perform the desired actions when a message arrives. Your implementation should handle all exceptions, and it should not throw any exceptions. Once you create a Message Listener, you must register it with a specific Message Consumer before calling the connections start method to begin receiving messages. A Message Listener is not specific to the type of the destination. The same listener can obtain messages from a queue or a topic, depending upon the destination set for the Message Consumer with which the listener is registered.

public class tibjmsAsyncMsgConsumer implements MessageListener{ /* Create a connection, session and consumer */ MessageConsumer QueueReceiver = session.createConsumer(queue); QueueReceiver.setMessageListener(this); connection.start();

public void onMessage(Message message){

Thursday, March 22, 2012 Page 6 of 8

/* Process message and handle exceptions */ } }

Do not use Session.setMessageListener(), which is used by app servers rather than the application

Working with Messages

EMS works with the following types of messages: Messages with no body Text Messages Map Messages Bytes Messages Stream Messages Object Messages

There is a separate create method for each type of message.

Creating a message TextMessage message = session.createTextMessage(NewMessage); Setting a message property Message.setBooleanProperty(JMS_TIBCO_COMPRESS, true); Getting a message property userId = Message.getStringProperty(JMS_TIBCO_SENDER);

Thursday, March 22, 2012 Page 7 of 8

Sending Messages Use the MessageProducer objects send() method to send a message to the destination specified by the MessageProducer object. QueueSender.send(message); Use the following form of the send() method to send a message to a specific destination MessageProducer NULLsender = session.createProducer(null); NULLsender.send(topic, message);

Receiving Messages Before receiving messages, the Message Consumer must start the connection to the EMS server. Before exiting, the Message Consumer must close the connection. Use the Connection objects start() method to start the connection connection.start(); Use the MessageConsumer objects receive() method to receive a message. This is typically used in a loop for the duration the client wishes to receive messages Message message = QueueReceiver.receive(); When the client has finished receiving messages, it uses the Close() method to close the connection connection.close();

Thursday, March 22, 2012 Page 8 of 8

También podría gustarte