1.push mode (at the client (consumer) set listener, rabbitmq service to the end consumer push messages)
package com.pld.content.manager.controller.mq;
import com.pld.content.manager.config.RabbitConfigCommon;
import com.pld.content.manager.domain.service.backend.AdvertisingService;
import com.pld.content.manager.domain.service.backend.UserArticleService;
import com.pld.content.manager.dto.advertising.req.AdvertisingOffDTO;
import com.pld.content.manager.dto.advertising.req.AdvertisingUpDTO;
import com.pld.content.manager.dto.article.req.ArticleOffDTO;
import com.pld.content.manager.dto.article.req.ArticleUpDTO;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.io.IOException;
/**
* @author tlj
* @date 2019/12/31
*/
@Service
@Slf4j
public class TriggerConsumer {
@Autowired
private UserArticleService userArticleService;
@Autowired
private AdvertisingService advertisingService;
@RabbitListener(queues = RabbitConfigCommon.TRIGGER_TIMED_ROUTING_KEY_ADV_OFF, containerFactory = "rabbitListenerContainerFactory")
@RabbitHandler
public void processTriggerAdvOff(AdvertisingOffDTO taskInfo, Channel channel, Message message) throws IOException {
try{
// 获取消息Id
/*String messageId = message.getMessageProperties().getMessageId();
GetResponse response = channel.basicGet("dlx.queue", false);
response.getBody()
channel.basicAck(response .getEnvelope() .getDeliveryTag() ,false);
response.getProps().getMessageId()
String message = new String(response.getBody(), "UTF-8");*/
advertisingService.putOffAdvertising(taskInfo);
// todo 异常处理 把消息从队列清掉 否则一直循环消费!
}catch (Exception e){
log.error("{}",e);
// 处理异常:拒绝消息,流入最终死信队列
channel.basicReject(message.getMessageProperties().getDeliveryTag(),false);
return;
}
// 正常流程:返回ack,删除消息
channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
}
}
2.poll modes: client actively reading
package com.pld.content.manager.controller.mq;
import com.pld.content.manager.config.RabbitConfigCommon;
import com.pld.content.manager.config.RabbitConfigForAdvOff;
import com.pld.content.manager.domain.service.backend.AdvertisingService;
import com.pld.content.manager.domain.service.backend.UserArticleService;
import com.pld.content.manager.dto.advertising.req.AdvertisingOffDTO;
import com.pld.content.manager.dto.advertising.req.AdvertisingUpDTO;
import com.pld.content.manager.dto.article.req.ArticleOffDTO;
import com.pld.content.manager.dto.article.req.ArticleUpDTO;
import com.rabbitmq.client.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
/**主动拉取消息消费者
* @author tlj
* @date 2019/12/31
*/
@Service
@Slf4j
public class ActiveConsumer {
@Value("${spring.rabbitmq.password}")
private String password;
@Autowired
private RabbitTemplate rabbitTemplate;
public void cs1() throws IOException, TimeoutException {
//创建连接工厂
ConnectionFactory factory = new ConnectionFactory();
factory.setHost(rabbitTemplate.getConnectionFactory().getHost());
factory.setPort(rabbitTemplate.getConnectionFactory().getPort());
factory.setUsername(rabbitTemplate.getConnectionFactory().getUsername());
factory.setPassword(password);
Connection connection = factory.newConnection();
// 创建消息信道
Channel channel = connection.createChannel();
GetResponse response = channel.basicGet(RabbitConfigCommon.TRIGGER_REQ_ROUTING_KEY_ADV_OFF, false);
String messageId = response.getProps().getMessageId();
if(Long.valueOf(messageId)%2==0){
channel.basicAck(response.getEnvelope().getDeliveryTag() ,false);
return;
}else{
channel.abort();
}
AMQP.Queue.DeclareOk declareOk = channel.queueDeclarePassive(RabbitConfigCommon.TRIGGER_REQ_ROUTING_KEY_ADV_OFF);
// 获取队列中的消息个数
int sum = declareOk.getMessageCount();
// channel.basicAck(response.getEnvelope().getDeliveryTag() ,false);
// response
}
}