Skip to main content

Accessing the MessageContext from service code?

6 replies [Last post]
michael_watson
Offline
Joined: 2006-08-10

The MessageContext can be used to pass information between handlers, however is there a way to access the MessageContext from within the service code so that information can be passed from a handler to the service code?

In a more general sense, imagine that there is some information contained in a header element that the service would like to log. Is there a way to get a handler to process the header element and then make the processed information available to the service?

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
jitu
Offline
Joined: 2003-06-14

Yes, you can access it using WebServiceContext. When the handler is setting any property, make sure it sets with APPLICATION scope property.

@WebService
public class HelloImpl {
@Resource
WebServiceContext wsCtxt;

@WebMethod
public void method() {
MessageContext msgCtxt = wsCtxt.getMessageContext();
}

}

michael_watson
Offline
Joined: 2006-08-10

Thanks a lot Jitu :-)

- Michael

jzacker
Offline
Joined: 2004-12-06

Is this practice threadsafe?

kohsuke
Offline
Joined: 2003-06-09

Depending on the calling thread WebServiceContext class returns the right object.

jzacker
Offline
Joined: 2004-12-06

If the calling thread is always from the servlet, is it good practice to use the injected resource? It seems like using this attribute without some sort of synchronization on it might cause problems. But if the servlet invoking the methods keeps track of that, then my question is moot.

kohsuke
Offline
Joined: 2003-06-09

I guess I don't understand your concern.

Jitu's code is correct, and JAX-WS does the right thing (TM)