Documentos de Académico
Documentos de Profesional
Documentos de Cultura
Prerequisites Add the full pathnames for the following jar files to the CLASSPATH o o o jms.jar tibjms.jar tibcrypt.jar (For SSL)
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
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.
A connection with the EMS server is defined by the Connection object obtained from a Connection Factory. Connection connection = factory.createConnection(username, password);
Before consuming messages, the Message Consumer client must "start" the connection.
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
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); }
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.
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.
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);
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();
Do not use Session.setMessageListener(), which is used by app servers rather than the application
EMS works with the following types of messages: Messages with no body Text Messages Map Messages Bytes Messages Stream Messages Object Messages
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);
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();