A message source must implement the atg.dms.patchbay.MessageSource
interface. Through this interface, the message source is assigned a MessageSourceContext
that it can use to create and send messages. The following example demonstrates how to do this:
import atg.dms.patchbay.*; import javax.jms.*; ... MessageSourceContext mContext; boolean mStarted = false; // These methods implement the MessageSource interface public void setMessageSourceContext (MessageSourceContext pContext) { mContext = pContext; } public void startMessageSource () { mStarted = true; } public void stopMessageSource () { mStarted = false; } // This method will send a message public void sendOneMessage () throws JMSException { if (mStarted && mContext != null) { TextMessage msg = mContext.createTextMessage (); msg.setJMSType ("atg.test.Test1"); msg.setText ("Test text string"); mContext.sendMessage (msg); } }
The setMessageSourceContext
, startMessageSource
, and stopMessageSource
methods implement the MessageSource
interface. Messages can be sent from any method in the message source, such as the sendOneMessage
method in the example.
The sendOneMessage
method makes sure that startMessageSource
has been called. It then creates, populates, and sends a TextMessage
. Typically the only data that needs to be set on a message is the JMSType
and the data in the message’s body. A TextMessage
’s body is set by calling setText
, an ObjectMessage
’s body is set by calling setObject
, and so on. The sendMessage
method then delivers the message to Patch Bay. Depending on how Patch Bay is configured, that message is delivered to a JMS destination or group of destinations.
If any of those destinations are managed by Local JMS, the sendMessage
call does not return until the message is delivered to all message sinks attached to the Local JMS destinations. Destinations that are not managed by Local JMS (such as those managed by SQL JMS) deliver messages asynchronously. In other words, the sendMessage
call returns immediately, even if the messages are not yet delivered to their final recipients.
If the destinations are managed by a transactional JMS provider (such as SQL JMS), any messages sent through sendMessage
are not actually sent until the overall transaction is committed. If the transaction rolls back, none of the messages are sent. This does not apply to Local JMS; because Local JMS is synchronous, sending a message happens instantly, without waiting for the current transaction to complete.