org.springframework.boot
spring-boot-starter-parent
2.2.6.RELEASE
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
org.apache.rocketmq
rocketmq-spring-boot-starter
2.2.3
配置
rocketmq.name-server=localhost:9876
rocketmq.producer.group=my-group
发送同步消息
@Resource
private RocketMQTemplate rocketMQTemplate;
@Test
public void testSendSyncMsg(){
String topic = "helloTopic";
Message message = MessageBuilder.withPayload("发送同步消息").build();
rocketMQTemplate.syncSend(topic,message);
}
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.stereotype.Component;
@Component
@RocketMQMessageListener(consumerGroup = "helloTopicGroup",topic = "helloTopic")
public class HelloListener implements RocketMQListener {
@Override
public void onMessage(MessageExt messageExt) {
System.out.println("消息内容为===>"+new String(messageExt.getBody()));
}
}
@Test
public void testSendASyncMsg() {
String topic = "helloTopic";
Message message = MessageBuilder.withPayload("发送异步消息").build();
rocketMQTemplate.asyncSend(topic, message ,new SendCallback() {
@Override
public void onSuccess(SendResult sendResult) {
System.out.println("消息发送状态===>"+sendResult.getSendStatus());
}
@Override
public void onException(Throwable throwable) {
System.out.println("出现异常");
}
});
Thread.sleep(3000);
}
@Test
public void testSendOneWayMsg() {
String topic = "helloTopic";
Message message = MessageBuilder.withPayload("发送一次性消息").build();
rocketMQTemplate.sendOneWay(topic,message);
}
import org.apache.rocketmq.common.message.MessageExt;
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 = "helloTopicGroup",topic = "helloTopic",messageModel = MessageModel.BROADCASTING)
public class HelloListener implements RocketMQListener {
@Override
public void onMessage(MessageExt messageExt) {
System.out.println("消息内容为===>"+new String(messageExt.getBody()));
}
}
@Test
public void testSendDelayMsg(){
String topic = "helloTopic";
Message message = MessageBuilder.withPayload("延时消息").build();
rocketMQTemplate.syncSend(topic,message,3000,3);
}
@Test
public void testSendTagMsg(){
String topic = "tagTopic";
Message message1 = MessageBuilder.withPayload("消息1").build();
Message message2 = MessageBuilder.withPayload("消息2").build();
Message message3 = MessageBuilder.withPayload("消息3").build();
rocketMQTemplate.sendOneWay(topic+":"+"TagA",message1);
rocketMQTemplate.sendOneWay(topic+":"+"TagB",message2);
rocketMQTemplate.sendOneWay(topic+":"+"TagC",message3);
}
@Component
@RocketMQMessageListener(consumerGroup = "tagTopicGroup",topic = "tagTopic",selectorExpression = "TagA || TagC")
public class TagListener implements RocketMQListener {
@Override
public void onMessage(MessageExt messageExt) {
System.out.println("Tag 消息内容为===>"+new String(messageExt.getBody()));
}
}
@Test
public void testSendSqlMsg(){
String topic = "sqlTopic";
Message message1 = MessageBuilder.withPayload("用户1").setHeader("age",20).build();
Message message2 = MessageBuilder.withPayload("用户2").setHeader("age",35).build();
Message message3 = MessageBuilder.withPayload("用户3").setHeader("age",66).build();
rocketMQTemplate.sendOneWay(topic,message1);
rocketMQTemplate.sendOneWay(topic,message2);
rocketMQTemplate.sendOneWay(topic,message3);
}
@Component
@RocketMQMessageListener(consumerGroup = "sqlTopicGroup",topic = "sqlTopic",selectorType = SelectorType.SQL92,selectorExpression = "age < 30")
public class SqlListener implements RocketMQListener {
@Override
public void onMessage(MessageExt messageExt) {
System.out.println("Tag 消息内容为===>"+new String(messageExt.getBody()));
}
}
@Test
public void testSendOrderlyMsg(){
String topic = "helloTopic";
String id = "11";
Message message1 = MessageBuilder.withPayload(id).build();
rocketMQTemplate.setMessageQueueSelector((list, message, o) -> {
long id1 = Long.parseLong(o.toString());
int index = (int) (id1 % list.size());
return list.get(index);
});
rocketMQTemplate.sendOneWayOrderly(topic,message1,id);
}
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.ConsumeMode;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.stereotype.Component;
@Component
@RocketMQMessageListener(consumerGroup = "orderTopicGroup",topic = "orderTopic",consumeMode = ConsumeMode.ORDERLY)
public class OrderListener implements RocketMQListener {
@Override
public void onMessage(MessageExt messageExt) {
System.out.println("线程===>"+Thread.currentThread().getName()+",队列ID===>"+messageExt.getQueueId()+",消息内容为===>"+new String(messageExt.getBody()));
}
}