declare switch
@Bean public TopicExchange exchange() { return new TopicExchange(exchange); }
declare queue
@Bean public Queue delayedQueue(){ return new Queue(queue); } @Bean public Queue delayedQueue1(){ return new Queue(queue1); }
queue binding switch
@Bean public Binding queueBindingExchange(@Qualifier("delayedQueue") Queue queue, @Qualifier("exchange") Exchange exchange){ return BindingBuilder.bind(queue).to(exchange).with("#").noargs(); }
Configuration file:
rabbitmq: exchange: exchange name host: ip address port: port number default 5672 username: username password: password virtualHost: / queue: queue name
Complete code:
@Configuration
public class RabbitmqConfig {
@Value("${rabbitmq.exchange}")
private String exchange;
@Value("${rabbitmq.host}")
private String host;
@Value("${rabbitmq.port}")
private Integer port;
@Value("${rabbitmq.username}")
private String username;
@Value("${rabbitmq.password}")
private String password;
@Value("${rabbitmq.virtual-host}")
private String virtualHost;
@Value("${rabbitmq.queue}")
private String queue;
@Bean
public TopicExchange exchange() {
return new TopicExchange(exchange);
}
@Bean
public ConnectionFactory connectionFactory() {
CachingConnectionFactory connectionFactory = new CachingConnectionFactory(host);
connectionFactory.setUsername(username);
connectionFactory.setPassword(password);
connectionFactory.setPort(port);
connectionFactory.setVirtualHost(virtualHost);
return connectionFactory;
}
@Bean
public AmqpAdmin amqpAdmin() {
return new RabbitAdmin(connectionFactory());
}
@Bean
public RabbitTemplate rabbitTemplate() {
return new RabbitTemplate(connectionFactory());
}
@Bean
public Queue delayedQueue(){
return new Queue(queue);
}
@Bean
public Binding queueBindingExchange(@Qualifier("delayedQueue") Queue queue,
@Qualifier("exchange") Exchange exchange){
return BindingBuilder.bind(queue).to(exchange).with("#").noargs();
}
}
After deploying rabbitmq whether it is local, virtual machine or server, start the service:
Visit: http://ip address:15672/#/queues
View the queue:
View the switch:
Check the queue and switch binding status: