Here are the steps to send/receive message from client side, (java code)
- Create a connection factory.
ConnectonFactory f = new com.tibco.tibjms.TibjmsConnectionFactory(String serverURL);
- Create a connection
Connection c = f.createConnection(String username, String password);
If the server property authorization is disabled, you do not need to specify the username and password.
- Create a session
Session s=c.createSession(Boolean transcationFlag, Int delivery_mode);
- Get destination
Javax.jms.Queue d=s.CreateQueue(String queueName);
Javax.jms.Topic d=s.CreateTopic(String topicName);
Once the Queue/Topic exists, then return the destination object.
- Create message producer to send message, or
MessageProducer p=s.createProducer(Destination d);
Create a Message object, TextMessage object for example,
TextMessage tm = s.createTextMessage();
You can set property for the message and the text body.
tm.setText(String textBody);
tm.setStringProperty(String propertyName, String propertyValue);
p.send(tm);
- Create message consumer to receive message
MessageConsumer p=s.createConsumer(Destination d, String selector);
For synchronously receiver,
p.receive();
For asynchronously receiver, you need to implement MessageListener class and restruture the onMessage function.
Class myMessageListener implements MessageListener
{
Public void onMessage(Message message)
{
// add code here to handle if message is sent to queue/topic
System.out.println(“The imcoming message is: “+message);
}
}
In the main class, set consumer listener
p.setMessageListener(new myMessageListener);
Note that, for the MessageConsumer class, you need to start the connection before receive message from Queue/Topic
- start();
Note: another way to get the ConnectionFactory/Queue/Topic object JNDI interface.