Skip to end of metadata
Go to start of metadata

One thing you'll often need to do when developing services, is develop a new version while keeping the old version running. This is but one instance where you may want a service router of some sort. This guide shows how to develop a Handler which will read in a version header, and direct it to the appropriate service.

Handlers are just interceptors which are able to be invoked before an after an endpoint is invoked. In this case where going to develop a handler which sits at the in flow. Handlers can participate in various "phases" of the message pipeline. In this case we're going to write a handler which is part of the pre-dispatch phase, meaning that it'll be invoked before we dispatch to our service.

Lets see the code:

An error occurred: http://svn.xfire.codehaus.org/viewrep/~raw,r=HEAD/xfire/trunk/xfire/xfire-core/src/test/org/codehaus/xfire/examples/router/ServiceRouterHandler.java. The system administrator has been notified.

Just a couple notes on something that should be pretty self explanatory:

  1. getPhase() tells XFire which phase we're participating in..
  2. We throw XFireFaults when we get unexpected values to help the user out.
  3. We are using the ServiceRegistry to resolve the service we want to invoke, then setting it in on the MessageContext so XFire knows where to send the message to.

Now lets look at a bit of set up. Here is a simple JUnit test.

An error occurred: http://svn.xfire.codehaus.org/viewrep/~raw,r=HEAD/xfire/trunk/xfire/xfire-core/src/test/org/codehaus/xfire/examples/router/ServiceRouterTest.java. The system administrator has been notified.

Lets see whats going on here. First, we're creating a ServiceRouter service. We create this with just an empty interface. Technically this isn't needed in the test, but we need to establish an service for certain transports like HTTP where XFire expects a service name in the URL.

Secondly, we're creating two versions of the Echo service in different namespaces. Then we register our three services. Finally, we add our ServiceRouterHandler to the list of global in handlers to be run.

The testInvoke() method just tries out our handler for a spin. We send two different documents. Echo1.xml and Echo2.xml. Via simple XPath assertions we're able to tell we got the correct document back!

Labels: