1.1消息发送方式
RocketMQ支持3种消息发送方式 :同步(sync)、异步(async)、单向(oneway)
同步:发送者向MQ执行发送消息API时,同步等待,直到消息服务器返回发送结果。
异步:发送者向MQ执行发送消息API时,指定消息发送成功后的回调函数,然后调用消息发送API后,立即返回,消息发送者线程不阻塞,直到运行结束,消息发送成功或失败的回调任务在一个新的线程中执行。
单向:消息发送者向MQ执行发送消息API时,直接返回,不等待消息服务器的结果,也不注册回调函数,简单地说,就是只管发,不在乎消息是否成功存储在消息服务器上。
1.2同步发送示例
package com.snail.study.rocketmq;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.common.RemotingHelper;
public class ProduceSync {
public static void main(String[] args) throws Exception {
// 创建produce group
DefaultMQProducer producer = new DefaultMQProducer("produceSync");
//指定name service 地址
producer.setNamesrvAddr("localhost:9876");
//启动实例
producer.start();
//构建消息,这里准备发送10条消息
for (int i = 0; i <10 ; i++) {
Message message = new Message(
"TopicTest" /*Topic*/,
"produceSyncTag"/*Tag*/,
("rocketmq hello "+i).getBytes(RemotingHelper.DEFAULT_CHARSET)/*message body*/
);
//逐条同步发送消息给其中一个borker
SendResult result = producer.send(message);
System.out.printf("%s%n", result);
}
//关闭实例
producer.shutdown();
}
}
1.3异步发送示例
package com.snail.study.rocketmq;
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.common.RemotingHelper;
public class ProducerAsync {
public static void main(String[] args) throws Exception {
// 创建produce group
DefaultMQProducer producer = new DefaultMQProducer("producerAsync");
//指定name service 地址
producer.setNamesrvAddr("localhost:9876");
//启动实例
producer.start();
//设置异步发送失败重试次数
producer.setRetryTimesWhenSendAsyncFailed(0);
//构建消息,这里准备发送10条消息
for (int i = 0; i <10 ; i++) {
int index=i;
Message message = new Message(
"TopicTest" /*Topic*/,
"produceAsyncTag"/*Tag*/,
("rocketmq hello "+i).getBytes(RemotingHelper.DEFAULT_CHARSET)/*message body*/
);
//异步发送并等待成功或失败返回输出相关信息
producer.send(message, new SendCallback() {
@Override
public void onSuccess(SendResult sendResult) {
System.out.printf("%-10d OK %s %n", index,
sendResult.getMsgId());
}
@Override
public void onException(Throwable e) {
System.out.printf("%-10d Exception %s %n", index, e);
e.printStackTrace();
}
});
}
//关闭实例
producer.shutdown();
}
}
1.4单向发送示例
package com.snail.study.rocketmq;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.common.RemotingHelper;
public class ProducerOneway {
public static void main(String[] args) throws Exception{
// 创建produce group
DefaultMQProducer producer = new DefaultMQProducer("producerOneway");
//指定name service 地址
producer.setNamesrvAddr("localhost:9876");
//启动实例
producer.start();
//构建消息,这里准备发送10条消息
for (int i = 0; i < 10; i++) {
Message msg = new Message("TopicTest" /* Topic */,
"producerOnewayTag" /* Tag */,
("rocketmq hello "+i).getBytes(RemotingHelper.DEFAULT_CHARSET) /* Message body */
);
//逐条oneway发送消息给其中一个borker
producer.sendOneway(msg);
}
//关闭实例
producer.shutdown();
}
}
1.5消息订阅示例
package com.snail.study.rocketmq;
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.client.exception.MQClientException;
import org.apache.rocketmq.common.message.MessageExt;
import java.util.List;
public class Consumer {
public static void main(String[] args) throws InterruptedException, MQClientException {
// 创建consumer group.
DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("Consumer");
//指定name service 地址
consumer.setNamesrvAddr("localhost:9876");
// 订阅一个主题以进行消费
consumer.subscribe("TopicTest", "*");
// Register callback to execute on arrival of messages fetched from brokers.
// 监听从brokers中获取消息后执行消息处理方法
consumer.registerMessageListener(new MessageListenerConcurrently() {
@Override
public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs,
ConsumeConcurrentlyContext context) {
System.out.printf("%s Receive New Messages: %s %n", Thread.currentThread().getName(), msgs);
return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
}
});
//启动consumer实例
consumer.start();
System.out.printf("Consumer Started.%n");
}
}