如果不想用通用的rabbitmq配置 那麼就可以使用SimpleMessageListener自定義消費者
在此隊列中 配置為此方法中的配置 而不是走rabbitmq的通用配置
import org.springframework.amqp.core.AcknowledgeMode;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.amqp.rabbit.listener.api.ChannelAwareMessageListener;
import org.springframework.amqp.support.ConsumerTagStrategy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import com.alibaba.fastjson.JSONObject;
import com.rabbitmq.client.Channel;
import org.springframework.context.annotation.Configuration;
@Configuration
public class SimpleMessageListener {
@Autowired
BusinessConfig businessConfig;
@Bean
public SimpleMessageListenerContainer messageContainer(ConnectionFactory connectionFactory) {
SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(connectionFactory);
// 添加隊列 可以添加多個隊列
//container.setQueues(businessConfig.Queue());
container.setExposeListenerChannel(true);
// 設定目前的消費者數量
// container.setMaxConcurrentConsumers(5);
container.setConcurrentConsumers(1);
// 設定确認模式手工确認
container.setAcknowledgeMode(AcknowledgeMode.MANUAL);
container.setMessageListener(new ChannelAwareMessageListener() {
@Override
public void onMessage(Message message, Channel channel) throws Exception {
byte[] body = message.getBody();
System.out.println("1 receive msg : " + JSONObject.parseObject(new String(body)));
// 不讀取消息并且将目前消息抛棄掉,消息隊列中删除目前消息
// channel.basicReject(message.getMessageProperties().getDeliveryTag(), false);
// 不讀取消息,消息隊列中保留目前消息未被檢視狀态
// channel.basicReject(message.getMessageProperties().getDeliveryTag(), true);
// 确認消息成功消費,删除消息隊列中的消息
// channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
// 确認消息成功消費,删除消息隊列中的消息,他跟上面貌似一樣
channel.basicAck(message.getMessageProperties().getDeliveryTag(), true);
}
});
return container;
}
@Bean
public SimpleMessageListenerContainer messageContainer2(ConnectionFactory connectionFactory) {
SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(connectionFactory);
// 監聽多個queue
//container.setQueues(businessConfig.Queue());
// 設定目前消費者數量
container.setConcurrentConsumers(1);
// 設定最大的消費者數量
container.setMaxConcurrentConsumers(5);
// 設定不要重回隊列
container.setDefaultRequeueRejected(false);
// 設定自動簽收
container.setAcknowledgeMode(AcknowledgeMode.AUTO);
// 設定消費端tag政策
container.setConsumerTagStrategy(new ConsumerTagStrategy() {
@Override
public String createConsumerTag(String queue) {
return queue + "_" + System.currentTimeMillis();
}
});
// 設定監聽
container.setMessageListener(new ChannelAwareMessageListener() {
@Override
public void onMessage(Message message, Channel channel) throws Exception {
// 消息處理
String msg = new String(message.getBody(), "UTF-8");
System.out.println("---消費者---隊列名:" + message.getMessageProperties().getConsumerQueue() + ",消息:" + msg
+ ",deliveryTag:" + message.getMessageProperties().getDeliveryTag());
channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);;
}
});
return container;
}
}