Announcement Announcement Module
No announcement yet.
JMS Message - Spring integration Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • JMS Message - Spring integration


    I was trying to send messages from one queue to another queue. To achieve this functionality, I have done following configuration.

    <jee:jndi-lookup jndi-name="ConnectionFactory" id="connectionFactory"
    environment-ref="jndiProps" expected-type="javax.jms.ConnectionFactory" />
    <jee:jndi-lookup jndi-name="queue/A" id="jBossQueue"
    environment-ref="jndiProps" expected-type="javax.jms.Queue" />
    <jee:jndi-lookup jndi-name="queue/B" id="jBossQueueTarget"
    environment-ref="jndiProps" expected-type="javax.jms.Queue" />
    <util:properties id="jndiProps" location=""/>
    <jms:channel id="jmsIn" queue="jBossQueue"/>
    <jms:channel id="jmsOut" queue="jBossQueueTarget"/>
    <integration:bridge output-channel="jmsOut" input-channel="jmsIn" />
    I am sending message using sample servlet code as
    Queue queue = (Queue)ctx.lookup("queue/A");
    QueueSession queueSession = queueCon.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
    QueueSender queueSender = queueSession.createSender(queue);
    TextMessage textMessage = queueSession.createTextMessage();
    textMessage.setText("A critical Message.");
    I have created a listener to listen on "jBossQueueTarget". Following is the code snippet
       TextMessage textMessage = (TextMessage)message;
       System.out.println("Message received: " + textMessage.getText());
    }catch(Exception e)
      System.out.println("Exception occurred while handling message.");
    But I am getting classcastexception in above code.
    Can someone tell me where i am making mistake or is there any way to achieve above functionality.

  • #2
    print the stack trace of the exception in your catch block and post it.


    • #3
      Indeed. It's necessary to see exactly where the ClassCastException occurs before we'd be able to address the problem.


      • #4
        Exception while sending message

        I dont know what happenned suddenly. Using same code, I am getting different exception at this time.

        23:31:06,399 WARN  [DefaultMessageListenerContainer] Execution of JMS message listener failed, and no ErrorHandler has been set.
        org.springframework.integration.core.MessagingException: failed to handle incoming JMS Message
        	at org.springframework.integration.jms.JmsDestinationBackedMessageChannel.onMessage(
        	at org.springframework.jms.listener.AbstractMessageListenerContainer.doInvokeListener(
        	at org.springframework.jms.listener.AbstractMessageListenerContainer.invokeListener(
        	at org.springframework.jms.listener.AbstractMessageListenerContainer.doExecuteListener(
        	at org.springframework.jms.listener.AbstractPollingMessageListenerContainer.doReceiveAndExecute(
        	at org.springframework.jms.listener.AbstractPollingMessageListenerContainer.receiveAndExecute(
        	at org.springframework.jms.listener.DefaultMessageListenerContainer$AsyncMessageListenerInvoker.invokeListener(
        	at org.springframework.jms.listener.DefaultMessageListenerContainer$AsyncMessageListenerInvoker.executeOngoingLoop(
        	at org.springframework.jms.listener.DefaultMessageListenerContainer$
        	at Source)
        Caused by: org.springframework.integration.message.MessageDeliveryException: Dispatcher has no subscribers.
        	at org.springframework.integration.dispatcher.UnicastingDispatcher.doDispatch(
        	at org.springframework.integration.dispatcher.UnicastingDispatcher.dispatch(
        	at org.springframework.integration.jms.JmsDestinationBackedMessageChannel.onMessage(
        	... 9 more


        • #5
          Message Exception - class cast

          I dont know whats going on, I restarted JBoss again and now i am getting following exception

          23:57:57,696 ERROR [STDERR] java.lang.ClassCastException: cannot be cast to javax.jms.TextMessage
          23:57:57,696 ERROR [STDERR] 	at
          23:57:57,696 ERROR [STDERR] 	at org.springframework.jms.listener.AbstractMessageListenerContainer.doInvokeListener(
          23:57:57,696 ERROR [STDERR] 	at org.springframework.jms.listener.AbstractMessageListenerContainer.invokeListener(
          23:57:57,696 ERROR [STDERR] 	at org.springframework.jms.listener.AbstractMessageListenerContainer.doExecuteListener(
          23:57:57,696 ERROR [STDERR] 	at org.springframework.jms.listener.AbstractPollingMessageListenerContainer.doReceiveAndExecute(
          23:57:57,696 ERROR [STDERR] 	at org.springframework.jms.listener.AbstractPollingMessageListenerContainer.receiveAndExecute(
          23:57:57,696 ERROR [STDERR] 	at org.springframework.jms.listener.DefaultMessageListenerContainer$AsyncMessageListenerInvoker.invokeListener(
          23:57:57,696 ERROR [STDERR] 	at org.springframework.jms.listener.DefaultMessageListenerContainer$AsyncMessageListenerInvoker.executeOngoingLoop(
          23:57:57,696 ERROR [STDERR] 	at org.springframework.jms.listener.DefaultMessageListenerContainer$
          23:57:57,696 ERROR [STDERR] 	at Source)
          The exception is encountered in QueueListener class created in above post.


          • #6
            Message Exception

            Problem seems to be very weired, each time i start/stop jboss I am getting either of the above exception.
            I am using Spring Integration 2.0.0 M and SPRING 3.
            can someone please let me know whats going on? why i am getting these random exceptions?
            Also how can I achieve the task, listed above?


            • #7
              You should probably not use jms:channel, but the jms gateways or channel adapters for this. I don't know what JBoss is doing, but the dispatcher has no subscribers error happens if you send a message to a direct channel that doesn't have subscribers.

              And yes, the error message could be better, there's an issue open for that.


              • #8
                The ClassCastException that you are seeing indicates that the received JMS Message is not a TextMessage... it's a JBoss "SpyMessage". I'm not sure if that's what you are intending to send, but if so, you might want to consider a custom MessageConverter implementation to deal with that type.


                • #9
                  Message Exception

                  Hi iwein,

                  Can you please tell me why I shouldn't use jms:channel here? can you please provide more details about same, it would help me to understand spring integration better.
                  Also I am sending message to Queue and not to Channel directly.
                  How can I achieve the same functionlity by using gateways or channel adapters, my primary intention is not to write any java code but getting this task done with just configuration.

                  Well now I am getting this error consistently, not sure why
                  org.springframework.integration.message.MessageDel iveryException: Dispatcher has no subscribers.


                  • #10
                    Message Exception

                    Can someone please help me to figure out where is the problem?


                    • #11
                      The <jms:channel/> is really intended for use where the producer and consumer are both Spring Integration components. For one-way interaction, you can use the <jms:message-driven-channel-adapter/> and <jms: outbound-channel-adapter/>. Any JMS producer could send to that inbound (message-driven) channel-adapter's Destination, and the Spring Integration components subscribed to its channel would receive the converted Message. For outbound, it's also uni-directional but in the other direction (from a Spring Integration component to a JMS Destination).

                      What is your ultimate goal here? I guess you are using that bridge as a first step prior to adding some other component between the JMS Destinations?


                      • #12
                        JMS - Spring Integration

                        Well mark, thanks for your reply. As explained earlier, I want to take a message from one queue as and when it arrives and pass it to another queue.
                        Isnt that possible without writing a single line of code and just with the configuration?
                        Well the reason behind that is, instead of using SPRING integration what if I just write a message listener bean and then as and when message comes the bean will just write that message to another queue.
                        Now the question is why I should use SPRING integration in such scenario, if I will have to write Java code. I have taken very basic example here.


                        • #13
                          Okay. For that use-case, you should use the inbound/outbound adapter pair. If you are not planning to add any components between them, you don't need a bridge as long as they share the same channel reference. The jms:channel on the other hand is simply for replacing a "channel" in Spring Integration with one that is backed by a JMS Queue (to be used when the producer and consumer are both Spring Integration components).


                          • #14
                            JMS - Message Queue

                            Well finally, I could able to achieve the desired functionality, but now there is one more problem. Have a look at the following configuration
                            <jee:jndi-lookup jndi-name="ConnectionFactory" id="connectionFactory"
                            environment-ref="activeMqProps" expected-type="javax.jms.ConnectionFactory" />
                            <jee:jndi-lookup jndi-name="ConnectionFactory" id="jbossConnectionFactory"
                            environment-ref="jndiJbossProps" expected-type="javax.jms.ConnectionFactory" />
                            <jee:jndi-lookup jndi-name="MyQueue" id="activeMqQueue"
                            environment-ref="activeMqProps" expected-type="javax.jms.Queue" />
                            <jee:jndi-lookup jndi-name="queue/B" id="jBossQueueTarget"
                            environment-ref="jndiJbossProps" expected-type="javax.jms.Queue" />
                            <util:properties id="activeMqProps" location=""/>
                            <util:properties id="jndiJbossProps" location=""/>
                            <jms:inbound-channel-adapter destination="activeMqQueue" channel="jbossIn" >
                            	<integration:interval-trigger interval="1000"/>
                            <jms:outbound-channel-adapter destination="jBossQueueTarget"  channel="jbossIn">
                            <integration:channel id="jbossIn" />
                            When I send a message from Servlet to ActiveMQ - MyQueue, it dynamically creates queue.B in Active MQ.
                            While sometimes the message is sent to Jboss Queue "queue/B" and sometimes the message is sent to Active MQ - "queue.B"
                            Can someone please tell me why this is happenning, why the dynamic queue is getting created in Active MQ? why messages are redirected to Active MQ rather than Jboss Queue?


                            • #15
                              If you don't explicitly provide a "connection-factory" attribute on those channel adapters, it will use the one named "connectionFactory". In your case, that means they should always be using the ActiveMQ ConnectionFactory rather than the JBoss one.