Copyright © TIBCO Software Inc. All Rights Reserved
Copyright © TIBCO Software Inc. All Rights Reserved


Chapter 12 Developing an EMS Client Application : Creating a Message Producer

Creating a Message Producer
As described in JMS Message Models, a Message Producer is an EMS client that either publishes messages to a topic or sends messages to a queue. When working with topics, a Message Producer is commonly referred to as a Publisher. Optionally, when creating a Message Producer, you can set the destination to NULL and specify the destination when you send or publish a message, as described in Sending Messages.
You must have send permission on a queue to create a message producer that sends messages to that queue. You must have durable permission on the topic to create a new durable subscriber for that topic, and have at least use_durable permission on the topic to attach to an existing durable subscriber for the topic. See User Permissions for details.
The following examples create a message producer that sends messages to the queue that was dynamically created in Dynamically Creating Topics and Queues.
Java
Use the Session object’s createProducer() method to create a MessageProducer object:
   MessageProducer QueueSender = session.createProducer(queue);
See the tibjmsMsgProducer.java sample client for a working example.
C
Use the tibemsSession_CreateProducer function to create a message producer of type tibemsMsgProducer:
   tibemsMsgProducer QueueSender = NULL;
   status = tibemsSession_CreateProducer(session,
                     &QueueSender,queue);
See the tibemsMsgProducer.c sample client for a working example.
C#
Use the Session.CreateProducer method to create a MessageProducer object:
   MessageProducer QueueSender = session.CreateProducer(queue);
See the csMsgProducer.cs sample client for a working example.
Configuring a Message Producer
A message producer can be configured to generate messages with default headers and properties that define how those messages are to be routed and delivered. Specifically, you can:
For example, as described in the Message Delivery Modes, you can set the message deliver mode to either PERSISTENT, NON_PERSISTENT, or RELIABLE_DELIVERY.
Java
Use the MessageProducer object’s setDeliveryMode() method to configure your Message Producer with a default delivery mode of RELIABLE_DELIVERY:
   QueueSender.setDeliveryMode(
            com.tibco.tibjms.Tibjms.RELIABLE_DELIVERY);
To configure the Message Producer with a default delivery mode of NON_PERSISTENT:
   QueueSender.setDeliveryMode(
            javax.jms.DeliveryMode.NON_PERSISTENT);
See the tibjmsMsgProducerPerf.java sample client for a working example.
 
Delivery mode cannot be set by using the Message.setJMSDeliveryMode() method. According to the JMS specification, the publisher ignores the value of the JMSDeliveryMode header field when a message is being published.
C
Use the tibemsMsgProducer_SetDeliveryMode function to configure your Message Producer to set a default delivery mode for each message it produces to RELIABLE_DELIVERY:
   tibems_int deliveryMode = TIBEMS_RELIABLE;
   status tibemsMsgProducer_SetDeliveryMode(QueueSender,
                                            deliveryMode);
C#
Set the DeliveryMode on the MessageProducer object to RELIABLE_DELIVERY:
   QueueSender.DeliveryMode = DeliveryMode.RELIABLE_DELIVERY;
See the csMsgProducerPerf.cs sample client for a working example.
Creating a Completion Listener for Asynchronous Sending
TIBCO Enterprise Message Service provides APIs for a Message Producer to send messages either synchronously or asynchronously. For asynchronous sending, you need to implement a CompletionListener that serves as an asynchronous event handler for message send result notification.
A completion listener implementation has two methods: onCompletion() is invoked after a message has successfully been sent, and onException() is invoked if the send failed. These methods are invoked in a different thread from that in which the message was sent. You implement the methods to perform the desired actions when the application is notified of send success or failure. Your implementation should handle all exceptions, and it should not throw any exceptions.
Once you create a completion listener, you pass it as an argument into the MessageProducer send method, or into the JMSProducer setAsync() method. If passed into the JMSProducer setAsync method, the JMSProducer will always send asynchronously.
Java
Create an implementation of the CompletionListener interface, create a CompletionListener and pass that into the appropriate send method:
/* create connection, session, producer, message */
TibjmsCompletionListener completionListener = new
  TibjmsCompletionListener();
msgProducer.send(destination, msg, completionListener);
Create a CompletionListener class and Implement the onCompletion() and onException() method to perform the desired actions when a message arrives:
class TibjmsCompletionListener implements CompletionListener
{
  public void onCompletion(Message msg)
  {
    /* Handle the send success case for the message */
  }
 
  public void onException(Message msg, Exception ex)
  {
    /* Handle the send failure case for the message */
  }
}
See the tibjmsMsgProducer.java sample client for a working example.
C
In C, Implement an onCompletion() function to perform the desired actions when a message is sent:
static void
onCompletion(tibemsMsg msg, tibems_status status, void* closure)
{
if (status == TIBEMS_OK)
{
/* Handle the send success case for the message */
}
else
{
/* Handle the send failure case for the message */
}
}
/* Create a connection, session, and producer. When sending, pass
* the onCompletion() function as the tibemsMsgCompletionCallback
*/
status = tibemsMsgProducer_AsyncSend(producer, msg, onCompletion, NULL);
See the tibemsMsgProducer.c sample client for a working example.
C#
Create an implementation of the ICompletionListener interface, create a CompletionListener and pass that into the appropriate send method.
EMSCompletionListener completionListener = new EMSCompletionListener();
 
producer.Send(destination, msg, completionListener);
Create an implementation of the IMessageListener interface to perform actions when a message is sent:
class EMSCompletionListener : ICompletionListener
{
  public void OnCompletion(Message msg)
  {
    /* Handle the send success case for the message */
  }
 
  public void OnException(Message msg, Exception ex)
  {
    /* Handle the send failure case for the message */
  }
}
See the csMsgProducer.cs sample client for a working example.

Copyright © TIBCO Software Inc. All Rights Reserved
Copyright © TIBCO Software Inc. All Rights Reserved