Two Message Broker controls are available from your business processes: Publish and Subscription. Your business process uses a Subscription control to dynamically subscribe to channels and receive messages. You bind the channel and optionally, an XQuery expression for filtering messages, when you create an instance of the control for your business process. The bindings cannot be overridden dynamically.
The Subscription control interface includes methods that allow your business process to subscribe to and unsubscribe from the bound Message Broker channel.
Subscribe operations are part of the larger XA transaction, as with other business process operations. This allows subscribe operations to be rolled back if the business process operation fails. Because a subscription is in a transaction, you have to commit the transaction to make it durable. If you're doing non-transactional work, that is, if a subscribe operation must be committed before performing an action that might trigger a return message, use <transaction/> blocks in the flow to commit the current business process state, including the subscription.
For information on how to add control instances to business processes, see Using Controls in Business Processes.
The following topics provide information about creating and using Message Broker Subscription controls:
To Create an Instance of a Message Broker Subscription Control
Note: If the Controls tab is not visible in WebLogic Workshop, click View —> Windows —> Data Palette from the menu bar.
Enter a filename for the MB control in the JCX file field, or click Browse to find the JCX file in your file system.
Note: If no options are available in the channel-name field, you must create a channel file, which defines the channels to which your business process can publish and subscribe. To learn how to create this file, see How Do I: Create Message Broker Channels?.
An instance of a MB Subscription control is created in your project and displayed in the Controls tab. The following figure shows an example instance of a MB Subscription control displayed in the Controls tab:
/** * @common:control */ private processes.mbSubscribe mbSubscribe;
When you create a new MB Subscription control, you create a new JCX file in your project. The following example JCX file is automatically created by the control wizard:
import com.bea.control.SubscriptionControl; import com.bea.data.RawData; import com.bea.xml.XmlObject;
/**
* Defines a new Subscription control.
*
* @jc:mb-subscription-control channel-name="/controls/channel1"
*/
public interface mbSubscribe extends SubscriptionControl, com.bea.control.ControlExtension { /** * @jc:mb-subscription-method filter-value-match="{value}" */
void subscribeWithFilterValue(String value);
interface Callback extends SubscriptionControl.Callback { /** * @jc:mb-subscription-callback message-body="{message}" */
void onMessage(XmlObject message); } }
You must select the This subscription will be filtered check box to ensure that the subscribeWithFilterValue() method in included in the JCX file. The onMethod method on the Calback interface uses the message type defined in the channel file.
This section describes the MB Subscription control interface.The methods you can use to subscribe to Message Broker channels are available from within your application.
package com.bea.control;
import weblogic.jws.control.Control;
/** * Message Broker Subscription control base interface */
public interface SubscriptionControl extends Control {
/** * Subscribes the control to the message broker. If the subscription * uses a filter expression, then the default filter value will be * used. If no default filter value is defined in the annotations, * then a <tt>null</tt> filter value will be used, meaning that any * filter result will trigger a callback. */
void subscribe();
/** * Unsubscribes the control from the message broker, stopping * further events (messages) from being delivered to the control. */
void unsubscribe(); interface Callback { /**
* Internal callback method used to implement user-defined callbacks. * JPDs cannot and should not attempt to implement this callback method. * * @param msg the message that triggered the subscription * @throws Exception * void _internalCallback(Object msg) throws Exception; */ } }
Note: If the subscription uses a filter, you must define custom subscription methods to specify the filter value to be matched at run time.
The Subscription control does not define callback methods for you. You must define a custom callback to specify how the business process expects to receive the event messages. (Event messages can be XML, raw data, or string.)
To learn more about the methods available on the MB Subscription control, see the SubscriptionControl Interface Javadoc.
This section describes the class and method attributes supported for the Subscription control.
Callback method attributes include:
MB Subscription controls must be extended. The following is an example of how to code a MB Subscription control in your JPD file.
/* * @jc:mb-subscription-control * channel-name="/controls/stocks" * xquery="$message/StockSymbol/text()" */ interface MySubscriptionControl extends SubscriptionControl, ControlExtension { /** * @jc:mb-subscription-method * filter-value-match="BEA" */ void subscribeToBEA(); /** * @jc:mb-subscription-method * filter-value-match="{symbol}" */ void subscribeToSymbol(String symbol); interface Callback { /** * @jc:mb-subscription-callback message-body="{myMsgReceived}" */ onXMLMessage(XmlObject myMsgReceived); } } . . . /* * @common:control */ MySubscriptionControl subCtrl; // subscribe to a message
void subscribeIt() { subCtrl.subscribeToBEA(); } // receive a message after subscribing subCtrl_onXMLMessage(XmlObject myMsgReceived) { }
In addition to the dynamic subscriptions you design at Control nodes in your business process, you can design static subscriptions at Start nodes to receive messages from Message Broker channels.
To learn how to design static subscriptions to Message Broker channels at business process Start nodes, see Designing Start Nodes.