欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Spring AMQP

程序员文章站 2022-07-15 08:12:42
...

service层
引用
Spring AMQP

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>

配置文件
Spring AMQP

 rabbitmq:
  host: 192.168.56.101
  virtual-host: /leyou
  username: leyou
  password: leyou
  template:
   exchange: LEYOU.ITME.EXCHANGE

写生产者
Spring AMQP

@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();
	}
}

写消费者
引用
Spring AMQP

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>

Spring AMQP

 rabbitmq:
  host: 192.168.56.101
  virtual-host: /leyou
  username: leyou
  password: leyou

Spring AMQP
写增、改

@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);
	}

Spring AMQP

相关标签: 小知识 JAVA