private static final String tagsName = "MY_TEST_01||mytest";//代表获取Tags为MY_TEST_01、mytest的消息
private static final int consumeThreadMin = 3;
private static final int consumeThreadMax = 5;
@Autowiredprivate
MsgListenerConcurrently msgListenerConcurrently;
/** * Spring bean init-method */
public void init() throws MQClientException {
try {
// 参数信息log.info("DefaultMQPushConsumer初始化!");
log.info("consumerGroup:"+consumerGroup);
log.info("namesrvAddr:"+namesrvAddr);
log.info("topicName:"+topicName);
log.info("tagsName:"+tagsName);
// 一个应用创建一个Consumer,由应用来维护此对象,可以设置为全局对象或者单例
// 注意:ConsumerGroupName需要由应用来保证唯一
consumer = new DefaultMQPushConsumer(consumerGroup);
consumer.setNamesrvAddr(namesrvAddr);
consumer.setInstanceName(String.valueOf(System.currentTimeMillis()));// 订阅指定topic下tags等于tag
consumer.subscribe(topicName, tagsName);
//MessageModel.CLUSTERING 集群消费 :只能有一个消费者消费一次 ,MessageModel.BROADCASTING 广播模式 所有的消费者都会消费一次
consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET);
consumer.setMessageModel(MessageModel.CLUSTERING);
consumer.registerMessageListener(msgListenerConcurrently);
consumer.setConsumeThreadMin(consumeThreadMin);
consumer.setConsumeThreadMax(consumeThreadMax);
consumer.start();log.info("DefaultMQPushConsumer初始化成功!");
} catch (Exception ex) {
log.error("DefaultMQPushConsumer初始化异常=" + ex.getMessage());ex.printStackTrace();
}
}
/** * Spring bean destroy-method */
public void destory() {
consumer.shutdown();
}
/** * @param args */
public static void main(String[] args) {
/ / TODO Auto-generated method stub
}
}
package com.cluster.mq;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;
import com.alibaba.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import com.alibaba.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import com.alibaba.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import com.alibaba.rocketmq.common.message.MessageExt;
import com.bwdz.fpt.common.xml.XmlUtil;
/**
* PushConsumer回调Listener方法
*
*/
@SuppressWarnings("unchecked")
public class MsgListenerConcurrently implements MessageListenerConcurrently {
private static final Logger log = Logger
.getLogger(MsgListenerConcurrently.class);
public ConsumeConcurrentlyStatus consumeMessage(List
ConsumeConcurrentlyContext context) {
for (MessageExt msg : msgs) {
String body = "";
String tags = msg.getTags();
String topic = msg.getTopic();
log.info("【tags:"+tags+",topic:"+topic+"】");
try {
body = new String(msg.getBody(), "UTF-8");
System.out.println("【MSG:"+body+"】");
//Map map = XmlUtil.Dom2Map(body);
if("topic".equals(topic)&&"A".equals(tags)){
System.out.println("A===============");
}else if("topic".equals(topic)&&"B".equals(tags)){
System.out.println("B===============");
}
return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
} catch (Exception e) {
String delay = msg.getProperty("DELAY");
if (delay != null && Integer.parseInt(delay) > 4) {// 重试次数超过3次丢弃此条消息
log.warn("消息重试次数过多,不再消费:" + body);
return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
}
log.error("mq消息消费异常,本条消息是:" + body);
e.printStackTrace();
}
}
return ConsumeConcurrentlyStatus.RECONSUME_LATER;
}
}
2、将上面创建的RocketMQ消费者工具类集成到Spring中,在spring配置文件中进行如下配置:
五、接下是检验成败的时候了
1、先启动好RocketMQ集群(具体请查看 RocketMQ集群解决方案),然后启动好ConsumerProject
2、单元运行ProducerProject工程 RocketMqProducer.java Main函数,执行后,ProducerProject工程控制台打印如下信息,说明通讯正常,然后再查看ConsumerProject就会收到发送的报文信息。
16-11-24 14:06:18 INFO RocketMqProducer:63 - MSG_ID:C0A8718A00002A9F000000000000EC08,SendStatus:SEND_OK
16-11-24 14:06:18 INFO RocketMqProducer:63 - MSG_ID:C0A8718A00002A9F000000000000EEEA,SendStatus:SEND_OK
16-11-24 14:06:18 INFO RocketMqProducer:63 - MSG_ID:C0A8718A00002A9F000000000000F1CC,SendStatus:SEND_OK
16-11-24 14:06:18 INFO RocketMqProducer:63 - MSG_ID:C0A8718A00002A9F000000000000F4AE,SendStatus:SEND_OK
16-11-24 14:06:18 INFO RocketMqProducer:63 - MSG_ID:C0A8718A00002A9F000000000000F790,SendStatus:SEND_OK