主要内容
生产者使用
一个组只能有一个生产者主名称
topic默认4个队列
异常
public class Const {
public static final String NAMESRV_ADDR_SINGLE = "192.168.11.81:9876";
public static final String NAMESRV_ADDR_MASTER_SLAVE = "192.168.11.81:9876;192.168.11.82:9876";
}
public class Producer {
public static void main(String[] args) throws MQClientException, RemotingException, MQBrokerException, InterruptedException {
DefaultMQProducer producer = new DefaultMQProducer("test_quick_producer_name");
producer.setNamesrvAddr(Const.NAMESRV_ADDR_MASTER_SLAVE);
producer.start();
for(int i = 0 ; i <5; i ++) {
// 1. 创建消息
Message message = new Message("test_quick_topic", // 主题
"TagA", // 标签
"key" + i, // 用户自定义的key ,唯一的标识
("Hello RocketMQ" + i).getBytes()); // 消息内容实体(byte[])
// 2.1 同步发送消息
// if(i == 1) {
// message.setDelayTimeLevel(3);
// }
SendResult sr = producer.send(message, new MessageQueueSelector() {
@Override
public MessageQueue select(List<MessageQueue> mqs, Message msg, Object arg) {
Integer queueNumber = (Integer)arg;
return mqs.get(queueNumber);
}
}, 2);
System.err.println(sr);
// SendResult sr = producer.send(message);
// SendStatus status = sr.getSendStatus();
// System.err.println(status);
//System.err.println("消息发出: " + sr);
// 2.2 异步发送消息
// producer.send(message, new SendCallback() {
// //rabbitmq急速入门的实战: 可靠性消息投递
// @Override
// public void onSuccess(SendResult sendResult) {
// System.err.println("msgId: " + sendResult.getMsgId() + ", status: " + sendResult.getSendStatus());
// }
// @Override
// public void onException(Throwable e) {
// e.printStackTrace();
// System.err.println("------发送失败");
// }
// });
}
producer.shutdown();
}
}
RemotingException --发送过程消息中断
MQBrokerException --broker没有存储成功
InterruptedException --多线程打断
消费者使用
25:40
15次重试,底层定时任务
public class Consumer {
public static void main(String[] args) throws MQClientException {
DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("test_quick_consumer_name");
consumer.setNamesrvAddr(Const.NAMESRV_ADDR_MASTER_SLAVE);
consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_LAST_OFFSET);
consumer.subscribe("test_quick_topic", "*"); //支持消息过滤
consumer.registerMessageListener(new MessageListenerConcurrently() {
@Override
public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs, ConsumeConcurrentlyContext context) {
MessageExt me = msgs.get(0);
try {
String topic = me.getTopic();
String tags = me.getTags();
String keys = me.getKeys();
// if(keys.equals("key1")) {
// System.err.println("消息消费失败..");
// int a = 1/0;
// }
String msgBody = new String(me.getBody(), RemotingHelper.DEFAULT_CHARSET);
System.err.println("topic: " + topic + ",tags: " + tags + ", keys: " + keys + ",body: " + msgBody);
} catch (Exception e) {
e.printStackTrace();
// int recousumeTimes = me.getReconsumeTimes();
// System.err.println("recousumeTimes: " + recousumeTimes);
// if(recousumeTimes == 3) { //重试多少次记录id
// // 记录日志....
// // 做补偿处理
// return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
// }
return ConsumeConcurrentlyStatus.RECONSUME_LATER;
}
return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
}
});
consumer.start();
System.err.println("consumer start...");
}