8 Replies Latest reply on Aug 26, 2011 6:25 AM by jamie archibald

    Type converter throws exception for null body

    jamie archibald Apprentice

      I have a client that sends a request to a service using activemq.

       

      class Service {

       

        configure() {

         from("activemq:queue:request")

         .bean(this, "getMyObject");

        }

           

         public MyObject getMyObject() {

             // do a database query and look for the object, return null if we can't find the object

             return null;

         }

      }

       

      class MyObject implements Serializable {

       

      }

       

      MyObject result = template.requestBody("activemq:queue:request?jmsMessageType=Object", new MyObject(), MyObject.class);

       

      I receive this exception No type converter available to convert from type: null to the required type: java.io.Serializable with value null.

       

      This used to work when jmsMessageType=Text (as my class is generated using jaxb) however I recently switched over to use Object instead for performance.

        • 1. Re: Type converter throws exception for null body
          Claus Ibsen Master

          Can you post the stacktrace? And what version of Camel are you using?

          • 2. Re: Type converter throws exception for null body
            jamie archibald Apprentice

            Here's the stack trace from my unit test.  Camel 2.8.0

             

             

            org.apache.camel.RuntimeCamelException: org.springframework.jms.MessageFormatException: No type converter available to convert from type: null to the required type: java.io.Serializable with value null; nested exception is javax.jms.MessageFormatException: No type converter available to convert from type: null to the required type: java.io.Serializable with value null

                 at org.apache.camel.util.ObjectHelper.wrapRuntimeCamelException(ObjectHelper.java:1145)[camel-core-2.8.0.jar:2.8.0]

                 at org.apache.camel.component.jms.EndpointMessageListener.onMessage(EndpointMessageListener.java:143)[camel-jms-2.7.0.jar:2.7.0]

                 at org.springframework.jms.listener.AbstractMessageListenerContainer.doInvokeListener(AbstractMessageListenerContainer.java:560)[spring-jms-3.0.5.RELEASE.jar:3.0.5.RELEASE]

                 at org.springframework.jms.listener.AbstractMessageListenerContainer.invokeListener(AbstractMessageListenerContainer.java:498)[spring-jms-3.0.5.RELEASE.jar:3.0.5.RELEASE]

                 at org.springframework.jms.listener.AbstractMessageListenerContainer.doExecuteListener(AbstractMessageListenerContainer.java:467)[spring-jms-3.0.5.RELEASE.jar:3.0.5.RELEASE]

                 at org.springframework.jms.listener.AbstractPollingMessageListenerContainer.doReceiveAndExecute(AbstractPollingMessageListenerContainer.java:325)[spring-jms-3.0.5.RELEASE.jar:3.0.5.RELEASE]

                 at org.springframework.jms.listener.AbstractPollingMessageListenerContainer.receiveAndExecute(AbstractPollingMessageListenerContainer.java:263)[spring-jms-3.0.5.RELEASE.jar:3.0.5.RELEASE]

                 at org.springframework.jms.listener.DefaultMessageListenerContainer$AsyncMessageListenerInvoker.invokeListener(DefaultMessageListenerContainer.java:1058)[spring-jms-3.0.5.RELEASE.jar:3.0.5.RELEASE]

                 at org.springframework.jms.listener.DefaultMessageListenerContainer$AsyncMessageListenerInvoker.executeOngoingLoop(DefaultMessageListenerContainer.java:1050)[spring-jms-3.0.5.RELEASE.jar:3.0.5.RELEASE]

                 at org.springframework.jms.listener.DefaultMessageListenerContainer$AsyncMessageListenerInvoker.run(DefaultMessageListenerContainer.java:947)[spring-jms-3.0.5.RELEASE.jar:3.0.5.RELEASE]

                 at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)[:1.6.0_26]

                 at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)[:1.6.0_26]

                 at java.lang.Thread.run(Thread.java:662)[:1.6.0_26]

            • 3. Re: Type converter throws exception for null body
              Claus Ibsen Master

              He you have a mix of Camel versions. camel-core 2.8.0 and camel-jms 2.7.0

               

              When you do request/reply over JMS with JMS objects, the Camel expect that there is a reply message that can be converted to Serializable. To be send back as the reply.

               

              To indicate an "empty" response you may consider sending back a MyObject having some state that its a "empty" response.

               

              What is your use case? What would you expect to happen when there is no reply body to send back?

              • 4. Re: Type converter throws exception for null body
                jamie archibald Apprentice

                I checked by classpath and it looks like I am using camel-jms 2.7.0, not sure why maven did this. I just explicitly set camel-jms 2.8.0.

                 

                I don't think it would be best practise to introduce a flag into all my objects on whether it exists or not. Ideally I would just return null.

                 

                Here is an example of why I want to return null. If it can't be done I can probably just switch back to using jmsMessageType=Text for now:

                 

                class MyServiceImpl implements IMyService {

                 

                   configure() {

                     from("activemq:queue:request")

                     .bean(this, "getMyObject")

                   }

                 

                   MyObject getMyObject(@Body String name) {

                      // look in database for the object, if it doesnt exist return null

                   }

                }

                 

                 

                class MyClient implements IMyClient {

                     MyObject getMyObject(String name) {

                       return template.requestBodyAndHeader("activemq:queue:request", name, MyObject.class)

                    }

                }

                 

                 

                public static void main() {

                    // our client that interacts with the service via activemq

                    // the service is deployed somewhere on the network

                 

                   MyClient client = new MyClient();

                   MyObject obj = client.getMyObject("Me");

                   if (obj == null) {

                       // do something

                   }

                }

                • 5. Re: Type converter throws exception for null body
                  Willem Jiang Master

                  I think the camel-jms 2.7.0 dependency is introduced by activemq-camel module. you need to specify the dependency of camel-jms 2.8.0 in your pom to override it.

                  • 6. Re: Type converter throws exception for null body
                    Claus Ibsen Master

                    Hi

                     

                    Keep an eye on this ticket

                    https://issues.apache.org/jira/browse/CAMEL-3354

                     

                    To support null return values from beans.

                    • 7. Re: Type converter throws exception for null body
                      Claus Ibsen Master

                      Beans already support returning null values.

                       

                      I created a ticket for the JMS req/reply with null values.

                      https://issues.apache.org/jira/browse/CAMEL-4384

                       

                      It works fine on trunk.

                      • 8. Re: Type converter throws exception for null body
                        jamie archibald Apprentice

                        Excellent thanks!

                         

                        For now I will just use jmsMessageType=Text and switch to Object when the bug has been resolved.

                         

                        Much thanks