Search code examples
spring-bootrabbitmqamqpspring-amqp

How to send mesage to rabitmq according spring boot getting started


I am trying follow steps from GETTING STARTED Messaging with RabbitMQ

I have downloaded and started rabitmq.
I use windows and it lauched as service:

enter image description here

I downloaded project skeleton and typed code from the article.
Thus project structure looks like this:

enter image description here

Application:

package hello;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@SpringBootApplication
public class Application {

    final static String queueName = "spring-boot";

    @Bean
    Queue queue() {
        return new Queue(queueName, false);
    }

    @Bean
    TopicExchange exchange() {
        return new TopicExchange("spring-boot-exchange");
    }

    @Bean
    Binding binding(Queue queue, TopicExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(queueName);
    }

    @Bean
    SimpleMessageListenerContainer container(ConnectionFactory connectionFactory,
                                             MessageListenerAdapter listenerAdapter) {
        SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);
        container.setQueueNames(queueName);
        container.setMessageListener(listenerAdapter);
        return container;
    }

    @Bean
    MessageListenerAdapter listenerAdapter(Receiver receiver) {
        return new MessageListenerAdapter(receiver, "receiveMessage");
    }


    public static void main(String[] args) throws InterruptedException {
        AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(Application.class);
    }
}

Receiver:

package hello;

import java.util.concurrent.CountDownLatch;
import org.springframework.stereotype.Component;

@Component
public class Receiver {

    private CountDownLatch latch = new CountDownLatch(1);

    public void receiveMessage(String message) {
        System.out.println("Received <" + message + ">");
        latch.countDown();
    }

    public CountDownLatch getLatch() {
        return latch;
    }

}

Runner:

package hello;

import java.util.concurrent.TimeUnit;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.stereotype.Component;

@Component
public class Runner implements CommandLineRunner {

    private final RabbitTemplate rabbitTemplate;
    private final Receiver receiver;
    private final ConfigurableApplicationContext context;

    public Runner(Receiver receiver, RabbitTemplate rabbitTemplate,
                  ConfigurableApplicationContext context) {
        this.receiver = receiver;
        this.rabbitTemplate = rabbitTemplate;
        this.context = context;
    }

    @Override
    public void run(String... args) throws Exception {
        System.out.println("Sending message...");
        rabbitTemplate.convertAndSend(Application.queueName, "Hello from RabbitMQ!");
        receiver.getLatch().await(10000, TimeUnit.MILLISECONDS);
        context.close();
    }

}

Then in the guide written that I need to start main method and I will see output like this:

Sending message...
Received

But I don't see these lines.
I see only lines like:

15:51:45.720 [container-1] DEBUG org.springframework.amqp.rabbit.listener.BlockingQueueConsumer - Retrieving delivery for Consumer@5bb8f9e2: tags=[{amq.ctag-2wLW2IpZdDrHrPqtlqOd4Q=spring-boot}], channel=Cached Rabbit Channel: AMQChannel(amqp://guest@127.0.0.1:5672/,1), conn: Proxy@3921095e Shared Rabbit Connection: SimpleConnection@226c7c81 [delegate=amqp://guest@127.0.0.1:5672/, localPort= 53157], acknowledgeMode=AUTO local queue size=0
15:51:46.721 [container-1] DEBUG org.springframework.amqp.rabbit.listener.BlockingQueueConsumer - Retrieving delivery for Consumer@5bb8f9e2: tags=[{amq.ctag-2wLW2IpZdDrHrPqtlqOd4Q=spring-boot}], channel=Cached Rabbit Channel: AMQChannel(amqp://guest@127.0.0.1:5672/,1), conn: Proxy@3921095e Shared Rabbit Connection: SimpleConnection@226c7c81 [delegate=amqp://guest@127.0.0.1:5672/, localPort= 53157], acknowledgeMode=AUTO local queue size=0
15:51:47.722 [container-1] DEBUG org.springframework.amqp.rabbit.listener.BlockingQueueConsumer - Retrieving delivery for Consumer@5bb8f9e2: tags=[{amq.ctag-2wLW2IpZdDrHrPqtlqOd4Q=spring-boot}], channel=Cached Rabbit Channel: AMQChannel(amqp://guest@127.0.0.1:5672/,1), conn: Proxy@3921095e Shared Rabbit Connection: SimpleConnection@226c7c81 [delegate=amqp://guest@127.0.0.1:5672/, localPort= 53157], acknowledgeMode=AUTO local queue size=0

P.S.

I understand That Runner class is not using in my application but I don't understand how to instantiate it and where to invoke. I can't find relevant information in guide.

Please, help me start my application.


Solution

  • Your problem is in the:

    public static void main(String[] args) throws InterruptedException {
        AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(Application.class);
    }
    

    We talk here about Spring Boot, therefore that code must be:

    SpringAplication.run(Application.class, args);
    

    http://docs.spring.io/spring-boot/docs/current/reference/html/getting-started-first-application.html#getting-started-first-application-main-method