+

Search Tips   |   Advanced Search

Invoking operations using custom operation selectors

If the application cannot use a default JMS binding operation selection or JMS user property operation selection to determine the target operation, we can use a JMS custom operation selector to invoke operations.

  • Configure the JMS binding for the SCA application.

    IBM recommends that the SCA component implementations contain only business logic to improve their portability, such as over other bindings. To improve portability, use the custom operation selector to hold the JMS-specific logic that handles an incoming JMS Message, enabling the component implementation to focus on binding-neutral business logic.

    We can configure a JMS custom operation selector in an SCA composite definition without any changes to the application. To specify the JMS custom operation selection, add the following operation selection element to the composite definition file:

    OSOA

      {http://www.ibm.com/xmlns/prod/websphere/sca/1.0/2007/06}operationSelector.jmsCustom

    OASIS

      {http://www.ibm.com/xmlns/prod/websphere/sca/1.1}operationSelector.jmsCustom

    The custom operation selector must implement the following interface:

    package com.ibm.websphere.soa.sca.operationselector.jms.OperationSelector;
    import javax.jms.Message;
     public interface OperationSelector {
      public String getOperationName(Message msg);
      public WireFormatContext getWireFormatContext();
      public void setWireFormatContext(WireFormatContext ctx);

    The interface exposes an instance of javax.jms.Message and enables interaction with the JMS user properties and message body. After doing operation selection, we do not need to use the reset() method to reposition the byte stream cursor for a BytesMessage. The run time automatically resets the cursor in all cases so that the wire format handler is not impacted by the operation selector reading the message first.

    If the message body of a BytesMessage is read using any of the read methods available on a BytesMessage, reposition the byte stream to the beginning using the reset() method so that message processors can read the data in the entire message body intended for the targeted operation.

    The wire format context is held by an instance of the com.ibm.websphere.soa.sca.wireformat.WireFormatContext class, which is passed with each invocation. The WireFormatContext class provides a java.util.Map interface where we can set property key and value pairs in the context. The WireFormatContext interface also provides several methods to extract useful information about the current context such as component and service names, invocation types, and the ability to mark exceptions. Refer to the Java documentation for the com.ibm.websphere.soa.sca.wireformat.WireFormatContext interface for a complete list of methods.

    1. In a composite definition file, configure a custom operation selector.

      Use the operationSelector.jmsCustom element and the class attribute to identify the customer operation selection implementation class. The custom operation selector applies when receiving a request at a service, or a callback at a reference.

      OSOA

      <component name="AccountComponent">
        <implementation.java ...>
        <service name="AccountService">
          <binding.jms>
            <destination ...>
            <response>
            ...
            </response>
            <xmlns:fep="http://www.ibm.com/xmlns/prod/websphere/sca/1.0/2007/06"
                      fep:operationSelector.jmsCustom
                      class="com.ibm.test.soa.sca.opsel.SimpleCustomOpSel"/>
          </binding.jms>
        </service>
      </component>

      OASIS

      <component name="AccountComponent">
        <implementation.java ...>
        <service name="AccountService">
          <binding.jms>
            <xmlns:fep="http://www.ibm.com/xmlns/prod/websphere/sca/1.1"
                      fep:operationSelector.jmsCustom
                      class="com.ibm.test.soa.sca.opsel.SimpleCustomOpSel"/>
            <destination ...>
            <response>
            ...
            </response>
          </binding.jms>
        </service>
      </component>

    2. Write code that implements the JMS custom operation selector class.

      For the SimpleCustomOpSel example class, we can use code such as the following:

      package com.ibm.test.soa.sca.opsel;
       import com.ibm.websphere.soa.sca.operationselector.jms.OperationSelector.*;
      import javax.jms.*;
       public class SimpleCustomOpSel implements OperationSelector {
           private WireFormatContext opSelContext;
           @Override
          public String getOperationName(Message msg) {
              String retVal = computeOperationName(msg);
              return retVal;
          }
           @Override
          public WireFormatContext getWireFormatContext() {
              return opSelContext;
          }
           @Override
          public void setWireFormatContext(WireFormatContext ctx) {
              this.opSelContext = ctx;
          }
           /*
           * There is a pre-packaged operationSelector that can do this, * <ts:operationSelector.jmsUser>.
           */
           private String computeOperationName(Message msg) {
               String opName = null;
               Integer val = null;
               try {
                   val = msg.getIntProperty("MyPropertyName");
                   if (val.equals(Integer.valueOf("1"))) {
                       opName = "addOperation";
                   } else if (val.equals(Integer.valueOf("2"))) {
      ...


    Results

    You have configured a custom operation selector.


    What to do next

    Package the operation selection implementation class with the application. The class must be loadable by an application-level class loader.

    Deploy and test the operation selector in our SCA application.


    Related tasks

  • Configure the SCA JMS binding
  • Invoking operations using JMS binding operation selection
  • Invoking operations using JMS user property operation selection

  • Service Component Architecture specifications and APIs