Page 2 of 2 FirstFirst 12
Results 11 to 13 of 13

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

  1. #11

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

  2. #12

    Default 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

  3. #13
    Join Date
    Jun 2011
    Posts
    1

    Default

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

Posting Permissions

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