Accessing JMS Transport headers and properties in message flow

In this recipe, we will show how to access the JMS Transport headers and properties in the message flow. This can be useful, for example, to decide for which different business services the message should be routed to.

We will implement a decision based on the JMS header JMSPriority and another decision based on the user-defined property myProperty.

Getting ready

For this recipe, we will use the queue SourceQueue from the OSB Cookbook standard environment.

Import the base OSB project containing the solution from the previous recipe into Eclipse from chapter-3getting-readyaccessing-jms-transport-headers-in-message-flow.

How to do it...

In Eclipse OEPE, perform the following steps to add the two decisions to the message flow of the proxy service:

  1. Open the proxy service JMSConsumer of the accessing-jms-transport-headers-in-message-flow.
  2. Navigate to the Message Flow tab.
  3. Right-click on the Log action and select Insert After | Flow Control | If Then to add an If Then action after the Log action.
  4. Select the If: <Condition> and navigate to the Properties tab.
  5. Click on <Condition>.
  6. In the Variable Structures tree on the right, expand the inbound node until you get to the JMSPriority of the transport | request - jms node.
  7. Drag the JMSPriority into the Expression field on the left. Add a > 5 to the right of the generated expression so that it reads: $inbound/ctx:transport/ctx:request/tp:headers/jms:JMSPriority > 5.
  8. Click OK.
    How to do it...
  9. Right-click on If: $inbou... and select Insert Into | Reporting | Log to add a Log action into the branch.
  10. Navigate to the Properties of the Log action, click <Expression> and set the expression to 'HIGH PRIORITY'. Click OK.
  11. Set the Severity drop-down listbox to Warning.
  12. Right-click on the Log action just created and select Copy.
  13. Right-click on the Else branch and select Paste to paste the Log action into the else branch.
  14. Change the expression 'HIGH PRIORITY' to 'LOW PRIORITY'.
    How to do it...
  15. Deploy the proxy service to the OSB server.

    Now it's time to test the changes to the proxy service.

  16. Send a message with JMSPriority set to 6 and a HIGH PRIORTY log message should show up on the console window.
  17. Send a message with JMSPriority set to 3 and a LOW PRIORITY log message should show up on the console window.

How it works...

In the incoming message in the proxy service, the transport-specific header and property values are available through the inbound context variable. Depending on the transport configured on the proxy service, the inbound variable holds different values in the request and response inside the transport node. In the case of JMS, we can find the different JMS headers, such as JMSPriority in the request node under the transport node (in the inbound variable).

By using XPath, these values can be accessed and used for the expressions of the OSB actions, such as If Then or Routing Table, for example, to implement a routing based on JMS header values.

There's more...

Here, we will learn how we can access user-defined JMS properties:

Accessing user-defined JMS properties

The user-defined JMS properties are only available if the headers are passed into the proxy service. This feature is disabled by default on a proxy service. To enable it, perform the following steps in OEPE:

  1. Navigate to the Transport tab of the JMSConsumer proxy service.
  2. Change the Get All Headers option to Yes.

    Now the user-defined JMS properties are passed into the proxy service and can be accessed through the inbound context variable. Let's add a Log action to print out the value of the myProperty JMS property:

  3. Navigate to the Message Flow tab.
  4. Add a Log action after the If Then action.
  5. On the Properties tab of the Log action, click on <Expression>.
  6. On the Variable Structures tab on the right, navigate to the user-header node below request - jms | headers.
  7. Drag the name element to the Expression field as shown in the following screenshot:
    Accessing user-defined JMS properties
  8. Extend the expression so that it accesses value attribute of the myProperty user-defined property: $inbound/ctx:transport/ctx:request/tp:headers/tp:user-header[@name = 'myProperty']/@value.
  9. Click OK.
  10. Change the Severity to Warning.
  11. Deploy the project to the OSB server.

Now, retest the project by sending a message to the SourceQueue with a user-defined property myProperty. The value passed should be shown on the console window.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset