creating a sample ems application in java
TRANSCRIPT
CREATING A SAMPLE EMS CLIENT APPLICATION
Prerequisites
Add the full pathnames for the following jar files to the CLASSPATH
o jms.jar
o tibjms.jar
o tibcrypt.jar (For SSL)
Import the packages
o import javax.jms.*;
o import javax.naming.*;
Creating a Connection Factory
Tuesday, April 11, 2023 Page 1 of 9
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
Tuesday, April 11, 2023 Page 2 of 9
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
Setting an Exception Listener
Tuesday, April 11, 2023 Page 3 of 9
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 object’s 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);
Tuesday, April 11, 2023 Page 4 of 9
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 the producer's default delivery mode. Set whether message IDs are disabled. Set whether message timestamps are disabled. Set the producer's default priority. Set 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
Tuesday, April 11, 2023 Page 5 of 9
Message Consumer for Queues
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 connection’s 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();
Tuesday, April 11, 2023 Page 6 of 9
public void onMessage(Message message){
/* 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);
Tuesday, April 11, 2023 Page 7 of 9
Getting a message property
userId = Message.getStringProperty(“JMS_TIBCO_SENDER”);
Sending Messages
Use the MessageProducer object’s 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 object’s start() method to start the connection
connection.start();
Use the MessageConsumer object’s 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();
Tuesday, April 11, 2023 Page 8 of 9
When the client has finished receiving messages, it uses the Close() method to close the connection
connection.close();
Tuesday, April 11, 2023 Page 9 of 9