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
, etc. The sendMessage
method then delivers the message to the Patch Bay. Depending on how the Patch Bay is configured, that message will then be delivered to a JMS destination (or group of destinations).
If any of those destinations are managed by Local JMS, then the sendMessage
call will not return until the message has been delivered to all of the message sinks attached to the Local JMS destinations. Destinations that are not managed by Local JMS (such as those managed by SQL JMS) will deliver messages asynchronously. In other words, the sendMessage
call will return immediately, even if the messages have not yet been delivered to their final recipients.
If the destinations are managed by a transactional JMS provider (such as SQL JMS), then any messages sent through sendMessage
are not actually sent until the overall transaction is committed. If the transaction rolls back, then none of the messages are sent. Note that 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.