LibraryToggle FramesPrintFeedback

Using a Custom Destination Chooser

Overview

Provider endpoints use a destination chooser to determine the JMS destination on which to send requests and receive replies. They have a default destination chooser that queries the message exchange for a property that specifies the destination to use. Consumer endpoints use destination choosers to determine where to send reply messages. In both cases, the destination chooser is the first method employed by an endpoint when looking for a JMS destination. If the destination chooser returns a destination, or a destination name, the endpoint will use the returned value.

To customize the logic used in choosing a destination, you can provide an implementation of the org.apache.servicemix.jms.endpoints.DestinationChooser interface and configure the endpoint to load it. The configured destination chooser will be used in place of the default destination chooser.

Implementing a destination chooser

Destination choosers implement the org.apache.servicemix.jms.endpoints.DestinationChooser interface. This interface has a single method: chooseDestination().

chooseDestination(), whose signature is shown in Example 37, takes the JBI message exchange and a copy of the message. It returns either a JMS Destination object or a string representing the destination name.

[Note]Note

If the destination chooser returns a string, the endpoint will use a destination resolver to convert the string into a JMS destination. See Using a Custom Destination Resolver.

Example 37. Destination Chooser Method

Object chooseDestination(MessageExchange exchange,
                         Object message);

The message parameter can be either of the following type of object:

  • javax.jbi.messaging.NormalizedMessage

  • javax.jbi.messaging.Fault

  • Exception

Example 38 shows a simple destination chooser implementation. It checks the message for a property that represents the JMS destination on which the request is to be placed.

Example 38. Simple Destination Chooser

package com.widgetVendor.example;

import package org.apache.servicemix.jms.endpoints.DestinationChooser;
import javax.jbi.messaging.MessageExchange;
import javax.jbi.messaging.NormalizedMessage;
import javax.jms.Destination;

public class widgetDestinationChooser implements DestinationChooser {

    public static final String DESTINATION_KEY = "org.apache.servicemix.jms.destination";

    public SimpleDestinationChooser() {
    }

    public Object chooseDestination(MessageExchange exchange, Object message) {
        Object property = null;
        if (message instanceof NormalizedMessage) {
            property = ((NormalizedMessage) message).getProperty(DESTINATION_KEY);
        }
        if (property instanceof Destination) {
            return (Destination) property;
        }
        if (property instanceof String) {
            return (String) property;
        }
        return new String("widgetDest");
    }
}

Configuring an endpoint to use a destination chooser

You can configure an endpoint to use a custom destination chooser in one of two ways. The recommended way is to configure the destination chooser as a bean and have the endpoint reference the destination chooser's bean. The other way is to explicitly include the destination chooser's configuration as a child of the endpoint.

As shown in Example 39, configuring an endpoint's destination chooser using a bean reference is a two step process:

  1. Configure a bean element for your destination chooser.

  2. Add a destinationChooser attribute that references the destination chooser's bean to your endpoint.

Example 39. Configuring a Destination Chooser with a Bean Reference

<beans xmlns:jms="http://servicemix.apache.org/jms/1.0"
       ... >
  ...
  <jms:provider service="my:widgetService"
                endpoint="jbiWidget"
                destinationName="widgetQueue"
                connectionFactory="#connectionFactory"
                destinationChooser="#widgetDestinationChooser" />
  <bean id="widgetDestinationChooser"
        class="com.widgetVendor.example.widgetDestinationChooser" />
  ...
</beans>

Example 40 shows an example configuration using the jms:destinationChooser element. This method is less flexible than the recommended method because other endpoints cannot reuse the destination chooser's configuration.

Example 40. Explicitly Configuring a Destination Chooser

<beans xmlns:jms="http://servicemix.apache.org/jms/1.0"
       ... >
  ...
  <jms:provider service="my:widgetService"
                endpoint="jbiWidget"
                destinationName="widgetQueue"
                connectionFactory="#connectionFactory">
    <jms:destinationChooser>
      <bean id="widgetDestinationChooser"
            class="com.widgetVendor.example.widgetDestinationChooser" />
    </jms:destinationChooser>
  </jms:provider>
  ...
</beans>

Comments powered by Disqus