http://blog.csdn.net/tototuzuoquan/article/details/78314572
使用的rocketmq的ip等是上面博文提及的ip等内容
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<groupId>com.toto.rocketmqgroupId>
<artifactId>rocketmq-demoartifactId>
<version>1.0-SNAPSHOTversion>
<dependencies>
<dependency>
<groupId>org.apache.rocketmqgroupId>
<artifactId>rocketmq-storeartifactId>
<version>4.0.0-incubatingversion>
dependency>
<dependency>
<groupId>org.apache.rocketmqgroupId>
<artifactId>rocketmq-clientartifactId>
<version>4.0.0-incubatingversion>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-pluginartifactId>
<version>3.1version>
<configuration>
<source>1.8source>
<target>1.8target>
configuration>
plugin>
<plugin>
<artifactId>maven-assembly-pluginartifactId>
<version>2.4version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependenciesdescriptorRef>
descriptorRefs>
configuration>
<executions>
<execution>
<id>make-assemblyid>
<phase>packagephase>
<goals>
<goal>singlegoal>
goals>
execution>
executions>
plugin>
plugins>
build>
project>
package demo.one;
import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.exception.RemotingException;
import java.util.UUID;
public class Producer {
public static void main(String[] args) {
//生成Producer
DefaultMQProducer producer = new DefaultMQProducer("pro_qch_test");
//配置Producer
producer.setNamesrvAddr("192.168.106.101:9876;192.168.106.102:9876");
producer.setInstanceName(UUID.randomUUID().toString());
//启动Producer
try{
producer.start();
}catch(MQClientException e) {
e.printStackTrace();
return;
}
//生产消息
String str = "Hello RocketMQ!------" + UUID.randomUUID().toString();
Message msg = new Message("qch_20170706",str.getBytes());
try{
producer.send(msg);
} catch(MQClientException | RemotingException | MQBrokerException | InterruptedException e){
e.printStackTrace();
return;
}
//停止Producer
producer.shutdown();
System.out.println("[-----------]Success\n");
}
}
package demo.one;
import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.exception.RemotingException;
import java.util.UUID;
public class ProducerTest {
private static DefaultMQProducer producer = null;
public static void main(String[] args) {
System.out.print("[----------]Start\n");
int pro_count = 1;
if (args.length > 0) {
pro_count = Integer.parseInt(args[0]);
}
boolean result = false;
try {
ProducerStart();
for (int i = 0; i < pro_count; i++) {
String msg = "hello rocketmq "+ i + "----" + UUID.randomUUID().toString();
SendMessage("qch_20170706", msg);
System.out.print(msg + "\n");
}
}finally {
producer.shutdown();
}
System.out.print("[----------]Succeed\n");
}
private static boolean ProducerStart() {
producer = new DefaultMQProducer("pro_qch_test");
producer.setNamesrvAddr("192.168.106.101:9876;192.168.106.102:9876");
producer.setInstanceName(UUID.randomUUID().toString());
try {
producer.start();
} catch(MQClientException e) {
e.printStackTrace();
return false;
}
return true;
}
private static boolean SendMessage(String topic, String str) {
Message msg = new Message(topic, str.getBytes());
try {
producer.send(msg);
} catch (MQClientException | RemotingException | MQBrokerException | InterruptedException e) {
e.printStackTrace();
return false;
}
return true;
}
}
通过这个类实现批量发送消息
在此类中args参数需要输入,接下来idea中模拟输入参数:
运行结果:
package demo.one;
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.consumer.ConsumeFromWhere;
import org.apache.rocketmq.common.message.MessageExt;
import java.util.List;
import java.util.UUID;
public class ConsumerTest {
public static void main(String[] args) {
DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("con_qch_test");
consumer.setInstanceName(UUID.randomUUID().toString());
consumer.setConsumeMessageBatchMaxSize(32);
consumer.setNamesrvAddr("192.168.106.101:9876;192.168.106.102:9876");
consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET);
consumer.registerMessageListener(new MessageListenerConcurrently() {
@Override
public ConsumeConcurrentlyStatus consumeMessage(
List list, ConsumeConcurrentlyContext consumeConcurrentlyContext) {
for(MessageExt me : list) {
System.out.print(new String(me.getBody()) + "\n");
}
System.out.println("=====================================");
return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
}
});
try {
consumer.subscribe("qch_20170706", "*");
consumer.start();
} catch (Exception e) {
e.printStackTrace();
}
}
}
在rocketmq里,consumer被分为2类:MQPullConsumer和MQPushConsumer,其实本质都是拉模式(pull),即consumer轮询从broker拉取消息。
区别是:
push方式里,consumer把轮询过程封装了,并注册MessageListener监听器,取到消息后,唤醒MessageListener的consumerMessage()来消费,对用户而言,感觉消息是被推送过来的。
pull方式里,取消息的过程需要用户自己写,首先通过打算消费的Topic拿到MessageQueue的集合,遍历MessageQueue集合,然后针对每个MessageQueue批量取消息,一次取完后,记录该队列下一次要取的开始offset,直到取完了,再换另一个MessageQueue.
package demo.one;
import org.apache.rocketmq.client.consumer.DefaultMQPullConsumer;
import org.apache.rocketmq.client.consumer.PullResult;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.common.message.MessageQueue;
import org.apache.rocketmq.common.protocol.heartbeat.MessageModel;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
public class PullConsumer {
private static final Map OFFSE_TABLE = new HashMap<>();
public static void main(String[] args) {
DefaultMQPullConsumer consumer = new DefaultMQPullConsumer("consumerGroup");
consumer.setInstanceName(UUID.randomUUID().toString());
consumer.setMessageModel(MessageModel.BROADCASTING);
consumer.setNamesrvAddr("192.168.106.101:9876;192.168.106.102:9876");
try {
consumer.start();
} catch (MQClientException e) {
e.printStackTrace();
}
try {
Set mqs = consumer.fetchSubscribeMessageQueues("qch_20170706");
for(MessageQueue mq : mqs) {
System.out.println("Consume from the queue: " + mq + "%n");
SINGLE_MQ:
while(true) {
try {
PullResult pullResult = consumer.pullBlockIfNotFound(mq,null,
getMessageQueueOffset(mq),32);
System.out.printf("%s%n", pullResult);
putMessageQueueOffset(mq,pullResult.getNextBeginOffset());
switch (pullResult.getPullStatus()) {
case FOUND:
break;
case NO_MATCHED_MSG:
break;
case NO_NEW_MSG:
break SINGLE_MQ;
case OFFSET_ILLEGAL:
break;
default:
break;
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
} catch (MQClientException e) {
e.printStackTrace();
}
}
private static long getMessageQueueOffset(MessageQueue mq) {
Long offset = OFFSE_TABLE.get(mq);
if (offset != null)
return offset;
return 0;
}
private static void putMessageQueueOffset(MessageQueue mq, long offset) {
OFFSE_TABLE.put(mq, offset);
}
}
package demo.one;
import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
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.consumer.ConsumeFromWhere;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.common.protocol.heartbeat.MessageModel;
import java.util.UUID;
public class PushConsumer {
public static void main(String[] args) throws MQClientException, InterruptedException {
//生成Consumer
DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("con_group_1");
//配置Consumer
consumer.setInstanceName(UUID.randomUUID().toString());
consumer.setMessageModel(MessageModel.BROADCASTING);
consumer.setConsumeMessageBatchMaxSize(32);
consumer.setNamesrvAddr("192.168.106.101:9876;192.168.106.102:9876");
consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET);
consumer.registerMessageListener(
(MessageListenerConcurrently)(list, consumeConcurrentlyContext) -> {
//消费消息
for(MessageExt me : list) {
System.out.print("msg=" + new String(me.getBody()) + "\n");
}
return ConsumeConcurrentlyStatus.CONSUME_SUCCESS; });
//启动Consumer
consumer.subscribe("qch_20170706", "*");
consumer.start();
//停止Consumer
Thread.sleep(60000);
consumer.shutdown();
}
}
package demo.one;
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.consumer.ConsumeFromWhere;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.common.protocol.heartbeat.MessageModel;
import java.util.List;
import java.util.UUID;
public class PushConsumerTest {
private static int count = 0;
public static void main(String[] args) {
System.out.print("Push Consumer main start!\n");
count = 0;
DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("con_group_1");
consumer.setInstanceName(UUID.randomUUID().toString());
consumer.setMessageModel(MessageModel.BROADCASTING);
consumer.setConsumeMessageBatchMaxSize(32);
consumer.setNamesrvAddr("192.168.106.101:9876;192.168.106.102:9876");
consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET);
consumer.registerMessageListener(new MessageListenerConcurrently() {
@Override
public ConsumeConcurrentlyStatus consumeMessage(
List list, ConsumeConcurrentlyContext consumeConcurrentlyContext) {
System.out.print("list count=" + list.size() + "\n");
for(MessageExt me : list) {
count ++;
System.out.print("count=" + count + ", msg=" + new String(me.getBody()) + "\n");
}
return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
}
});
try {
consumer.subscribe("qch_20170706", "*");
consumer.start();
System.out.print("Push Consumer Started!\n");
} catch (Exception e) {
e.printStackTrace();
}
}
}
package demo.one;
import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.common.consumer.ConsumeFromWhere;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.common.protocol.heartbeat.MessageModel;
import java.util.UUID;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
public class PushConsumerThreadPollTest {
public static void main(String[] args){
int threadCount = 3;
int waitTime = 60000;
ExecutorService executor = Executors.newFixedThreadPool(threadCount);
for(int i = 0; i < threadCount; i++) {
Runnable runner = new ExecutorThread(String.valueOf(i));
executor.execute(runner);
}
try {
Thread.sleep(60000);
executor.shutdown();
executor.awaitTermination(waitTime, TimeUnit.MICROSECONDS);
} catch (InterruptedException e) {}
}
}
class ExecutorThread implements Runnable {
private String name = "";
private int count = 0;
ExecutorThread(String name) {
this.name = name;
}
@Override
public void run() {
StartPushConsumer();
}
private void StartPushConsumer() {
System.out.print("Consumer Name=" + name + "\n");
count = 0;
DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("con_group_1");
consumer.setInstanceName(UUID.randomUUID().toString());
//广播消费
//consumer.setMessageModel(MessageModel.BROADCASTING);
//集群消费
consumer.setMessageModel(MessageModel.CLUSTERING);
consumer.setConsumeMessageBatchMaxSize(32);
consumer.setNamesrvAddr("192.168.106.101:9876;192.168.106.102:9876");
consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET);
consumer.registerMessageListener((MessageListenerConcurrently) (list, consumeConcurrentlyContext) -> {
System.out.print("list count=" + list.size() + "\n");
for(MessageExt me : list) {
count ++;
System.out.print("name=" + name + ", count=" + count + ", msg=" + new String(me.getBody()) + "\n");
}
return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
});
try {
consumer.subscribe("qch_20170706", "*");
consumer.start();
System.out.print("Consumer started. name=" + name + "\n");
} catch (Exception e) {
e.printStackTrace();
}
}
}
package demo.two;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
public class Producer {
public static void main(String[] args) throws MQClientException {
/**
* 一个应用创建一个Producer,由应用来维护此对象,可以设置为全局对象或者单例
* 注意:ProducerGroupName需要由应用来保证唯一
* ProducerGroup这个概念发送普通的消息时,作用不大,但是发送分布式事务消息时,比较关键,
* 因为服务器会回查这个Group下的任意一个Producer
*/
DefaultMQProducer producer = new DefaultMQProducer("ProducerGroupName");
/**
* Producer对象在使用之前必须要调用start初始化,初始化一次即可
* 注意:切记不可以在每次发送消息时,都调用start方法。
*/
producer.setNamesrvAddr("192.168.106.101:9876;192.168.106.102:9876");
producer.start();
/**
* 下面这段代码表明一个Producer对象可以发送多个topic,多个tag的消息。
* 注意:send方法是同步调用,只要不抛异常就标识成功。但是发送成功也可会有多种状态,
* 例如消息写入Master成功,但是Slave不成功,这种情况消息属于成功,但是对于个别应用如果对消息可靠性要求极高,
* 需要对这种情况做处理。另外,消息可能会存在发送失败的情况,失败重试由应用来处理。
*/
try {
{
Message msg = new Message("TopicTest1",// topic
"TagA",// tag
"OrderID001",// key
("Hello MetaQ").getBytes());// body
SendResult sendResult = producer.send(msg);
System.out.println(sendResult);
}
{
Message msg = new Message("TopicTest2",// topic
"TagB",// tag
"OrderID0034",// key
("Hello MetaQ").getBytes());// body
SendResult sendResult = producer.send(msg);
System.out.println(sendResult);
}
{
Message msg = new Message("TopicTest3",// topic
"TagC",// tag
"OrderID061",// key
("Hello MetaQ").getBytes());// body
SendResult sendResult = producer.send(msg);
System.out.println(sendResult);
}
} catch (Exception e){
e.printStackTrace();
}
/**
* 应用退出时,要调用shutdown来清理资源,关闭网络连接,从MetaQ服务器上注销自己
* 注意:我们建议应用在JBoss、Tomcat等容器的退出钩子里调用shutdown方法。
*/
producer.shutdown();
}
}
package demo.two;
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.consumer.ConsumeFromWhere;
import org.apache.rocketmq.common.message.MessageExt;
import java.util.List;
public class Consumer {
/**
* 当前粒子是PushConsumer用法,使用方式给用户感觉消息从RocketMQ服务器推到了应用客户端。
* 但是实际PushConsumer内部是使用长轮询Pull方法从Broker拉消息,然后再回调用户Listener方法。
* @param args
*/
public static void main(String[] args) throws MQClientException {
/**
* 一个应用创建一个Consumer,由应用来维护此对象,可以设置为全局对象或者单例
* 注意:ConsumerGroupName需要由应用来保证唯一
*/
DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("ConsumerGroupName");
consumer.setNamesrvAddr("192.168.106.101:9876;192.168.106.102:9876");
/**
* 订阅指定topic下tags分别等于TagA或tagC或TagD
*/
consumer.subscribe("TopicTest1", "TagA || TagC || TagD");
/**
* 订阅指定topic下所有消息
* 注意:一个consumer对象可以订阅多个topic
*/
consumer.subscribe("TopicTest2", "*");
consumer.subscribe("TopicTest3","*");
/**
* 设置Consumer第一次启动是从队列头部开始消费还是队列尾部开始消费
* 如果非第一次启动,那么按照上次消费的位置继续消费
*/
consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET);
consumer.registerMessageListener(new MessageListenerConcurrently() {
@Override
public ConsumeConcurrentlyStatus consumeMessage(List msgs, ConsumeConcurrentlyContext context) {
System.out.println(Thread.currentThread().getName() + " Receive New Messages:" + msgs);
MessageExt msg = msgs.get(0);
System.out.println(new String(msg.getBody()));
if (msg.getTopic().equals("TopicTest1")) {
//执行TopicTest1的消费逻辑
if (msg.getTopic() != null && msg.getTags().equals("TagA")) {
//执行TagA的消费
System.out.println("执行TagA的消费");
} else if(msg.getTags() != null && msg.getTags().equals("TagC")) {
//执行TagC的消费
System.out.println("执行TagC的消费");
} else if (msg.getTags() != null && msg.getTags().equals("TagD")) {
//执行TagD的消费
System.out.println("执行TagD的消费");
}
} else if(msg.getTopic().equals("TopicTest2")) {
//执行TopicTest2的消费逻辑
System.out.println("执行TopicTest2的消费逻辑");
} else if (msg.getTopic().equals("TopicTest3")){
System.out.println("执行TopicTest3的消费逻辑");
}
return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
}
});
/**
* Consumer对象在使用之前必须调用start初始化,初始化一次即可
*/
consumer.start();
System.out.println("Consumer Started.");
}
}
package demo.three;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import java.util.Date;
public class Producer {
public static void main(String[] args) throws MQClientException {
DefaultMQProducer producer = new DefaultMQProducer("rmq-group");
/**
* 默认情况下,一台服务器只能启动一个Producer或Consumer实例,所以如果需要在
* 一台服务器启动多个实例,需要设置实例的名称。
*/
producer.setNamesrvAddr("192.168.106.101:9876;192.168.106.102:9876");
producer.setInstanceName("producer");
producer.start();
try {
for (int i = 0; i < 10; i++) {
Thread.sleep(1000); //每秒发送一次MQ
Message msg = new Message(
"TopicA-test", //topic
"TagA", //tag
(new Date() + "Hello RocketMQ,QuickStart" + i).getBytes());
SendResult sendResult = producer.send(msg);
System.out.println(sendResult);
}
} catch (Exception e) {
e.printStackTrace();
}
producer.shutdown();
}
}
package demo.three;
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 MQClientException {
DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("rmq-group");
consumer.setNamesrvAddr("192.168.106.101:9876;192.168.106.102:9876");
consumer.setInstanceName("consumer");
consumer.subscribe("TopicA-test", "TagA");
consumer.registerMessageListener(new MessageListenerConcurrently() {
@Override
public ConsumeConcurrentlyStatus consumeMessage(
List msgs, ConsumeConcurrentlyContext context) {
for (MessageExt msg : msgs) {
System.out.println(new String(msg.getBody()));
}
return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
}
});
consumer.start();
System.out.println("Consumer Started.");
}
}