0 Replies Latest reply on Apr 19, 2017 10:57 AM by Chintan Parekh

    java.lang.ClassCastException: org.hornetq.jms.client.HornetQMessage cannot be cast to javax.jms.TextMessage

    Chintan Parekh Newbie

      Hi,

       

      Below configuration receives message from _request queue and process it and  send it to notification queue (_response). There is standalone Java listener running which listens on that queue. But when message arrives, it is throwing ClassCastException. Here is Switchyard configuration and route.xml

       

      <?xml version="1.0" encoding="UTF-8"?>
      <switchyard xmlns="urn:switchyard-config:switchyard:1.0" xmlns:swyd="urn:switchyard-config:switchyard:1.0" xmlns:camel="urn:switchyard-component-camel:config:1.0" xmlns:jms="urn:switchyard-component-camel-jms:config:1.0" xmlns:sca="http://docs.oasis-open.org/ns/opencsa/sca/200912">
        <sca:composite name="helloworld_action" targetNamespace="urn:switchyard-quickstart:helloworld_action:0.1.0">
          <sca:service name="SimpleListener" promote="HelloWorld_ActionESB/SimpleListener">
            <jms:binding.jms>
              <jms:queue>hpoes_data_service_request</jms:queue>
              <jms:connectionFactory>#ConnectionFactory</jms:connectionFactory>
            </jms:binding.jms>
          </sca:service>
          <sca:component name="HelloWorld_ActionESB">
            <camel:implementation.camel>
              <camel:xml path="META-INF/route.xml"/>
            </camel:implementation.camel>
            <sca:service name="SimpleListener">
              <interface.esb inputType="java:org.apache.camel.Message" outputType="java:org.apache.camel.Message"/>
            </sca:service>
            <sca:reference name="notificationAction">
              <interface.esb inputType="java:java.lang.String"/>
            </sca:reference>
            <sca:property value="spring_context.xml" name="springContextXML"/>
          </sca:component>
          <sca:reference name="notificationAction" multiplicity="1..1" promote="HelloWorld_ActionESB/notificationAction">
            <jms:binding.jms>
              <jms:queue>hpoes_data_service_response</jms:queue>
              <jms:connectionFactory>#ConnectionFactory</jms:connectionFactory>
              <jms:disableReplyTo>true</jms:disableReplyTo>
            </jms:binding.jms>
          </sca:reference>
        </sca:composite>
        <domain>
          <properties>
            <property name="org.switchyard.handlers.messageTrace.enabled" value="true"/>
          </properties>
        </domain>
      </switchyard>
      

       

      route.xml

      <?xml version="1.0" encoding="UTF-8"?>
      <route xmlns="http://camel.apache.org/schema/spring" id="org.switchyard.quickstarts.helloworld.action.SimpleListener"
          errorHandlerRef="loggingErrorHandler">
          <from uri="switchyard://SimpleListener" />
          <log message="[message] '${body}'" />
          <to uri="bean:genericServiceInvokerAction?method=executeAction" />
           <setHeader headerName="CamelJmsMessageType">
              <constant>JmsMessageType.Text</constant>
          </setHeader>
          <to uri="switchyard://notificationAction" />
      <camelContext xmlns="http://camel.apache.org/schema/spring"/></route>
      

       

      Service bean (genericServiceInvokerAction)

      public Message executeAction(Message message) throws Exception {
        JmsMessage jmsMessage = message.getBody(JmsMessage.class);
        HashMap<String, Serializable> map = jmsMessage.getBody(HashMap.class);
        try {
        // process message from map and create json result
        String splitJsonResult = jsonResult;
        message.setBody(splitJsonResult);
        message.getExchange().getProperties().put(Constants.SERVICE_REPLY_CLASS_FIELD, 
        }
        catch (Throwable ex) {
        logger.error("Error", ex);
        String jsonResult = ex.toString();
        if (ex instanceof InvocationTargetException) {
        ex = ((InvocationTargetException) ex).getTargetException();
        jsonResult = ex.getMessage() == null ? "" : ex.getMessage();
        if (ex instanceof RuntimeException) {
        ex = new RuntimeException(); 
        }
        }
        message.setBody(jsonResult);
        message.getExchange().getProperties().put(Constants.SERVICE_REPLY_CLASS_FIELD, ex.getClass().getName());
        }
        return message;
        }
      

       

      Java listener which is listens on queue: hpoes_data_service_response

      private TextMessage processResponseMessage(QueueSession session,
        InitialContext initialContext, String correlationId)
        throws JMSException, NamingException, ClassNotFoundException,
        NoSuchMethodException, InstantiationException,
        IllegalAccessException, InvocationTargetException, Exception {
        TextMessage replyMessage;
        logger.debug("Waiting for message w/ id " + correlationId);
        MessageConsumer consumer = session.createConsumer((Destination)initialContext.lookup("jms/hpoes_data_service_response"));
         
        replyMessage = (TextMessage) consumer.receive(); //fails here which ClassCastException
        logger.debug("Got message w/ id " + correlationId);
        replyMessage.acknowledge();
        consumer.close();
        String clazz = replyMessage.getStringProperty(Constants.SERVICE_REPLY_CLASS_FIELD);
        if ("boolean".equals(clazz))
        clazz = "java.lang.Boolean";
        if ("int".equals(clazz))
        clazz = "java.lang.Integer";
            
        // exception ?
        if (Exception.class.isAssignableFrom(Class.forName(clazz))) {
        @SuppressWarnings("unchecked")
        Class<Exception> ex = (Class<Exception>) Class.forName(clazz);
        Constructor<Exception> co = (Constructor<Exception>) ex. getConstructor(String.class);
        Exception e = co.newInstance("Service Error. Message was: " + replyMessage.getText());
        throw e;
        }
        return replyMessage;
        }
      

       

      can someone help me to find solution?

       

      thanks

      Chintan