分布式开放消息系统(RocketMQ)的原理与实践
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.MessageQueueSelector;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.common.message.MessageQueue;
public class Producer {
public static void main(String[] args) throws MQClientException, InterruptedException {
// 声明并初始化一个producer
// 需要一个producer group名字作为构造方法的参数,这里为producer1
DefaultMQProducer producer = new DefaultMQProducer("order_producer");
producer.setNamesrvAddr("localhost:9876");
//producer.createTopic(key, newTopic, queueNum);
producer.start();
//String[] tags = new String[] {"TagA","TagC","TagD"};
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateStr = sdf.format(date);
for (int i = 1; i <= 5; i++) {
try {
// 时间戳
String body = dateStr + " order_0 " + i;
Message msg = new Message("TopicTest",// topic
//tags[i%tags.length],// tag
"order_0",// tag
"KEY"+i,
body.getBytes()// body
);
SendResult sendResult = producer.send(msg,new MessageQueueSelector() {
@Override
public MessageQueue select(List mqs, Message msg, Object arg) {
Integer id = (Integer)arg;
return mqs.get(id);
}
// 0是队列的下标
},0);
System.out.println(sendResult + ", body:" + body);
} catch (Exception e) {
e.printStackTrace();
Thread.sleep(1000);
}
}
for (int i = 1; i <= 5; i++) {
try {
// 时间戳
String body = dateStr + " order_1 " + i;
Message msg = new Message("TopicTest",// topic
//tags[i%tags.length],// tag
"order_1",// tag
"KEY"+i,
body.getBytes()// body
);
SendResult sendResult = producer.send(msg,new MessageQueueSelector() {
@Override
public MessageQueue select(List mqs, Message msg, Object arg) {
Integer id = (Integer)arg;
return mqs.get(id);
}
// 1是队列的下标
},1);
System.out.println(sendResult + ", body:" + body);
} catch (Exception e) {
e.printStackTrace();
Thread.sleep(1000);
}
}
for (int i = 1; i <= 5; i++) {
try {
// 时间戳
String body = dateStr + " order_2 " + i;
Message msg = new Message("TopicTest",// topic
//tags[i%tags.length],// tag
"order_2",// tag
"KEY"+i,
body.getBytes()// body
);
SendResult sendResult = producer.send(msg,new MessageQueueSelector() {
@Override
public MessageQueue select(List mqs, Message msg, Object arg) {
Integer id = (Integer)arg;
return mqs.get(id);
}
// 2是队列的下标
},2);
System.out.println(sendResult + ", body:" + body);
} catch (Exception e) {
e.printStackTrace();
Thread.sleep(1000);
}
}
producer.shutdown();
}
}
import java.util.List;
import java.util.Random;
import java.util.concurrent.TimeUnit;
import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeOrderlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeOrderlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerOrderly;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.common.consumer.ConsumeFromWhere;
import org.apache.rocketmq.common.message.MessageExt;
public class Consumer1 {
public static void main(String[] args) throws InterruptedException, MQClientException {
DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("order_consumer");
consumer.setNamesrvAddr("localhost:9876");
/**
* 设置Consumer第一次启动是从队列头部开始消费还是队列尾部开始消费
* 如果非第一次启动,那么按照上次消费的位置继续消费
*/
consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET);
/**消费线程池最小数量:默认10**/
consumer.setConsumeThreadMin(10);
/**消费线程池最大数量:默认120**/
consumer.setConsumeThreadMax(20);
// 订阅的主题,以及过滤的标签内容
consumer.subscribe("TopicTest", "*");
//设置一个Listener,主要进行消息的逻辑处理
consumer.registerMessageListener(new MessageListenerOrderly() {
private Random random = new Random();
@Override
public ConsumeOrderlyStatus consumeMessage(List msgs, ConsumeOrderlyContext context) {
// 设置自动提交
context.setAutoCommit(true);
for(MessageExt msg: msgs) {
System.out.println(msg + ",content:" + new String(msg.getBody()));
}
try {
TimeUnit.SECONDS.sleep(random.nextInt(5));
} catch (InterruptedException e) {
e.printStackTrace();
}
return ConsumeOrderlyStatus.SUCCESS;
}
});
//调用start()方法启动consumer
consumer.start();
System.out.println("C1 Started.");
}
}
import java.util.List;
import java.util.Random;
import java.util.concurrent.TimeUnit;
import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeOrderlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeOrderlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerOrderly;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.common.consumer.ConsumeFromWhere;
import org.apache.rocketmq.common.message.MessageExt;
public class Consumer2 {
public static void main(String[] args) throws InterruptedException, MQClientException {
DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("order_consumer");
consumer.setNamesrvAddr("localhost:9876");
/**
* 设置Consumer第一次启动是从队列头部开始消费还是队列尾部开始消费
* 如果非第一次启动,那么按照上次消费的位置继续消费
*/
consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET);
/**消费线程池最小数量:默认10**/
consumer.setConsumeThreadMin(10);
/**消费线程池最大数量:默认120**/
consumer.setConsumeThreadMax(20);
// 订阅的主题,以及过滤的标签内容
consumer.subscribe("TopicTest", "*");
//设置一个Listener,主要进行消息的逻辑处理
consumer.registerMessageListener(new MessageListenerOrderly() {
private Random random = new Random();
@Override
public ConsumeOrderlyStatus consumeMessage(List msgs, ConsumeOrderlyContext context) {
// 设置自动提交
context.setAutoCommit(true);
for(MessageExt msg: msgs) {
System.out.println(msg + ",content:" + new String(msg.getBody()));
}
try {
TimeUnit.SECONDS.sleep(random.nextInt(5));
} catch (InterruptedException e) {
e.printStackTrace();
}
return ConsumeOrderlyStatus.SUCCESS;
}
});
//调用start()方法启动consumer
consumer.start();
System.out.println("C2 Started.");
}
}