WSO2Con 2013 CFP Banner

How do I make message contexts available to my service impl class?

Q: How do I make message contexts available to my service impl class?

Date: Tue, 30th May, 2006
Level:
Reads: 9583
Discuss this article on Stack Overflow
Eran Chinthaka
Software Engineer
WSO2 Inc.
chinthaka's picture

Applies To:

Apache Axis2/Java 1.0

Apache Axis2/Java 1.1 or later

A: Apache Axis2/Java 1.0 :

There are two ways you can implement this.

1. Add the following method in to your service implementation class.

public void setOperationContext(OperationContext opContext){
................
................
}
2. Implement the org.apache.axis2.Service to your service implementation class. Then by using the setOperationContext() in that you will get access to the message contexts. Thats it!

To access the IN message context :
MessageContext inContext = opContext.getMessageContext(WSDLConstants.MESSAGE_LABEL_IN) 

To access the OUT message context :

MessageContext outContext = opContext.getMessageContext(WSDLConstants.MESSAGE_LABEL_OUT) 

Note:This method will only work if you are using one of in-built message receivers coming with Axis2 distribution, such as RPCMessageReceiver, RawXMLINOutMessageReceiver etc.

A: Apache Axis2/Java 1.1 or above:

For Axis2 1.1 or above use the following from your service implementation class instead of the setOperationContext(..)

To get incoming message context:

MessageContext inContext =
MessageContext.getCurrentMessageContext();

To get outgoing message context:

OperationContext operationContext
=inMessageContext.getOperationContext();

MessageContext outMessageContext =
operationContext.getMessageContext(WSDLConstants.MESSAGE_LABEL_OUT_VALUE);
misfit.physics.gmail.com's picture

cannot get the messagecontext in axis1.5.1

hello, im a newbie. im trying to make the example here work: http://wso2.org/library/406 i used a simple runner to register the ClientService in the ActiveService since im only after the notification part. package sample; public class runner { public static void main(String[] args) { ActiveService obj = new ActiveService(); String strEpr = "http://localhost:8080/axis2/services/ClientService"; obj.register(strEpr); } } however, this line below will ALWAYS return a null MessageContext in the ActiveService.java: MessageContext msgCtx = MessageContext.getCurrentMessageContext(); I've read somewhere (http://thejavamonkey.blogspot.com/2008/04/axis-2-client-side-getting-message.html) to use this: MessageContext msgCtx =_getServiceContext().getCurrentOperationContext().getMessageContext("In") but in the MessageContext API for 1.5.1, this is nowhere to be found. Question: How to you get the MessageContext of the current service IN apache axis 1.5.1? thanks.
teaguersadelle.hotmail.com's picture

did you find a solution? if

did you find a solution? if so, could you pm me please? i am kind o having the same issue. - find me here http://freewordsearchmaker.org/
nielsplatz's picture

Is there a place to find help when this does not work?

I've writen the two latest comments, and would like to know if there is anywhere special that has answers regarding axis2.... any special comunity, that answers fast? With regards :)
nielsplatz's picture

Classpath problems when using axis2 1.4 with Weblogic 8.1 sp6

I've got an issue that only arises, when you need for the axis2.war (deployed exploded), to be part of an existing weblogic application. I've got an existing system running under weblogic that now needs to be exposed thgough web services using axis2. One of our problems are that when we try to call a method through SOAPUI, that uses an object of type MessageContext (org/apache/axis2/context/MessageContext) then we get the error shown later. If I isolate the axis2 application, and deploy it as a separate weblogic application, alongside the old existing application, then no problem arises, but when I include it into it (the old existing application) then the error occures. My suspicion is, that there is a classpath conflict, but I have all the axis2 jar files in the webinf/lib in the axis2.war file, so it shouldn’t be the case. maybe you can tell me, if I’m running into an error that is caused by a bug in axis2 or the communication between axis2 (axis2 1.4) and weblogic (weblogic 8.1 sp6). 2008-11-26 10:02:48,160 ERROR axis2.rpc.receivers.RPCMessageReceiver - org/apache/axis2/context/MessageContext java.lang.reflect.InvocationTargetException at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:324) at org.apache.axis2.rpc.receivers.RPCUtil.invokeServiceClass(RPCUtil.java:194) at org.apache.axis2.rpc.receivers.RPCMessageReceiver.invokeBusinessLogic(RPCMessageReceiver.java:102) at org.apache.axis2.receivers.AbstractInOutMessageReceiver.invokeBusinessLogic(AbstractInOutMessageReceiver.java:40) at org.apache.axis2.receivers.AbstractMessageReceiver.receive(AbstractMessageReceiver.java:100) at org.apache.axis2.engine.AxisEngine.receive(AxisEngine.java:176) at org.apache.axis2.transport.http.HTTPTransportUtils.processHTTPPostRequest(HTTPTransportUtils.java:275) at org.apache.axis2.transport.http.AxisServlet.doPost(AxisServlet.java:131) at javax.servlet.http.HttpServlet.service(HttpServlet.java:760) at javax.servlet.http.HttpServlet.service(HttpServlet.java:853) at weblogic.servlet.internal.ServletStubImpl$ServletInvocationAction.run(ServletStubImpl.java:1077) at weblogic.servlet.internal.ServletStubImpl.invokeServlet(ServletStubImpl.java:465) at weblogic.servlet.internal.ServletStubImpl.invokeServlet(ServletStubImpl.java:348) at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:7047) at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:321) at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:121) at weblogic.servlet.internal.WebAppServletContext.invokeServlet(WebAppSe rvletContext.java:3902) at weblogic.servlet.internal.ServletRequestImpl.execute(ServletRequestImpl.java:2773) at weblogic.kernel.ExecuteThread.execute(ExecuteThread.java:224) at weblogic.kernel.ExecuteThread.run(ExecuteThread.java:183) Caused by: java.lang.NoClassDefFoundError: org/apache/axis2/context/MessageContext at dk.product.flytte.webservices.services.impl.SearchPropertyServiceImpl.searchProperties(SearchPropertyServiceImpl.java:80) ... 23 more 2008-11-26 10:02:48,176 ERROR apache.axis2.engine.AxisEngine - org/apache/axis2/ context/MessageContext org.apache.axis2.AxisFault: org/apache/axis2/context/MessageContext at org.apache.axis2.rpc.receivers.RPCMessageReceiver.invokeBusinessLogic(RPCMessageReceiver.java:158) at org.apache.axis2.receivers.AbstractInOutMessageReceiver.invokeBusinessLogic(AbstractInOutMessageReceiver.java:40) at org.apache.axis2.receivers.AbstractMessageReceiver.receive(AbstractMessageReceiver.java:100) at org.apache.axis2.engine.AxisEngine.receive(AxisEngine.java:176) at org.apache.axis2.transport.http.HTTPTransportUtils.processHTTPPostRequest(HTTPTransportUtils.java:275) at org.apache.axis2.transport.http.AxisServlet.doPost(AxisServlet.java:131) at javax.servlet.http.HttpServlet.service(HttpServlet.java:760) at javax.servlet.http.HttpServlet.service(HttpServlet.java:853) at weblogic.servlet.internal.ServletStubImpl$ServletInvocationAction.run(ServletStubImpl.java:1077) at weblogic.servlet.internal.ServletStubImpl.invokeServlet(ServletStubImpl.java:465) at weblogic.servlet.internal.ServletStubImpl.invokeServlet(ServletStubImpl.java:348) at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:7047) at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:321) at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:121) at weblogic.servlet.internal.WebAppServletContext.invokeServlet(WebAppServletContext.java:3902) at weblogic.servlet.internal.ServletRequestImpl.execute(ServletRequestImpl.java:2773) at weblogic.kernel.ExecuteThread.execute(ExecuteThread.java:224) at weblogic.kernel.ExecuteThread.run(ExecuteThread.java:183) Caused by: java.lang.reflect.InvocationTargetException at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:324) at org.apache.axis2.rpc.receivers.RPCUtil.invokeServiceClass(RPCUtil.java:194) at org.apache.axis2.rpc.receivers.RPCMessageReceiver.invokeBusinessLogic(RPCMessageReceiver.java:102) ... 17 more Caused by: java.lang.NoClassDefFoundError: org/apache/axis2/context/MessageContext at dk.product.flytte.webservices.services.impl.SearchPropertyServiceImpl.searchProperties(SearchPropertyServiceImpl.java:80) ... 23 more
nielsplatz's picture

Elaborating the issue

I found more on this issue, that there is a classloader problem, and as described above, there is two situations 1) axis2.war is run outside our existing application, and here the classpath looks like: classpath = weblogic.utils.classloaders.ChangeAwareClassLoader@fab5b1( finder: weblogic.utils.classloaders.CodeGenClassFinder@e84763, annotation: axis2@services, parent: weblogic.utils.classloaders.GenericClassLoader@1aea727( finder: weblogic.utils.classloaders.CodeGenClassFinder@1a6c368, annotation: axis2@, parent: weblogic.utils.classloaders.GenericClassLoader@1e13a2c( finder: weblogic.utils.classloaders.CodeGenClassFinder@1670cc6, annotation: ApplicationClassLoader@, parent: weblogic.utils.classloaders.GenericClassLoader@29d75( finder: weblogic.utils.classloaders.CodeGenClassFinder@77b794, annotation: null, parent: sun.misc.Launcher$AppClassLoader@53ba3d)))) 2) the axis2.war is deployed as part of the existing application on weblogic, resulting in another very different classpath: classpath = weblogic.utils.classloaders.GenericClassLoader@11ce4fe( finder: weblogic.utils.classloaders.CodeGenClassFinder@16a2c7b, annotation: xxy@, parent: weblogic.utils.classloaders.GenericClassLoader@1285252( finder: weblogic.utils.classloaders.CodeGenClassFinder@18e5ebd, annotation: ApplicationClassLoader@, parent: weblogic.utils.classloaders.GenericClassLoader@177fa9a( finder: weblogic.utils.classloaders.CodeGenClassFinder@1edcd24, annotation: null, parent: sun.misc.Launcher$AppClassLoader@53ba3d))) Hope that you will respond on this issue With regards Niels
roeland maes's picture

Is 'MessageContext.getCurrentMessageContext()' synchronized?

If the Message Receiver receives two Requests at the same time, will the method MessageContext.getCurrentMessageContext() always return the correct MessageContext in the Web Service logic implementation? I can 't see whether the invoke-logic method is sychronized.
afkham_azeez's picture

Yes it will

MessageContext.getCurrentMessageContext() will always return the correct MessageContext. The current message context is held within a ThreadLocal object. Regards Afkham Azeez
chrisahn's picture

when is setOperationContext() called?

In terms of axis2 1.0 is setOperationContext() called for every invocation of a Web service method, or once the service object is instantiated? I guess the latter is most appropriate, since otherwise this wouldn't be thread-safe for application scoped service objects. Am I correct?
ajith's picture

when the setOperationContext() called

The setOperationContext() method is called just before the service objects business logic gets invoked. When and how often the service object gets initialized depends on the scope of the service and the init method is called only when the service object initializes. As for thread safety, if you implement the setOperationContext() method in a service class having 'application scope', then you should be managing the thread synchronization by your self! Have a look at the getTheImplementationObject(..) method and the configureBusinessLogicProvider(...) method of the org.apache.axis2.receivers.AbstractMessageReceiver and org.apache.axis2.engine.DependencyManager respectively that lives in the axis2-core module.