AccountUpdateListener.java 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package yike.example.mq;
  2. import org.springframework.amqp.core.AmqpTemplate;
  3. import org.springframework.amqp.rabbit.annotation.Exchange;
  4. import org.springframework.amqp.rabbit.annotation.Queue;
  5. import org.springframework.amqp.rabbit.annotation.QueueBinding;
  6. import org.springframework.amqp.rabbit.annotation.RabbitListener;
  7. import org.springframework.beans.factory.annotation.Autowired;
  8. import org.springframework.stereotype.Component;
  9. import com.alibaba.fastjson.JSONObject;
  10. @Component
  11. public class AccountUpdateListener extends BaseListener{
  12. @Autowired
  13. private AmqpTemplate amqpTemplate;
  14. private final String routingKey = "account.update";
  15. private final String exchange = "account";
  16. private final String queueName = routingKey + QUEUE_NAME;
  17. public void send(String msg) {
  18. this.amqpTemplate.convertAndSend(exchange, routingKey, super.addExchangeAndRoutingKey(msg, exchange, routingKey));
  19. }
  20. public void send(Long id, String name) {
  21. JSONObject jsonObject = new JSONObject();
  22. jsonObject.put("id", id);
  23. jsonObject.put("name", name);
  24. this.amqpTemplate.convertAndSend(exchange, routingKey, super.addExchangeAndRoutingKey(jsonObject, exchange, routingKey));
  25. }
  26. // @RabbitListener(bindings = @QueueBinding(
  27. // value = @Queue(value = queueName, durable = "true"),
  28. // exchange = @Exchange(value = exchange, ignoreDeclarationExceptions = "true"),
  29. // key = routingKey ))
  30. public void receiveQueue(String text) {
  31. System.out.println("接受到:" + text);
  32. JSONObject jsonObject = JSONObject.parseObject(text);
  33. jsonObject.clear();
  34. }
  35. }