Hello I have a problem with multi rabbit mq when I tried to Autowired Connection Factory Context Wrapper I got this message:
APPLICATION FAILED TO START
Description:
Field contextWrapper in com.example.demo.DemoApplication required a bean of type 'org.springframework.boot.autoconfigure.amqp.ConnectionFactoryContextWrapper' that could not be found.
The injection point has the following annotations: - @org.springframework.beans.factory.annotation.Autowired(required=true)
Action:
Consider defining a bean of type 'org.springframework.boot.autoconfigure.amqp.ConnectionFactoryContextWrapper' in your configuration.
Process finished with exit code 0
and this is my code:
package com.example.demo;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.amqp.ConnectionFactoryContextWrapper;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.RestController;
@EnableRabbit
@RestController
@EnableScheduling
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@Autowired
private RabbitTemplate rabbitTemplate;
@Autowired
private ConnectionFactoryContextWrapper contextWrapper;
@Scheduled(fixedRate = 1000L)
void publish() {
rabbitTemplate.convertAndSend("ex1", "rt1", "data");
contextWrapper.run("broker2",
() -> rabbitTemplate.convertAndSend("ex2", "rt2", "data"));
}
@RabbitListener(bindings = @QueueBinding(exchange = @Exchange("ex1"), key = "rt1", value = @Queue("q1")))
void consume1(String message) {
System.out.println("RabbitMQ #1: " + message);
}
@RabbitListener(containerFactory = "broker2",
bindings = @QueueBinding(exchange = @Exchange("ex2"), key = "rt2", value = @Queue("q2")))
void consume2(String message) {
System.out.println("RabbitMQ #2: " + message);
}
}
This is my pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- Multiples rabbit mq-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.amqp</groupId>
<artifactId>spring-rabbit</artifactId>
<version>2.4.6</version>
</dependency>
<dependency>
<groupId>com.free-now.multirabbit</groupId>
<artifactId>spring-multirabbit</artifactId>
<version>2.7.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
and my properties.yml
spring:
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
multirabbitmq:
connections:
broker2:
host: localhost
port: 5673
username: guest
password: guest
broker3:
host: localhost
port: 5674
username: guest
password: guest
I can't find the solution to my problem, looks like problem with spring boot version, but i'm not sure, I don't have a problem with Autowired RabbitTemplate is just with connectionfactorycontextwrapper