Announcement Announcement Module
Collapse
No announcement yet.
Not able to run an example with org.springframework.amqp.rabbit.core.RabbitTemplat e Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Not able to run an example with org.springframework.amqp.rabbit.core.RabbitTemplat e

    Hi,

    I have the requirement of using rabbitmq with spring framework. I am taking help from this page : http://ndpar.blogspot.com/2010/08/wo...in-spring.html

    The code being used is this :

    applicationcontext.xml
    =================

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schem...-beans-3.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <context:annotation-config />

    <bean id="rabbitConnectionFactory" class="org.springframework.amqp.rabbit.connection. SingleConnectionFactory"
    p:username="guest" password="guest" port="5672">
    <constructor-arg value="localhost" />
    </bean>

    <bean id="rabbitTemplate" class="org.springframework.amqp.rabbit.core.Rabbit Template"
    p:connectionFactory-ref="rabbitConnectionFactory" p:exchange="amq.fanout" />

    <bean id="messageSender" class="rabbitMQSimpleApp.MessageSender" />

    <bean class="org.springframework.amqp.rabbit.listener.Si mpleMessageListenerContainer"
    p:connectionFactory-ref="rabbitConnectionFactory" p:queueName="myQueue"
    p:messageListener-ref="messageListener" />

    <bean id="messageListener" class="rabbitMQSimpleApp.MessageHandler" />

    </beans>

    MessageHandler.java
    ================
    package rabbitMQSimpleApp;

    import org.springframework.amqp.core.Message;
    import org.springframework.amqp.core.MessageListener;

    public class MessageHandler implements MessageListener {

    @Override
    public void onMessage(Message message) {
    System.out.println("Received message: " + message);
    System.out.println("Text: " + new String(message.getBody()));
    }
    }


    MessageSender.java
    ===============
    package rabbitMQSimpleApp;

    import org.springframework.amqp.core.AmqpTemplate;
    import org.springframework.beans.factory.annotation.Autow ired;

    public class MessageSender {

    @Autowired
    private AmqpTemplate template;

    public void send(String text) {
    template.convertAndSend(text);
    }
    }


    Sender.java
    ==========
    package rabbitMQSimpleApp;

    import org.springframework.context.ConfigurableApplicatio nContext;
    import org.springframework.context.support.ClassPathXmlAp plicationContext;

    public class Sender {

    public static void main(String[] args) {
    ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("rabbitMQSimpleApp/applicationContext.xml");
    MessageSender messageSender = context.getBean(MessageSender.class);
    messageSender.send("Hello from Spring-AMQP");
    }
    }

    Exception coming
    =============
    Exception in thread "main" org.springframework.context.ApplicationContextExce ption: Failed to start bean 'org.springframework.amqp.rabbit.listener.SimpleMe ssageListenerContainer#0'; nested exception is org.springframework.amqp.AmqpIOException: java.io.IOException
    at org.springframework.context.support.DefaultLifecyc leProcessor.doStart(DefaultLifecycleProcessor.java :169)
    at org.springframework.context.support.DefaultLifecyc leProcessor.access$1(DefaultLifecycleProcessor.jav a:154)
    at org.springframework.context.support.DefaultLifecyc leProcessor$LifecycleGroup.start(DefaultLifecycleP rocessor.java:335)
    at org.springframework.context.support.DefaultLifecyc leProcessor.startBeans(DefaultLifecycleProcessor.j ava:143)
    at org.springframework.context.support.DefaultLifecyc leProcessor.onRefresh(DefaultLifecycleProcessor.ja va:108)
    at org.springframework.context.support.AbstractApplic ationContext.finishRefresh(AbstractApplicationCont ext.java:908)
    at org.springframework.context.support.AbstractApplic ationContext.refresh(AbstractApplicationContext.ja va:428)
    at org.springframework.context.support.ClassPathXmlAp plicationContext.<init>(ClassPathXmlApplicationCon text.java:139)
    at org.springframework.context.support.ClassPathXmlAp plicationContext.<init>(ClassPathXmlApplicationCon text.java:83)
    at rabbitMQSimpleApp.Sender.main(Sender.java:9)
    Caused by: org.springframework.amqp.AmqpIOException: java.io.IOException
    at org.springframework.amqp.rabbit.support.RabbitUtil s.convertRabbitAccessException(RabbitUtils.java:11 8)
    at org.springframework.amqp.rabbit.support.RabbitAcce ssor.convertRabbitAccessException(RabbitAccessor.j ava:107)
    at org.springframework.amqp.rabbit.listener.AbstractR abbitListeningContainer.start(AbstractRabbitListen ingContainer.java:200)
    at org.springframework.context.support.DefaultLifecyc leProcessor.doStart(DefaultLifecycleProcessor.java :166)
    ... 9 more
    Caused by: java.io.IOException
    at com.rabbitmq.client.impl.AMQChannel.wrap(AMQChanne l.java:121)
    at com.rabbitmq.client.impl.AMQChannel.exnWrappingRpc (AMQChannel.java:145)
    at com.rabbitmq.client.impl.ChannelN.queueDeclarePass ive(ChannelN.java:548)
    at com.rabbitmq.client.impl.ChannelN.queueDeclarePass ive(ChannelN.java:71)
    at org.springframework.amqp.rabbit.listener.SimpleMes sageListenerContainer.createBlockingQueueConsumer( SimpleMessageListenerContainer.java:214)
    at org.springframework.amqp.rabbit.listener.SimpleMes sageListenerContainer.initializeConsumers(SimpleMe ssageListenerContainer.java:191)
    at org.springframework.amqp.rabbit.listener.SimpleMes sageListenerContainer.doStart(SimpleMessageListene rContainer.java:157)
    at org.springframework.amqp.rabbit.listener.AbstractR abbitListeningContainer.start(AbstractRabbitListen ingContainer.java:197)
    ... 10 more

  • #2
    Primary aim is to run a pub sub mechanism

    Is there any example for a pub-sub mechanism against rabbitmq+spring combo ?

    The sample application looks too clumsy as it contains all the configuration code in java files and is not understandable !

    Help appreciated !

    Comment


    • #3
      Are you talking about the Hello World sample? Our goal there was to keep it as simple and clear as possible. Can you elaborate on what you find "clumsy"?

      Thanks,
      Mark

      Comment


      • #4
        Originally posted by Mark Fisher View Post
        Are you talking about the Hello World sample? Our goal there was to keep it as simple and clear as possible. Can you elaborate on what you find "clumsy"?

        Thanks,
        Mark
        okay, let me explain it :

        I have to run an asynch pub-sub mechanism as given in hello world sample..

        We have the following java files:
        1. Producer....it's using ProducerConfiguration (ProducerConfiguration class contains the code which I can not understand, as I am new to spring...I think that we can do these configurations through rabbitConfiguration.xml file and that would be understandable)
        2. Consumer.......it's using ConsumerConfiguration directly and HelloWorldHandler/HelloWorldConfiguration indirectly (ConsumerConfiguration and HelloWorldConfiguration classes can be again configured through rabbitConfiguration.xml file and that would be understandable)

        Can you please help me transferring this configurations to rabbitConfiguration.xml and explain?

        Comment


        • #5
          any pointers for hello world? Any lights on the error shown in the first post? somebody?

          Comment


          • #6
            Does "myQueue" exist on the broker?

            Comment


            • #7
              yes, that might be the problem...
              is there a way we can create it through the code?
              And in JMS, we have "topics" for pub-sub...seems here we have "queues" both in p2p and pubsub, is it?

              Comment


              • #8
                You can declare queues through our AmqpAdmin. Please refer to the documentation:
                http://static.springsource.org/sprin...-configuration

                As for the question about "topics", AMQP has a different model and different terminology. One important point is the distinction between Exhanges and Queues. Please refer to the AMQP Specification for an overview:
                http://www.amqp.org/confluence/displ...+Specification (I would recommend reading about Exchanges in version 0-9-1).

                Comment


                • #9
                  Thanks a lot Mark!
                  I will be looking at the specs and will try it out

                  Comment


                  • #10
                    Hi Mark,

                    I have gone through the code and the links suggested by you. I am in the process of transferring all the configuration code from java to xml file for the hello world example. I have a doubt with the ConsumerConfiguration class. You have defined listenerContainer() in ConsumerConfiguration class which is returning a SimpleMessageListenerContainer object. Now I am not able to find out that who is using that SimpleMessageListenerContainer object in the hierarchy of ConsumerConfiguration class? I have to map this object to something in my xml config file. Message is correctly being sent to the Broker but nothing happens when I run my Consumer.

                    Here is my code:


                    Producer.java
                    ==========
                    package rabbitMQSampleApp1;

                    import org.springframework.amqp.core.AmqpTemplate;
                    import org.springframework.context.ConfigurableApplicatio nContext;
                    import org.springframework.context.support.ClassPathXmlAp plicationContext;

                    public class Producer {

                    public static void main(String[] args) throws Exception {
                    ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("rabbitMQSampleApp1/rabbitConfiguration.xml");
                    AmqpTemplate amqpTemplate = context.getBean(AmqpTemplate.class);
                    amqpTemplate.convertAndSend("Hello World");
                    System.out.println("Message Sent");
                    }

                    }


                    ConsumerHandler.java
                    =================
                    package rabbitMQSampleApp1;

                    public class ConsumerHandler {
                    public void handleMessage(String text) {
                    System.out.println("Received: " + text);
                    }
                    }


                    Consumer.java
                    ===========
                    package rabbitMQSampleApp1;

                    import org.springframework.context.annotation.AnnotationC onfigApplicationContext;

                    public class Consumer {

                    public static void main(String[] args) {
                    new AnnotationConfigApplicationContext("rabbitMQSample App1/rabbitConfiguration.xml");
                    }
                    }

                    rabbitConfiguration.xml
                    ==================
                    <?xml version="1.0" encoding="UTF-8"?>
                    <beans xmlns="http://www.springframework.org/schema/beans"
                    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                    xmlns="http://www.springframework.org/schema/p"
                    xmlns:context="http://www.springframework.org/schema/context"
                    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schem...-beans-3.0.xsd
                    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

                    <!-- Define a connectionFactory -->
                    <bean id="connectionFactory" class="org.springframework.amqp.rabbit.connection. SingleConnectionFactory">
                    <constructor-arg value="localhost"/>
                    <property name="username" value="guest"/>
                    <property name="password" value="guest"/>
                    </bean>

                    <!-- Tell the AMQP Admin about that connectionFactory -->
                    <bean id="amqpAdmin" class="org.springframework.amqp.rabbit.core.Rabbit Admin">
                    <constructor-arg ref="connectionFactory"/>
                    </bean>

                    <!-- Since the AMQP Admin knows about the connectionFactory,
                    we can create a queue on Rabbit Broker using the RabbitTemplate provided by Spring framework-Rabbit APIs -->
                    <bean id="rabbitTemplate" class="org.springframework.amqp.rabbit.core.Rabbit Template"
                    p:connectionFactory-ref="connectionFactory" p:queue="hello.world.queue" />

                    <!-- With the help of all the configurations mentioned above this line, a producer can simply use an
                    amqp template in its java file to publish anything on the that queue only ??
                    Question is that how and why an amqp template objects sends the notification only on that queue? -->


                    <!-- Below are the consumer settings -->

                    <!-- make one consumer handler -->
                    <bean id="consumerHandler" class="rabbitMQSampleApp1.ConsumerHandler" />

                    <!-- pass a new instance of that consumer handler as a constructor argument to a messageListenerAdaptor object-->
                    <bean id="messageListenerAdaptor" class="org.springframework.amqp.rabbit.listener.ad apter.MessageListenerAdapter">
                    <constructor-arg ref="consumerHandler"/>
                    </bean>

                    <!-- make an object of SimpleMessageListenerContainer -->
                    <bean class="org.springframework.amqp.rabbit.listener.Si mpleMessageListenerContainer"
                    p:connectionFactory-ref="connectionFactory" p:queueName="hello.world.queue"
                    p:messageListener-ref="messageListenerAdaptor" />

                    <!-- What to do with the object of SimpleMessageListenerContainer -->

                    </beans>
                    Thanks,
                    Kshitiz

                    Comment


                    • #11
                      It runs finally

                      With the help of http://ndpar.blogspot.com/2010/08/wo...in-spring.html and Mark, I am able to run a sample finally

                      Here is the code

                      Admin.java
                      =========
                      package rabbitMQSampleApp;
                      import org.springframework.amqp.core.AmqpAdmin;
                      import org.springframework.amqp.core.Binding;
                      import org.springframework.amqp.core.DirectExchange;
                      import org.springframework.amqp.core.Queue;
                      import org.springframework.amqp.rabbit.core.RabbitAdmin;

                      import org.springframework.context.ConfigurableApplicatio nContext;
                      import org.springframework.context.support.ClassPathXmlAp plicationContext;

                      public class Admin {
                      public static void main(String[] args) {
                      ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("rabbitMQSampleApp/applicationContext-admin.xml");
                      new Admin().declareEverything(context.getBean(RabbitAd min.class));
                      }
                      private void declareEverything(AmqpAdmin admin) {
                      Queue queue = new Queue("sampleQueue");
                      DirectExchange exchange = new DirectExchange("myExchange");
                      Binding binding = new Binding(queue, exchange, "myRoutingKey");
                      admin.declareQueue(queue);
                      admin.declareExchange(exchange);
                      admin.declareBinding(binding);
                      }
                      }

                      Consumer.java
                      ===========
                      package rabbitMQSampleApp;

                      import org.springframework.amqp.core.Message;
                      import org.springframework.amqp.core.MessageListener;

                      public class Consumer implements MessageListener {

                      @Override
                      public void onMessage(Message message) {
                      System.out.println("Received message: " + message);
                      }
                      }


                      Producer.java
                      ===========
                      package rabbitMQSampleApp;

                      import org.springframework.amqp.core.AmqpTemplate;
                      import org.springframework.context.ConfigurableApplicatio nContext;
                      import org.springframework.context.support.ClassPathXmlAp plicationContext;

                      public class Producer {
                      public static void main(String[] args) throws Exception {
                      ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("rabbitMQSampleApp/rabbitConfiguration.xml");
                      AmqpTemplate amqpTemplate = context.getBean(AmqpTemplate.class);

                      /* We have retrieved an amqpTemplate from the context.
                      * Producer can simply use that amqp template to publish anything.
                      * Question is that how and why this amqp template object
                      * sends the notification only on queue which we have declared through Admin class
                      * where is this mapping present ? */

                      amqpTemplate.convertAndSend("Hello World");
                      System.out.println("Message Sent");
                      }
                      }

                      applicationContext-admin.xml
                      =======================
                      <?xml version="1.0" encoding="UTF-8"?>
                      <beans xmlns="http://www.springframework.org/schema/beans"
                      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                      xmlns="http://www.springframework.org/schema/p"
                      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

                      <!-- Define a connectionFactory -->
                      <bean id="connectionFactory" class="org.springframework.amqp.rabbit.connection. SingleConnectionFactory">
                      <constructor-arg value="localhost" />
                      <property name="username" value="guest" />
                      <property name="password" value="guest" />
                      </bean>

                      <!-- Tell the Admin bean about that connectionFactory and initialize it, create a queue and an exchange on Rabbit Broker using the RabbitTemplate provided by Spring framework-Rabbit APIs -->
                      <bean id="Admin" class="org.springframework.amqp.rabbit.core.Rabbit Admin">
                      <constructor-arg ref="connectionFactory" />
                      </bean>

                      </beans>

                      rabbitConfiguration.xml
                      =================
                      <?xml version="1.0" encoding="UTF-8"?>
                      <beans xmlns="http://www.springframework.org/schema/beans"
                      xmlns="http://www.springframework.org/schema/p"
                      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                      xmlns:context="http://www.springframework.org/schema/context"
                      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schem...-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

                      <!-- Define a connectionFactory -->
                      <bean id="connectionFactory" class="org.springframework.amqp.rabbit.connection. SingleConnectionFactory">
                      <constructor-arg value="localhost" />
                      <property name="username" value="guest" />
                      <property name="password" value="guest" />
                      </bean>

                      <bean id="rabbitTemplate" class="org.springframework.amqp.rabbit.core.Rabbit Template"
                      p:connectionFactory-ref="connectionFactory"
                      p:routingKey="myRoutingKey"
                      p:exchange="myExchange" />

                      <!-- Declare a messageListener bean -->
                      <bean id="messageListener" class="rabbitMQSampleApp.Consumer" />

                      <!-- pass the above messageListener object and other settings to SimpleMessageListenerContainer in Rabbit API-->
                      <bean class="org.springframework.amqp.rabbit.listener.Si mpleMessageListenerContainer"
                      p:connectionFactory-ref="connectionFactory"
                      p:queueName="sampleQueue"
                      p:messageListener-ref="messageListener" />

                      </beans>

                      Comment


                      • #12
                        Unable to run the example

                        Hi,
                        I tried the above example and getting this error
                        can any one help--

                        Could not instantiate bean class [org.springframework.amqp.rabbit.connection.SingleC onnectionFactory]: Constructor threw exception; nested exception is java.lang.NoSuchMethodError: com.rabbitmq.client.ConnectionFactory.setHost(Ljav a/lang/StringV

                        Thanks In Advance,
                        Raghu varma

                        Comment


                        • #13
                          Update your rabbitmq-client
                          e.g.
                          <dependency>
                          <groupId>com.rabbitmq</groupId>
                          <artifactId>rabbitmq-client</artifactId>
                          <version>2.4.1</version>
                          </dependency>

                          Comment

                          Working...
                          X