O'Reilly logo

Java SOA Cookbook by Eben Hewitt

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Sharing Data Between Handler Invocations

Problem

You want to share data between invocations of your handlers within a chain during processing, but handler instances cannot rely on thread-local state.

Solution

Obtain the message context from the parameter to your handler, and add data to it. There are predefined properties you can set and access later, or you can add your own. These properties are exposed as a map.

The following code illustrates using a custom property and a built-in property with the message context to share data between two handlers defined on the same service.

Your test client adds the handler resolver to the service before getting the port:

@Test
public void testMessageContext(){
    HelloWSService service = new HelloWSService();

    //Set handler resolver into service.
    service.setHandlerResolver(new MultipleHandlerResolver());

    // Get the Hello stub
    Hello hello = service.getHelloWSPort();
        
    //Invoke the business method
    String result = hello.sayHello(name);
        
    assertEquals("Hello, " + name + "!", result);
.
}

This is a new handler resolver that defines more than one handler:

public class MultipleHandlerResolver implements HandlerResolver {
    private static final Logger LOGGER = 
            Logger.getLogger(MultipleHandlerResolver.class);
    
    private final List<Handler> chain;
    
    //constructor. we'll set up chain here.
    public MultipleHandlerResolver() { 
        chain = new ArrayList<Handler>();
        chain.add(new SetVersionHandler());
        chain.add(new VersionInstructionsHandler());
    }
//...

This resolver defines one handler ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required