消息发送模式
Rocket MQ发送消息可分为同步发送、异步发送和单向发送
同步发送:发送消息要等待,直到MQ响应
异步发送:发送消息不需要等待
单向发送:发送之后代码继续运行,无需关心发送结果,无需等待MQ响应
消息发送示例
package com.klcwqy.rocketmq;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.exception.RemotingException;
/**
* Rocket MQ发送消息模式
* @author klcwqy
*
*/
public class SendMessageModel {
private static DefaultMQProducer producer = null;
static {
producer = new DefaultMQProducer("SendMessageModel");
producer.setNamesrvAddr("192.168.8.2:9876;192.168.8.3:9876");
try {
producer.start();
//设置发送异步消息失败时的尝试次数,异步消息时打开
//producer.setRetryTimesWhenSendAsyncFailed(0);
} catch (MQClientException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 发送同步消息
* @param message
* @throws Exception
*/
public static void sendMessage(Message message) throws Exception {
//等待发送结果
SendResult sendResult = producer.send(message);
System.out.printf("%s%n",sendResult);
}
/**
* 发送异步消息
* @param message
* @throws Exception
*/
public static void sendAsyncMessage(Message message) throws Exception {
//无需等待结果
producer.send(message, new SendCallback() {
public void onSuccess(SendResult sendResult) {
//发送成功时调用
System.out.printf("%s%n",sendResult);
}
public void onException(Throwable e) {
//发送失败时调用
}
});
}
/**
* 发送单向消息
* @param message
* @throws InterruptedException
* @throws RemotingException
* @throws MQClientException
*/
public static void sendOneWayMessage(Message message) throws Exception {
//无需等待结果
producer.sendOneway(message);
}
}
消息消费模式
消息消费分为拉取和推送模式
拉取模式:客户端主动去MQ拉取消息
推送模式:MQ主动将消息推送到客户端
消息消费示例
1 推送模式
package com.klcwqy.rocketmq;
import java.util.List;
import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.common.message.MessageExt;
public class PushConsume {
public static void main(String[] args) throws Exception {
//创建消费
DefaultMQPushConsumer consume = new DefaultMQPushConsumer("test-consume-group");
//绑定NameServer
consume.setNamesrvAddr("192.168.8.2:9876;192.168.8.3:9876");
//订阅Topic
consume.subscribe(CaseOneProducer.TOPIC, "*");
//注册回调,有消息时mq会推进来
consume.registerMessageListener(new MessageListenerConcurrently() {
public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs, ConsumeConcurrentlyContext context) {
MessageExt messageExt = msgs.get(0);
System.out.println(new String(messageExt.getBody()));
return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
}
});
//启动消费者
consume.start();
}
}
2 拉取模式
package com.klcwqy.rocketmq;
import java.util.List;
import org.apache.rocketmq.client.consumer.DefaultLitePullConsumer;
import org.apache.rocketmq.common.consumer.ConsumeFromWhere;
import org.apache.rocketmq.common.message.MessageExt;
public class PullConsume {
public static void main(String[] args) throws Exception {
//创建消费
DefaultLitePullConsumer consume = new DefaultLitePullConsumer("test-consume-group");
//绑定NameServer
consume.setNamesrvAddr("192.168.8.2:9876;192.168.8.3:9876");
consume.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET);
consume.subscribe("TopicTest", "*");
consume.start();
while (true) {
List<MessageExt> messageExts = consume.poll();
System.out.printf("%s%n", messageExts);
}
}
}