Monday, June 22, 2009

JAX-WS handler usage for clients

See this page for a Sun Tech Tip on how to get Handlers working for client code. Getting it working on the web service producer side seemed to go fine, but for the client side (e.g. logging requests and responses for a web service client embedded in your web application), I have had some problems. The Tech Tip referred to gives three ways of doing it: 1) add a @HandlerChain annotation after your @WebServiceClient,
2) add a HandlerResolver, or
3) programatically add the Handler at runtime.
I've so far only had any joy with the last option, by writing code to add a handler once I've actually got myself an instance of the SEI/port. E.g.:

SynchServiceSoap port = synchService.getPort(SynchServiceSoap.class);
//Log the messages via a Handler...

//should get the current handler chain and then add the new handler to it,
//but in this case it's the only one...
LoggingHandler loggingHandler = new LoggingHandler();
List<Handler> handlerChain = new ArrayList<Handler>();
handlerChain.add(loggingHandler);
((BindingProvider)port).getBinding().setHandlerChain(handlerChain);

This works fine. I also tried to use the <jaxws:binding> configuration file option and then running wsimport, expecting wsimport to add @HandlerChain annotations to the generated service class, but it didn't.

I have tried out the "basic" option, which is to annotate with @HandlerChain after the @WebServiceClient, but it's been quite tricky to get right. This is because, if you're not careful, every time you run wsimport it will recreate the service classes and your annotation will be lost. If you bear this in mind, and ensure the annotation is preserved, it works OK.

Labels:

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

Links to this post:

Create a Link

<< Home