12345678910111213141516171819202122232425262728293031323334353637383940 |
- package com.zoniot.ccrc.rabbit;
- import com.alibaba.fastjson.JSON;
- import com.rabbitmq.client.Channel;
- import com.zoniot.ccrc.commom.model.CommunityData;
- import com.zoniot.ccrc.service.CommunityService;
- import com.zoniot.ccrc.service.DeviceService;
- import lombok.extern.slf4j.Slf4j;
- import org.springframework.amqp.core.Message;
- import org.springframework.amqp.rabbit.annotation.RabbitListener;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Component;
- import java.io.IOException;
- @Component
- @Slf4j
- public class SyncCommunityReceiver {
- @Autowired
- private CommunityService communityService;
- //@RabbitListener(queues = {"${spring.rabbitmq.community.queue}"},containerFactory = "customContainerFactory")
- public void receiver(Channel channel, Message message) throws IOException {
- try {
- String msg = new String(message.getBody());
- log.info("-----SyncCommunityReceiver msg-----," +msg);
- CommunityData communityData = JSON.parseObject(msg, CommunityData.class);
- // 1,处理消息
- communityService.syncCommunity(communityData);
- // 2,确认消息消费成功
- channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
- } catch (Exception e) {
- channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, false);
- log.error("consumer message error !",e);
- }
- }
- }
|