# Message Broker Publish Control

Two Message Broker controls are available from your business processes: Publish and Subscription. Your business process uses a Publish control to publish messages to Message Broker channels. You bind the Message Broker channel to the Publish control when you declare the control, but it can be overridden dynamically. You can add additional methods to your extension (subclass) of the Message Broker Publish control.

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 Publish controls:

- To Create an Instance of a Message Broker Publish Control
- Using Methods of the MB Publish Interface
- Example Code for MB Publish Control

To Create an Instance of a Message Broker Publish Control

1. Click Add on the Controls tab to display a list of controls that represent the resources with which your business process can interact.

Note: If the Controls tab is not visible in WebLogic Workshop, click View —> Windows —> Data Palette from the menu bar.

2. Choose Integration Controls to display the list of controls used for integrating applications.
3. Choose MB Publish. The Insert Control dialog box is displayed.
4. In the Insert Control dialog box (Step 1), enter a name for the instance of this control. The name you enter must be a valid Java identifier.
5. In the Insert Control dialog box (Step 2), select one of the following options:

- Use a MB Publish control already defined by a JCX file

Enter a filename for the MB control in the JCX file field, or click Browse to find the JCX file in your file system.

- Create a new MB Publish control to use

Enter a filename in the New JCX name field.

6. In the Insert Control dialog box (Step 3), specify the channel name as follows:

- channel-name—Select a channel to which you want your business process to publish.

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?.

- message type—This read-only field displays the type of data published to the specified channel: String, XmlObject, RawData.
- metadata type—This read-only field displays the metadata type value if qualifiedMetadataType is set in the channel definition.

7. Click Create to close the Insert Control dialog box.

An instance of a MB Publish control is created in your project and displayed in the Controls tab. The following figure shows an example instance of a MB Publish control displayed in the Controls tab:

#### JCX File for Your MB Publish Control

When you create a new MB Publish 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.PublishControl;
    import com.bea.data.RawData;
    import com.bea.xml.XmlObject;

    /**
     * Defines a new Publish control.
     *
     * @jc:mb-publish-control channel-name="/controls/channe1"
     */

    public interface mbPublish extends PublishControl,
     com.bea.control.ControlExtension
        /**
         * @jc:mb-publish-method message-body="{value}"
         */
        void publish(String value);
        /**
         * @jc:mb-publish-method message-metadata="{metadata}" message-body="{value}"
         */
        void publishWithMetadata(XmlObject metadata, String value);
    }

### Using Methods of the MB Publish Interface

This section describes the MB Publish control interface. Use the methods from within your application to publish to Message Broker channels.

MB Publish Control Interface

    package com.bea.control;

    import com.bea.wli.control.dynamicProperties.PublishControlPropertiesDocument;
    import org.w3c.dom.Element;
    import weblogic.jws.control.Control;

    /**
     * Message Broker Publish control base interface
     */

    public interface PublishControl extends Control {

        /**
         * Temporarily sets the message headers to use in the next publish operation
         * @param headers headers to set
         */

        void setOutputHeaders(Element[] headers);

        /**
         * Sets the dynamic properties for the control
         * @param props the dynamic properties for the control
         */

         void setProperties(PublishControlPropertiesDocument props);
        /**
         * Sets the dynamic properties for the control
         * @return the current properties for the control
         */

         PublishControlPropertiesDocument getProperties();
    }

The PublishControlPropertiesDocument XML Bean is defined in DynamicProperties.xsd which is located in the Schemas folder of each process application.

To learn more about the methods available on the MB Publish control, see the PublishControl Interface Javadoc.

#### Method Attributes

The following method attributes determine the behavior of the MB Publish control.

Class attributes include:

channel-name

The name of the Message Broker channel to which the MB Publish control publishes messages.

message-metadata

By default, this XML header is included in messages published with this control. Valid values include a string containing XML.

Method attributes include:

message-metadata

XML header to include in messages published with the control method to which it is associated. Valid values include a string containing XML, or a method parameter in curly braces. For example: {parameter1}.

message-body

Valid values include a string containing text that is used as the message body in the published message, or a method parameter in curly braces. For example: {parameter2}.

### Example Code for MB Publish Control

The Publish control allows you to override class-level annotations with dynamic properties. To do so, use an XML variable that conforms to the control's dynamic property schema.

The following is an example of an XML variable you can use to specify the dynamic properties:

    <PublishControlProperties>
        <channel-name>potopic</channel-name>
        <message-metadata>
            <custom-header>ACME Corp</custom-header>
        <message-metadata>
    </PublishControlProperties>

The XML Schema for the MB Publish control dynamic properties is shown in the following listing. You can obtain this schema by adding the WLI Schemas project template to you application. You can get and set these properties using the getProperties and setProperties methods.

    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
    targetNamespace="http://www.bea.com/wli/control/dynamicProperties"
    xmlns="http://www.bea.com/wli/control/dynamicProperties"
    elementFormDefault="qualified">
        <xs:element name="PublishControlProperties">
            <xs:complexType>
                <xs:sequence>
                    <xs:element name="channel-name" type="xs:string"
                                minOccurs="0" maxOccurs="1" />
                    <xs:element name="message-metadata" type="header"
                                minOccurs="0" maxOccurs="1" />
                </xs:sequence>
            </xs:complexType>
        </xs:element>

    <!-- The following complex-type represents any arbitrary sequence of XML content -->

        <xs:complexType name="header">
            <xs:sequence>
                <xs:any namespace="##other" minOccurs="0"
                        maxOccurs="unbounded" processContents="lax" />
            </xs:sequence>
        </xs:complexType>
    </xs:schema>

Example Code

MB Publish controls must be extended. The following is an example of how to code a MB Publish control in your JPD file.

    /*
     * @jc:mb-publish-control channel-name="/controls/potopic"
     */

    interface MyPublishControl extends PublishControl,com.bea.control.ControlExtension {
        /**
         * @jc:mb-publish-method
         *      message-metadata="<custom-header>ACME Corp</custom-header>"
         *      message-body="{myMsgToSend}"
         */

        void publishPO(XmlObject myMsgToSend);
    }

    /*
     * @common:control
     */
    private MyPublishControl pubCtrl;

    // publish a message
    void sendIt(XmlObject myMsgToSend) {
        pubCtrl.publishPO(myMsgToSend);
    }