Page 1 of 2 12 LastLast
Results 1 to 10 of 13

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

  1. #1

    Default 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:p="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" p:password="guest" p: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. #2

    Default 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 !

  3. #3
    Join Date
    Oct 2005
    Location
    Boston, MA
    Posts
    2,840

    Default

    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

  4. #4

    Default

    Quote 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?

  5. #5

    Default

    any pointers for hello world? Any lights on the error shown in the first post? somebody?
    SCBCD (EJB 3.0), SCWCD 1.5, SCJP 1.4

  6. #6
    Join Date
    Oct 2005
    Location
    Boston, MA
    Posts
    2,840

    Default

    Does "myQueue" exist on the broker?

  7. #7

    Default

    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?
    SCBCD (EJB 3.0), SCWCD 1.5, SCJP 1.4

  8. #8
    Join Date
    Oct 2005
    Location
    Boston, MA
    Posts
    2,840

    Default

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

  9. #9

    Default

    Thanks a lot Mark!
    I will be looking at the specs and will try it out
    SCBCD (EJB 3.0), SCWCD 1.5, SCJP 1.4

  10. #10

    Default

    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
    SCBCD (EJB 3.0), SCWCD 1.5, SCJP 1.4

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •