SyncCommunityReceiver.java 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. package com.zoniot.ccrc.rabbit;
  2. import com.alibaba.fastjson.JSON;
  3. import com.rabbitmq.client.Channel;
  4. import com.zoniot.ccrc.commom.model.CommunityData;
  5. import com.zoniot.ccrc.service.CommunityService;
  6. import com.zoniot.ccrc.service.DeviceService;
  7. import lombok.extern.slf4j.Slf4j;
  8. import org.springframework.amqp.core.Message;
  9. import org.springframework.amqp.rabbit.annotation.RabbitListener;
  10. import org.springframework.beans.factory.annotation.Autowired;
  11. import org.springframework.stereotype.Component;
  12. import java.io.IOException;
  13. @Component
  14. @Slf4j
  15. public class SyncCommunityReceiver {
  16. @Autowired
  17. private CommunityService communityService;
  18. //@RabbitListener(queues = {"${spring.rabbitmq.community.queue}"},containerFactory = "customContainerFactory")
  19. public void receiver(Channel channel, Message message) throws IOException {
  20. try {
  21. String msg = new String(message.getBody());
  22. log.info("-----SyncCommunityReceiver msg-----," +msg);
  23. CommunityData communityData = JSON.parseObject(msg, CommunityData.class);
  24. // 1,处理消息
  25. communityService.syncCommunity(communityData);
  26. // 2,确认消息消费成功 
  27. channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
  28. } catch (Exception e) {
  29. channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, false);
  30. log.error("consumer message error !",e);
  31. }
  32. }
  33. }