Spring AMQP
程序员文章站
2022-07-15 08:12:42
...
service层
引用
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
配置文件
rabbitmq:
host: 192.168.56.101
virtual-host: /leyou
username: leyou
password: leyou
template:
exchange: LEYOU.ITME.EXCHANGE
写生产者
@Autowired
private AmqpTemplate amqpTemplate;
在某一方法(增删改)写入
……{
sendMsg("insert",spuBo.getId());
}
private void sendMsg(String type,Long id){
try{
this.amqpTemplate.convertAndSend("item." + type,id);
}catch (AmqpException e) {
e.printStackTrace();
}
}
写消费者
引用
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
rabbitmq:
host: 192.168.56.101
virtual-host: /leyou
username: leyou
password: leyou
写增、改
@Component
public class GoodsListener {
@Autowired
private GoodsHtmlService goodsHtmlService;
@RabbitListener(bindings = @QueueBinding(
value = @Queue(value="LEYOU.ITEM.SAVE.QUEUE",durable="true"),
exchange = @Exchange(value="LEYOU.ITEM.EXCHANGE",ignoreDeclarationExceptions="true",type=ExchangeTypes.TOPIC),
key = {"item.insert","item.update"}
))
public void save(Long id){
if(id == null){
return;
}
this.goodsHtmlService.createHtml(id);
}
}
写删除
public void deleteHtml(Long id){
File file = new File("C:\\tools\\nginx-1.14.0\\html\\item\\" + id + ".html");
file.deleteOnExit();
}
@RabbitListener(bindings = @QueueBinding(
value = @Queue(value="LEYOU.ITEM.DELETE.QUEUE",durable="true"),
exchange = @Exchange(value="LEYOU.ITEM.EXCHANGE",ignoreDeclarationExceptions="true",type=ExchangeTypes.TOPIC),
key = {"item.delete"}
))
public void delete(Long id){
if(id == null){
return;
}
this.goodsHtmlService.deleteHtml(id);
}
下一篇: python flask登录及装饰器验证
推荐阅读