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

  • JMS Message - Spring integration

    Hi,

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

    Code:
    <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="classpath:jndi.properties"/>
    	
    <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
    Code:
    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.");
    queueSender.send(textMessage);
    I have created a listener to listen on "jBossQueueTarget". Following is the code snippet
    Code:
    try{
       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.

    Comment


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

      Comment


      • #4
        Exception while sending message

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

        Code:
        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(JmsDestinationBackedMessageChannel.java:157)
        	at org.springframework.jms.listener.AbstractMessageListenerContainer.doInvokeListener(AbstractMessageListenerContainer.java:559)
        	at org.springframework.jms.listener.AbstractMessageListenerContainer.invokeListener(AbstractMessageListenerContainer.java:498)
        	at org.springframework.jms.listener.AbstractMessageListenerContainer.doExecuteListener(AbstractMessageListenerContainer.java:467)
        	at org.springframework.jms.listener.AbstractPollingMessageListenerContainer.doReceiveAndExecute(AbstractPollingMessageListenerContainer.java:323)
        	at org.springframework.jms.listener.AbstractPollingMessageListenerContainer.receiveAndExecute(AbstractPollingMessageListenerContainer.java:261)
        	at org.springframework.jms.listener.DefaultMessageListenerContainer$AsyncMessageListenerInvoker.invokeListener(DefaultMessageListenerContainer.java:976)
        	at org.springframework.jms.listener.DefaultMessageListenerContainer$AsyncMessageListenerInvoker.executeOngoingLoop(DefaultMessageListenerContainer.java:968)
        	at org.springframework.jms.listener.DefaultMessageListenerContainer$AsyncMessageListenerInvoker.run(DefaultMessageListenerContainer.java:870)
        	at java.lang.Thread.run(Unknown Source)
        Caused by: org.springframework.integration.message.MessageDeliveryException: Dispatcher has no subscribers.
        	at org.springframework.integration.dispatcher.UnicastingDispatcher.doDispatch(UnicastingDispatcher.java:97)
        	at org.springframework.integration.dispatcher.UnicastingDispatcher.dispatch(UnicastingDispatcher.java:90)
        	at org.springframework.integration.jms.JmsDestinationBackedMessageChannel.onMessage(JmsDestinationBackedMessageChannel.java:154)
        	... 9 more

        Comment


        • #5
          Message Exception - class cast

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

          Code:
          23:57:57,696 ERROR [STDERR] java.lang.ClassCastException: org.jboss.mq.SpyObjectMessage cannot be cast to javax.jms.TextMessage
          23:57:57,696 ERROR [STDERR] 	at com.spring.integration.mq.QueueListener.onMessage(QueueListener.java:21)
          23:57:57,696 ERROR [STDERR] 	at org.springframework.jms.listener.AbstractMessageListenerContainer.doInvokeListener(AbstractMessageListenerContainer.java:559)
          23:57:57,696 ERROR [STDERR] 	at org.springframework.jms.listener.AbstractMessageListenerContainer.invokeListener(AbstractMessageListenerContainer.java:498)
          23:57:57,696 ERROR [STDERR] 	at org.springframework.jms.listener.AbstractMessageListenerContainer.doExecuteListener(AbstractMessageListenerContainer.java:467)
          23:57:57,696 ERROR [STDERR] 	at org.springframework.jms.listener.AbstractPollingMessageListenerContainer.doReceiveAndExecute(AbstractPollingMessageListenerContainer.java:323)
          23:57:57,696 ERROR [STDERR] 	at org.springframework.jms.listener.AbstractPollingMessageListenerContainer.receiveAndExecute(AbstractPollingMessageListenerContainer.java:261)
          23:57:57,696 ERROR [STDERR] 	at org.springframework.jms.listener.DefaultMessageListenerContainer$AsyncMessageListenerInvoker.invokeListener(DefaultMessageListenerContainer.java:976)
          23:57:57,696 ERROR [STDERR] 	at org.springframework.jms.listener.DefaultMessageListenerContainer$AsyncMessageListenerInvoker.executeOngoingLoop(DefaultMessageListenerContainer.java:968)
          23:57:57,696 ERROR [STDERR] 	at org.springframework.jms.listener.DefaultMessageListenerContainer$AsyncMessageListenerInvoker.run(DefaultMessageListenerContainer.java:870)
          23:57:57,696 ERROR [STDERR] 	at java.lang.Thread.run(Unknown Source)
          The exception is encountered in QueueListener class created in above post.

          Comment


          • #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?

            Comment


            • #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.

              Comment


              • #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.

                Comment


                • #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.

                  Comment


                  • #10
                    Message Exception

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

                    Comment


                    • #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?

                      Comment


                      • #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.

                        Comment


                        • #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).

                          Comment


                          • #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
                            Code:
                            <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="classpath:jndi.properties"/>
                            <util:properties id="jndiJbossProps" location="classpath:jndi-jboss.properties"/>
                            	
                            <jms:inbound-channel-adapter destination="activeMqQueue" channel="jbossIn" >
                                    <integration:poller>
                            	<integration:interval-trigger interval="1000"/>
                                   </integration:poller>
                            </jms:inbound-channel-adapter>
                            	
                            <jms:outbound-channel-adapter destination="jBossQueueTarget"  channel="jbossIn">
                            </jms:outbound-channel-adapter>
                            	
                            <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?

                            Comment


                            • #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.

                              Comment

                              Working...
                              X