Asynchronous ServiceClient invocation exception in AXIS2

Following error may disguise the actual problem in AXIS2 implementation which starts as –

 

   1:  java.lang.ClassCastException: org.apache.axis2.receivers.RawXMLINOnlyMessageReceiver cannot be cast to org.apache.axis2.engine.MessageReceiver
   2:  at org.apache.axis2.deployment.DescriptionBuilder.loadMessageReceiver(DescriptionBuilder.java:190)

 

In most of the situations, this error comes up when you think everything is fine and it should work. You should be able to transmit message from one end to other without any problem and it actually happens.

You send something from end 1 to end 2, end 2 receives it, processes it and then end 2 tries to send something back to end 1, but then, this error appears.

 

The problem isn’t in the implementation but in the the usage. Generally we try to invoke asynchronous behavior out of synchronous web-service structure. Let us do the post-mortem of above example –

 

Scenario 1

  • end 1 sends abc and wait for return of message-flow
  • end 2 receives abc
  • end 2 processes abc
  • end 2 tries to send xyz to end 1 (than returning the value for message-flow abc)
  • end 1 which is waiting for abc message-flow return value, receives xyz which isn’t expected and hence it throws ClassCastException

 

And the ideal flow should be –

Scenario 2

  • end 1 sends abc and wait for return of message-flow
  • end 2 receives abc
  • end 2 processes abc
  • end 2 sends return value for abc message flow
  • end 1 receives and process return value for abc message flow
  • end 2 tries to send xyz to end 1
  • end 1 receives xyz…(and the flow continues)

 

This is still an open issue over AXIS2 website (https://issues.apache.org/jira/browse/AXIS2-2972?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel).

 

If you don’t want want to proceed in this (synchronous) way, then you can implement asynchronous web-services using AXIS2 (which isn’t in scope of this topic – so wouldn’t be explaining here) but if you can’t change the existing synchronous web-service and have access to only the code which invokes the web-service plus flow can also not be changed as explained in scenario 1, then there is one temporary solution –

Maintain the pool of Stub or ServiceClient instances from start of server and use another Stub instance when starting a new message flow

Take it as a temporary work-around as it isn’t the practical solution or suggested solution for running a web-service in production environment and try to convert Scenario 1 into Scenario 2 for your application or synchronous web-service into asynchronous web-service as soon as it can be done.

 

Here is the complete stacktrace of exception for which solution is explained above –

java.lang.ClassCastException: org.apache.axis2.receivers.RawXMLINOnlyMessageReceiver cannot be cast to org.apache.axis2.engine.MessageReceiver
at org.apache.axis2.deployment.DescriptionBuilder.loadMessageReceiver(DescriptionBuilder.java:190)
at org.apache.axis2.deployment.DescriptionBuilder$1.run(DescriptionBuilder.java:142)
at org.apache.axis2.java.security.AccessController.doPrivileged(AccessController.java:130)
at org.apache.axis2.deployment.DescriptionBuilder.processMessageReceivers(DescriptionBuilder.java:138)
at org.apache.axis2.deployment.AxisConfigBuilder.populateConfig(AxisConfigBuilder.java:92)
at org.apache.axis2.deployment.DeploymentEngine.populateAxisConfiguration(DeploymentEngine.java:640)
at org.apache.axis2.deployment.FileSystemConfigurator.getAxisConfiguration(FileSystemConfigurator.java:105)
at org.apache.axis2.context.ConfigurationContextFactory.createConfigurationContext(ConfigurationContextFactory.java:60)
at org.apache.axis2.context.ConfigurationContextFactory.createConfigurationContextFromFileSystem(ConfigurationContextFactory.java:174)
at org.apache.axis2.client.ServiceClient.initializeTransports(ServiceClient.java:211)
at org.apache.axis2.client.ServiceClient.configureServiceClient(ServiceClient.java:138)
at org.apache.axis2.client.ServiceClient.<init>(ServiceClient.java:133)
at org.apache.axis2.client.ServiceClient.<init>(ServiceClient.java:229)
at com.test.client.BasicClient.<init>(BasicClient.java:76)
at com.test.client.TestClient.<init>(TestClient.java:31)
at com.test.client.servlet.TestServer.<init>(TestServer.java:452)
at com.test.client.TestServer.getInstance(TestServer.java:65)
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:597)
at com.test.client.ClientMessageHandler.init(ClientMessageHandler.java:51)
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:597)
at org.apache.axis2.engine.DependencyManager.initServiceClass(DependencyManager.java:56)
at org.apache.axis2.engine.DependencyManager.initService(DependencyManager.java:94)
at org.apache.axis2.context.ConfigurationContextFactory.initApplicationScopeServices(ConfigurationContextFactory.java:91)
at org.apache.axis2.context.ConfigurationContextFactory.createConfigurationContext(ConfigurationContextFactory.java:76)
at org.apache.axis2.transport.http.AxisServlet.initConfigContext(AxisServlet.java:486)
at org.apache.axis2.transport.http.AxisServlet.init(AxisServlet.java:406)

 

Thanks to Anand Mandil for suggesting the last temporary work-out. It proves very handy when you are in do-or-die position.

One thought on “Asynchronous ServiceClient invocation exception in AXIS2”

  1. Copy the Stubs in the same war file as you are deploying the application, because if you will compile the stubs in EJB module then it will be loaded by different class loader. As we know application server deploy the EAR and WAR in EJB and Servlet container respectively.
    Kindly let me know if u required any more help regarding the same.

    Kind Regards,
    Dipak Kumar
    Sr Software Engineer
    Birla Group

Leave a Reply to Diapk Kumar Cancel reply

Your email address will not be published.