配置文件,在rabbit中自動創建exchange,queue和綁定它們的關係
- 代碼裏初始化exchange
- 代碼裏初始化queue
- 代碼裏綁定exchange,queue和routekey
- 配置文件,直接聲明vhost
代碼裏初始化exchange
/**
* rabbitMq裏初始化exchange.
*
* @return
*/
@Bean
public TopicExchange crmExchange() {
return new TopicExchange(EXCHANGE);
}
代碼裏初始化queue
/**
* rabbitMq裏初始化隊列crm.hello.
*
* @return
*/
@Bean
public Queue helloQueue() {
return new Queue(HELLO);
}
代碼裏綁定exchange,queue和routekey
/**
* 綁定exchange & queue & routekey.
*
* @param queueMessage 隊列
* @param exchange 交換機
* @param routekey 路由
* @return
*/
public Binding bindingExchange(Queue queueMessage, TopicExchange exchange, String routekey) {
return BindingBuilder.bind(queueMessage).to(exchange).with(routekey);
}
配置文件
spring:
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
virtual-host: lind
完整代碼
package com.lind.microservice.productCenter.mq;
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.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* amqp配置.
*/
@Configuration
public class AmqpConfig {
/**
* 交換機.
*/
public final static String EXCHANGE = "crm";
/**
* hello隊列.
*/
public final static String HELLO = "crm.hello";
/**
* 創建訂單隊列.
*/
public final static String LIND_GENERATE_ORDER = "crm.generate.order";
/**
* 綁定exchange & queue & routekey.
*
* @param queueMessage 隊列
* @param exchange 交換機
* @param routekey 路由
* @return
*/
public Binding bindingExchange(Queue queueMessage, TopicExchange exchange, String routekey) {
return BindingBuilder.bind(queueMessage).to(exchange).with(routekey);
}
/**
* rabbitMq裏初始化exchange.
*
* @return
*/
@Bean
public TopicExchange crmExchange() {
return new TopicExchange(EXCHANGE);
}
/**
* rabbitMq裏初始化隊列crm.hello.
*
* @return
*/
@Bean
public Queue helloQueue() {
return new Queue(HELLO);
}
/**
* rabbitMq裏初始化隊列crm.generate.order.
*
* @return
*/
@Bean
public Queue orderQueue() {
return new Queue(LIND_GENERATE_ORDER);
}
}
隊列發佈者
package com.lind.microservice.productCenter.mq;
import java.util.Date;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
@Configuration
public class HelloPublisher {
@Autowired
AmqpTemplate rabbitTemplate;
@Autowired
AmqpConfig amqpConfig;
public void hello() {
String context = "hello " + new Date();
System.out.println("HelloPublisher : " + context);
amqpConfig.bindingExchange(
amqpConfig.helloQueue(),
amqpConfig.crmExchange(),
"crm.hello.#"
);
this.rabbitTemplate.convertAndSend(AmqpConfig.EXCHANGE, AmqpConfig.HELLO, context);
}
}
隊列訂閱者
package com.lind.microservice.productCenter.mq;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@Component
@RabbitListener(queues = AmqpConfig.HELLO)
public class HelloSubscriber {
@RabbitHandler
public void process(String hello) {
System.out.println("HelloSubscriber : " + hello);
}
}