I\'m working on a project that involves 2 queue\'s, and multiples Listeners interacting with them. Flow:
Based on the scenario explained above I have tried to create a sample application which uses Spring Java Config.
Messages are published to trashroute and webapp queues, and respective receivers (persistence and webapp) receive the messages.
RabbitConfiguration.java (Contains configuration for both Sender and Receiver)
@Configuration
@EnableRabbit
public class RabbitConfiguration {
public static final String BROADCAST_TRASHROUTE_QUEUE = "trashroute.rabbit.queue";
public static final String BROADCAST_WEBAPP_QUEUE = "webapp.rabbit.queue";
@Bean
public ConnectionFactory connectionFactory() {
CachingConnectionFactory connectionFactory =
new CachingConnectionFactory("localhost");
return connectionFactory;
}
@Bean
public AmqpAdmin amqpAdmin() {
return new RabbitAdmin(connectionFactory());
}
@Bean
public Queue trashRouteQueue() {
return new Queue(BROADCAST_TRASHROUTE_QUEUE);
}
@Bean
public Queue webAppQueue() {
return new Queue(BROADCAST_WEBAPP_QUEUE);
}
@Bean
public RabbitTemplate rabbitTemplate() {
RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory());
return rabbitTemplate;
}
@Bean
public FanoutExchange trashRouteExchange() {
FanoutExchange exchange = new FanoutExchange("trashroute");
return exchange;
}
@Bean
public Binding trashRouteBinding() {
return BindingBuilder.bind(trashRouteQueue()).to(trashRouteExchange());
}
@Bean
public Binding webAppBinding() {
return BindingBuilder.bind(webAppQueue()).to(trashRouteExchange());
}
@Bean
SimpleMessageListenerContainer persistenceListenerContainer(ConnectionFactory connectionFactory, @Qualifier("persistenceListenerAdapter") MessageListenerAdapter listenerAdapter) {
SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
container.setConnectionFactory(connectionFactory);
container.setQueues(trashRouteQueue(), webAppQueue());
container.setMessageListener(listenerAdapter);
return container;
}
@Bean
MessageListenerAdapter persistenceListenerAdapter(PersistenceListener receiver) {
return new MessageListenerAdapter(receiver, "receiveMessage");
}
@Bean
SimpleMessageListenerContainer webAppListenerContainer(ConnectionFactory connectionFactory, @Qualifier("webAppListenerAdapter") MessageListenerAdapter listenerAdapter) {
SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
container.setConnectionFactory(connectionFactory);
container.setQueues(trashRouteQueue(), webAppQueue());
container.setMessageListener(listenerAdapter);
return container;
}
@Bean
MessageListenerAdapter webAppListenerAdapter(WebAppListener webAppListener) {
return new MessageListenerAdapter(webAppListener, "receiveMessage");
}
@Bean
PersistenceListener persistenceListener() {
return new PersistenceListener();
}
@Bean
WebAppListener webAppListener() {
return new WebAppListener();
}
}
PersistenceListener.java
public class PersistenceListener {
public void receiveMessage(String message) {
System.out.println("Persistence Listener: Messsage Received <" + message + ">");
}
}
WebAppListener.java
public class WebAppListener {
public void receiveMessage(String message) {
System.out.println("WebAppListener: Message Received <" + message + ">");
}
}
Application.java
@SpringBootApplication
public class Application implements CommandLineRunner {
@Autowired
AnnotationConfigApplicationContext context;
public static void main(String[] args) throws InterruptedException {
SpringApplication.run(Application.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 rabbitTemplate = (RabbitTemplate) context.getBean("rabbitTemplate");
rabbitTemplate.convertAndSend(RabbitConfiguration.BROADCAST_TRASHROUTE_QUEUE, "Hello from trashroute queue!");
rabbitTemplate.convertAndSend(RabbitConfiguration.BROADCAST_WEBAPP_QUEUE, "Hello from webapp queue!");
Thread.sleep(10000);
context.close();
}
}
Hope this will help. Although you would need to refactor the code if you want to use this in production.