springboot:整合rocketmq
一、简易消息操作
生产者整合mq
导入依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.apache.rocketmq</groupId>
<artifactId>rocketmq-spring-boot-starter</artifactId>
<version>2.1.1</version>
</dependency>
</dependencies>
application.properties
server.port=8080
rocketmq.name-server=127.0.0.1:9876
rocketmq.producer.group=my-producer
生产者发送消息
@RestController
@RequestMapping("/mqMessageController")
public class MqMessageController {
private static final Logger log = LoggerFactory.getLogger(MqMessageController.class);
@Autowired
private RocketMQTemplate rocketMQTemplate;
@RequestMapping("/pushMessage.action")
public String get(@RequestParam("id") int id) {
rocketMQTemplate.convertAndSend("first-topic","你好,Java旅途" + id);
return "OK";
}
}
测试生产者服务和RocketMQ通讯
创建消费者服务
- pom和生产者一样引用RocketMQ
- application.properties配置文件中配置消费者信息(my-producer)
- 创建消费者监听类
package com.hl.mqconsumer.consumer;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.stereotype.Component;
@Component
@RocketMQMessageListener(topic = "first-topic",consumerGroup = "my-consumer")
public class Consumer implements RocketMQListener<String> {
@Override
public void onMessage(String message) {
System.out.println(message);
}
}
测试
二、对各种不同类型的消息发送
2.1 消息生产者(mq-product)
配置文件
server.port=8080
#nameservice服务器地址(多个以英文逗号隔开)
rocketmq.name-server=127.0.0.1:9876
#组名
rocketmq.producer.group=hl-producer-group
#主题
rocketmq.producer.topic=hl-topic
#sync tag(同步消息tag)
rocketmq.producer.sync-tag=hl-sync-tags
#async tag(异步消息tag)
rocketmq.producer.async-tag=hl-async-tags
#oneway tag(单向消息tag)
rocketmq.producer.oneway-tag=hl-oneway-tags
提供不同类型的接口下发不同类型的消息
OrderStep.java
package com.hl.mqproduct.model;
import lombok.Data;
import java.util.ArrayList;
import java.util.List;
@Data
public class OrderStep {
private String id;
private String desc;
public OrderStep(String id, String desc) {
this.id = id;
this.desc = desc;
}
public static List<OrderStep> buildOrderSteps(String id) {
List<OrderStep> orderSteps = new ArrayList<>();
OrderStep createStep = new OrderStep(id, "create order");
orderSteps.add(createStep);
OrderStep payStep = new OrderStep(id, "pay order");
orderSteps.add(payStep);
OrderStep callbackStep = new OrderStep(id, "call back order");
orderSteps.add(callbackStep);
return orderSteps;
}
}
ResponseMsg.java
package com.hl.mqproduct.model;
import lombok.Data;
@Data
public class ResponseMsg {
public static final int CODE_FAIL = 500;
public static final int CODE_SUCCESS = 200;
public static final String MSG_SUCCESS = "success";
public static final String MSG_FAIL = "fail";
private int code;
private String msg;
private Object data;
public ResponseMsg() {
this.code = CODE_FAIL;
this.msg = MSG_FAIL;
}
public void setSuccessData(Object data) {
this.code = CODE_SUCCESS;
this.msg = MSG_SUCCESS;
this.data = data;
}
}
ListSplitter.java
package com.hl.mqproduct.util;
import java.util.Iterator;
import java.util.List;
public class ListSplitter<T> implements Iterator<List<T>> {
private final List<T> ts;
private int size;
private int currIndex;
public ListSplitter(List<T> ts, int size) {
this.ts = ts;
this.size = size;
}
@Override
public boolean hasNext() {
return currIndex < ts.size();
}
@Override
public List<T> next() {
int nextIndex = currIndex;
int totalSize = 0;
for (; nextIndex < ts.size(); nextIndex++) {
T t = ts.get(nextIndex);
totalSize = totalSize + t.toString().length();
if (totalSize > size) {
break;
}
}
List<T> subList = ts.subList(currIndex, nextIndex);
currIndex = nextIndex;
return subList;
}
}
SendCallbackListener.java
package com.hl.mqproduct.listener;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
@Slf4j
public class SendCallbackListener implements SendCallback {
private int id;
public SendCallbackListener(int id) {
this.id = id;
}
@Override
public void onSuccess(SendResult sendResult) {
log.info("CallBackListener on success : " + JSONObject.toJSONString(sendResult));
}
@Override
public void onException(Throwable throwable) {
log.error("CallBackListener on exception : ", throwable);
}
}
TransactionListener.java
package com.hl.mqproduct.listener;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQTransactionListener;
import org.apache.rocketmq.spring.core.RocketMQLocalTransactionListener;
import org.apache.rocketmq.spring.core.RocketMQLocalTransactionState;
import org.apache.rocketmq.spring.support.RocketMQHeaders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHeaders;
import java.util.HashMap;
import java.util.Map;
@Slf4j
@RocketMQTransactionListener
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class TransactionListener implements RocketMQLocalTransactionListener {
private static final Map<String, RocketMQLocalTransactionState> TRANSACTION_STATE_MAP = new HashMap<>();
@Override
public RocketMQLocalTransactionState executeLocalTransaction(Message message, Object o) {
log.info("执行本地事务");
MessageHeaders headers = message.getHeaders();
String transactionId = (String) headers.get(RocketMQHeaders.TRANSACTION_ID);
TRANSACTION_STATE_MAP.put(transactionId, RocketMQLocalTransactionState.UNKNOWN);
log.info("transactionId is {}",transactionId);
try {
Thread.sleep(10 * 1000l);
} catch (InterruptedException e) {
e.printStackTrace();
}
RocketMQLocalTransactionState state = RocketMQLocalTransactionState.ROLLBACK;
if (Integer.parseInt(transactionId) % 2 == 0) {
state = RocketMQLocalTransactionState.COMMIT;
}
log.info("transactionId is {}, state {}",transactionId, state.toString());
TRANSACTION_STATE_MAP.remove(transactionId);
return state;
}
@Override
public RocketMQLocalTransactionState checkLocalTransaction(Message message) {
MessageHeaders headers = message.getHeaders();
String transactionId = (String) headers.get(RocketMQHeaders.TRANSACTION_ID);
log.info("检查本地事务,事务ID:{}",transactionId);
RocketMQLocalTransactionState state = TRANSACTION_STATE_MAP.get(transactionId);
if(null != state){
return state;
}
return RocketMQLocalTransactionState.ROLLBACK;
}
}
MqMessageController.java
package com.hl.mqproduct.controller;
import com.alibaba.fastjson.JSONObject;
import com.hl.mqproduct.listener.SendCallbackListener;
import com.hl.mqproduct.model.OrderStep;
import com.hl.mqproduct.model.ResponseMsg;
import com.hl.mqproduct.util.ListSplitter;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.producer.MessageQueueSelector;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.client.producer.SendStatus;
import org.apache.rocketmq.client.producer.TransactionSendResult;
import org.apache.rocketmq.common.message.MessageQueue;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.apache.rocketmq.spring.support.RocketMQHeaders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.List;
@Slf4j
@RestController
@RequestMapping("/mqMessageController")
public class MqMessageController {
@Autowired
private RocketMQTemplate rocketMQTemplate;
@Value(value = "${rocketmq.producer.topic}:${rocketmq.producer.sync-tag}")
private String syncTag;
@Value(value = "${rocketmq.producer.topic}:${rocketmq.producer.async-tag}")
private String asyncag;
@Value(value = "${rocketmq.producer.topic}:${rocketmq.producer.oneway-tag}")
private String onewayTag;
@RequestMapping("/pushMessage.action")
public ResponseMsg pushMessage(@RequestParam("id") int id) {
log.info("pushMessage start : " + id);
String messageStr = "order id : " + id;
Message<String> message = MessageBuilder.withPayload(messageStr)
.setHeader(RocketMQHeaders.KEYS, id)
.build();
SendResult sendResult = rocketMQTemplate.syncSend(syncTag, message);
log.info("pushMessage finish : " + id + ", sendResult : " + JSONObject.toJSONString(sendResult));
ResponseMsg msg = new ResponseMsg();
if (sendResult.getSendStatus() == SendStatus.SEND_OK) {
msg.setSuccessData(sendResult.getMsgId() + " : " + sendResult.getSendStatus());
}
return msg;
}
@RequestMapping("/pushAsyncMessage.action")
public ResponseMsg pushAsyncMessage(@RequestParam("id") int id) {
log.info("pushAsyncMessage start : " + id);
String messageStr = "order id : " + id;
Message<String> message = MessageBuilder.withPayload(messageStr)
.setHeader(RocketMQHeaders.KEYS, id)
.build();
rocketMQTemplate.asyncSend(asyncag, message, new SendCallbackListener(id));
log.info("pushAsyncMessage finish : " + id);
ResponseMsg msg = new ResponseMsg();
msg.setSuccessData(null);
return msg;
}
@RequestMapping("/pushOneWayMessage.action")
public ResponseMsg pushOneWayMessage(@RequestParam("id") int id) {
log.info("pushOneWayMessage start : " + id);
String messageStr = "order id : " + id;
Message<String> message = MessageBuilder.withPayload(messageStr)
.setHeader(RocketMQHeaders.KEYS, id)
.build();
rocketMQTemplate.sendOneWay(onewayTag, message);
log.info("pushOneWayMessage finish : " + id);
ResponseMsg msg = new ResponseMsg();
msg.setSuccessData(null);
return msg;
}
@RequestMapping("/pushSequeueMessage.action")
public ResponseMsg pushSequeueMessage(@RequestParam("id") int id) {
log.info("pushSequeueMessage start : " + id);
for (int i = 0; i < 3; i++) {
String myId = id + "" + i;
List<OrderStep> myOrderSteps = OrderStep.buildOrderSteps(myId);
for (OrderStep orderStep : myOrderSteps) {
String messageStr = String.format("order id : %s, desc : %s", orderStep.getId(), orderStep.getDesc());
Message<String> message = MessageBuilder.withPayload(messageStr)
.setHeader(RocketMQHeaders.KEYS, orderStep.getId())
.build();
rocketMQTemplate.setMessageQueueSelector(new MessageQueueSelector() {
@Override
public MessageQueue select(List<MessageQueue> list, org.apache.rocketmq.common.message.Message message, Object o) {
int queueNum = Integer.valueOf(String.valueOf(o)) % list.size();
log.info(String.format("queueNum : %s, message : %s", queueNum, new String(message.getBody())));
return list.get(queueNum);
}
});
rocketMQTemplate.syncSendOrderly(syncTag, message, orderStep.getId());
}
}
log.info("pushSequeueMessage finish : " + id);
ResponseMsg msg = new ResponseMsg();
msg.setSuccessData(null);
return msg;
}
@RequestMapping("/pushDelayMessage.action")
public ResponseMsg pushDelayMessage(@RequestParam("id") int id) {
log.info("pushDelayMessage start : " + id);
String messageStr = "order id : " + id;
Message<String> message = MessageBuilder.withPayload(messageStr)
.setHeader(RocketMQHeaders.KEYS, id)
.build();
SendResult sendResult = rocketMQTemplate.syncSend(syncTag, message, 1 * 1000l, 4);
log.info("pushDelayMessage finish : " + id + ", sendResult : " + JSONObject.toJSONString(sendResult));
ResponseMsg msg = new ResponseMsg();
if (sendResult.getSendStatus() == SendStatus.SEND_OK) {
msg.setSuccessData(sendResult.getMsgId() + " : " + sendResult.getSendStatus());
}
return msg;
}
@RequestMapping("/pushBatchMessage.action")
public ResponseMsg pushBatchMessage(@RequestParam("id") int id) {
log.info("pushBatchMessage start : " + id);
List<Message> messages = new ArrayList<>();
for (int i = 0; i < 3; i++) {
String myId = id + "" + i;
String messageStr = "order id : " + myId;
Message<String> message = MessageBuilder.withPayload(messageStr)
.setHeader(RocketMQHeaders.KEYS, myId)
.build();
messages.add(message);
}
ListSplitter splitter = new ListSplitter(messages, 1024 * 1024 * 4);
while (splitter.hasNext()) {
List<Message> listItem = splitter.next();
rocketMQTemplate.syncSend(syncTag, listItem);
}
log.info("pushBatchMessage finish : " + id);
ResponseMsg msg = new ResponseMsg();
msg.setSuccessData(null);
return msg;
}
@RequestMapping("/pushSqlMessage.action")
public ResponseMsg pushSqlMessage(@RequestParam("id") int id) {
log.info("pushSqlMessage start : " + id);
List<Message> messages = new ArrayList<>();
for (int i = 0; i < 10; i++) {
String myId = id + "" + i;
String messageStr = "order id : " + myId;
Message<String> message = MessageBuilder.withPayload(messageStr)
.setHeader(RocketMQHeaders.KEYS, myId)
.setHeader("money", i)
.build();
messages.add(message);
}
rocketMQTemplate.syncSend(syncTag, messages);
log.info("pushSqlMessage finish : " + id);
ResponseMsg msg = new ResponseMsg();
msg.setSuccessData(null);
return msg;
}
@RequestMapping("/pushTransactionMessage.action")
public ResponseMsg pushTransactionMessage(@RequestParam("id") int id) {
log.info("pushTransactionMessage start : " + id);
String messageStr = "order id : " + id;
Message<String> message = MessageBuilder.withPayload(messageStr)
.setHeader(RocketMQHeaders.KEYS, id)
.setHeader("money", 10)
.setHeader(RocketMQHeaders.TRANSACTION_ID, id)
.build();
TransactionSendResult transactionSendResult = rocketMQTemplate.sendMessageInTransaction(syncTag, message, null);
log.info("pushTransactionMessage result : " + JSONObject.toJSONString(transactionSendResult));
log.info("pushTransactionMessage finish : " + id);
ResponseMsg msg = new ResponseMsg();
msg.setSuccessData(null);
return msg;
}
}
2.2 消息消费者(mq-consumer)
配置文件
server.port=8081
#nameservice服务器地址(多个以英文逗号隔开)
rocketmq.name-server=127.0.0.1:9876
#组名
rocketmq.consumer.group=hl-consumer-group
#监听主题
rocketmq.consumer.topic=hl-topic
#tags(监听多个tag时使用 || 进行分割,如果监听所有使用*或者不填)
rocketmq.consumer.tags=hl-sync-tags||hl-async-tags||hl-oneway-tags
监听接口配置
package com.hl.mqconsumer.consumer;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.MessageModel;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.stereotype.Component;
@Component
@RocketMQMessageListener(consumerGroup = "${rocketmq.consumer.group}", topic = "${rocketmq.consumer.topic}",
selectorExpression = "${rocketmq.consumer.tags}", messageModel = MessageModel.BROADCASTING)
@Slf4j
public class Consumer implements RocketMQListener<String> {
@Override
public void onMessage(String message) {
System.out.println(message);
}
}
2.3 消息消费者(mq-consumer-02)
配置文件
server.port=8082
rocketmq.name-server=127.0.0.1:9876
rocketmq.consumer.group=hl-consumer-group
rocketmq.consumer.topic=hl-topic
#tags(监听多个tag时使用 || 进行分割,如果监听所有使用*或者不填),支持sql过滤
#tags: anran-sync-tags||anran-async-tags||anran-oneway-tags
rocketmq.consumer.tags=money > 5
监听接口配置
package com.hl.mqconsumer02.controller;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.ConsumeMode;
import org.apache.rocketmq.spring.annotation.MessageModel;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.annotation.SelectorType;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.stereotype.Component;
@Component
@RocketMQMessageListener(consumerGroup = "${rocketmq.consumer.group}", topic = "${rocketmq.consumer.topic}",
selectorExpression = "${rocketmq.consumer.tags}", messageModel = MessageModel.CLUSTERING,
consumeMode = ConsumeMode.ORDERLY, selectorType = SelectorType.SQL92)
@Slf4j
public class Consumer implements RocketMQListener<String> {
@Override
public void onMessage(String message) {
log.info(message);
}
}
2.4 消息消费者(mq-consumer-03)
配置文件
server.port=8083
rocketmq.name-server=127.0.0.1:9876
rocketmq.consumer.group=hl-consumer-group
rocketmq.consumer.topic=hl-topic
rocketmq.consumer.tags=hl-sync-tags||hl-async-tags||hl-oneway-tags
监听接口配置
package com.hl.mqconsumer03.consumer;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.ConsumeMode;
import org.apache.rocketmq.spring.annotation.MessageModel;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.stereotype.Component;
@Component
@RocketMQMessageListener(consumerGroup = "${rocketmq.consumer.group}", topic = "${rocketmq.consumer.topic}",
selectorExpression = "${rocketmq.consumer.tags}", messageModel = MessageModel.CLUSTERING,
consumeMode = ConsumeMode.ORDERLY)
@Slf4j
public class Consumer implements RocketMQListener<String> {
@Override
public void onMessage(String message) {
log.info(message);
try {
Thread.sleep(10 * 1000l);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
注意: 1.如果两个消费者group和topic都一样,则二者轮循接收消息 2.如果两个消费者topic一样,而group不一样,则消息变成广播机制
|