In an earlier post I promised an example of where service integration bus mediations still have a role to play in a WebSphere ESB environment. WebSphere ESB currently only supports dynamic endpoints on an import or callout (those that can be changed at runtime by a mediation flow) for SCA and web service bindings. In this post I’ll show how a service integration bus mediation can be used to add dynamic behaviour to a JMS binding.
Before continuing, I should say that there is more than one way to implement this behaviour. You could, for example, remove the import completely and replace it with a Java component that simply makes the appropriate JMS calls to send the message to a destination selected dynamically. I prefer the approach outlined here though as it means you maximise the use of the normal WebSphere ESB functionality such as serializing the business object in to the JMS message.
At a high level, what I’m going to suggest is that you use a regular JMS binding defined to use a static destination and then you apply a service integration bus mediation to that destination to perform the dynamic routing. The first issue to resolve is how the mediation module indicates to the service integration bus mediation where the message should be routed. I’m going to use a JMS user property to pass the destination name. The following shows how a custom Java primitive can be used to set a JMS property via the service message object header properties.
smo.getHeaders().setJMSHeader(
ServiceMessageObjectFactory.eINSTANCE.createJMSHeaderType());
List properties = smo.getHeaders().getProperties();
PropertyType property =
ServiceMessageObjectFactory.eINSTANCE.createPropertyType();
property.setName("DestinationName");
property.setValue(smo.getString("/body/operation/in/destination"));
property.setPropertyType("java.lang.String");
properties.add(property);In this example, the destination name is pulled directly out the body of the request but you might, for example, obtain it from a registry lookup. Note that I explicitly create the JMS header although I don’t appear to use it. This is important as it is the presence of this header that causes the header properties to be written out as JMS properties in the message.
I won’t go in to the details of creating the service integration bus mediation - you can read all about that in Dan Murphy’s series of developerWorks articles. The important part is the handler code that retrieves the JMS user property from the message and uses it to reroute the message:
SIMessageContext siMessageContext = (SIMessageContext) messageContext;
SIMessage message = siMessageContext.getSIMessage();
String destinationName =
(String) message.getUserProperty("DestinationName");
SIDestinationAddress destination = SIDestinationAddressFactory
.getInstance().createSIDestinationAddress(destinationName, true);
List forwardRoutingPath = message.getForwardRoutingPath();
forwardRoutingPath.add(destination);
message.setForwardRoutingPath(forwardRoutingPath);The only real thing to note here is that the forward routing path returned from the getter method is a copy of the list so that you need to set it back again once you have added the new destination.
For the simple case, that’s all there is to it. The particular scenario I was interested in though was using publish/subscribe and it was the topic that I wanted to dynamically route to, not the destination. I used the same approach as outlined above, passing the topic name as a JMS user property instead. The code in the mediation handler then looked as follows:
SIMessageContext siMessageContext = (SIMessageContext) messageContext;
SIMessage message = siMessageContext.getSIMessage();
String topicName = (String) message.getUserProperty("TopicName");
message.setDiscriminator(topicName);The discriminator is what the service integration bus programming model calls the topic name within a topic space. I had the mediation module send the request to a particular topic within the required topic space and then, when applying the service integration bus mediation to that topic space, I specified that topic name as the discriminator. This ensured that the mediation is only driven when a message arrives at that particular topic.
It was at this point that I realized a flaw in this approach. The destination name contained in the JMS message is not altered by the modifications made in the mediation handler. Generally this isn’t a problem as the receiving client either doesn’t care or knows what destination it connected to in order to receive the message. However, in my case the consumer is using a topic wildcard to receive messages and it is important that it knows what topic the message was actually received on as it is then going to republish them to the corresponding topic in another publish/subscribe system. (For the curious, the client is the JMS bridge in MicroBroker.)
At this point I threw away my service integration bus mediation and wrote a message-driven bean to receive the messages from the mediation module and republish them to the required topic. The JMS destination for the send was simply created using Session.createTopic(). In my case I just needed to pass this the topic name but, if you need to redirect to something other than the default topic space, then you need to be aware of the URI syntax outlined in the InfoCenter. Note that, in a point-to-point scenario, this approach might not be appropriate as it results in a change in the message identifier. In publish/subscribe the publisher and subscriber get different identifiers anyway.