title: How to receive object using Spring jmsTemplate
body: <p>Here's my use case: </p>
  
  <ol>
  <li><p>RESTFul calls a service to create a message in a jms queue using jmsTemplate:</p>
  
  <pre><code>@Autowired
  JmsTemplate jmsTemplate;
  public void sendMessage(final ApplicationContext appContext) {
      jmsTemplate.send(new MessageCreator() {
          @Override
          public Message createMessage(Session session) throws JMSException {
          ObjectMessage objectMessage = session.createObjectMessage(appContext);
          return objectMessage;
      }
  });
  }
  </code></pre></li>
  <li><p>An other RESTFul calls a service to get the message from the queue using:   </p>
  
  <pre><code>public ApplicationContext getMessage(final ApplicationContext appContext) {
      Object wObj = jmsTemplate.receive(MessagingConfiguration.NAMED_QUEUE);
      return (ApplicationContext)wObj;
  }
  </code></pre></li>
  </ol>
  
  <p>As you can read, it is quite simple. I know I can use asynchronuous listeners with @JmsListener annotation but I need to synchronise the receive action. When I try this code, I get a class cast exception (which makes sens). My question: how do I convert the message (ActiveMQObjectMessage) to my POJO (ApplicationContext)?</p>
  
  <p>Stacktrace:</p>
  
  <blockquote>
    <p>SEVERE: Servlet.service() for servlet [dispatcher] in context with path [/ApplicationContextManager] threw exception [Request processing failed; nested exception is java.lang.ClassCastException: org.apache.activemq.command.ActiveMQObjectMessage cannot be cast to com.tigidou.commons.appcontext.model.ApplicationContext] with root cause
    java.lang.ClassCastException: org.apache.activemq.command.ActiveMQObjectMessage cannot be cast to com.tigidou.commons.appcontext.model.ApplicationContext</p>
  </blockquote>