-1

I try to learn RabbitMQ with SpringBoot i have create 2 classes : Receiver.java (it's a POJO):

This class will receive the message.

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;
    }
}

And my seconde class RabbitMq2Application.java (it is the springBootApplication) : for receive my message.

@SpringBootApplication
public class RabbitMq2Application implements CommandLineRunner{


    final static String queueName = "spring-boot";

    @Autowired
    AnnotationConfigApplicationContext context;

    @Autowired
    RabbitTemplate rabbitTemplate;

    @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
    Receiver receiver() {
        return new Receiver();
    }

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

    public static void main(String[] args) {
        SpringApplication.run(RabbitMq2Application.class, args);
    }

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

And I get this error : org.springframework.beans.factory.NoSuchBeanDefinitionException

I suspect it's because the connectionfactory because he is @autowired.

User0123456789
  • 760
  • 2
  • 10
  • 25
zaafrani
  • 65
  • 3
  • 9

1 Answers1

0

Please, share more StackTrace. Typically there are a lot of info around that NoSuchBeanDefinitionException.

From other hand check, please, if you import the proper ConnectionFactory class. That's because there are really all required beans for you (RabbitAutoConfiguration):

@Autowired
private ConnectionFactory connectionFactory;

@Bean
@ConditionalOnMissingBean(RabbitTemplate.class)
public RabbitTemplate rabbitTemplate() {
    return new RabbitTemplate(this.connectionFactory);
}

The required type is: org.springframework.amqp.rabbit.connection.ConnectionFactory.

Artem Bilan
  • 113,505
  • 11
  • 91
  • 118