链接:https://pan.baidu.com/s/1gD-pV0boCKrOj9VKy8Zgcg?pwd=sh7s
D:\program\RocketMQ\rocketmq\bin>D:\program\RocketMQ\rocketmq\bin\mqnamesrv.cmd
D:\program\RocketMQ\rocketmq\bin>mqbroker.cmd -n localhost:9876 autoCreateTopicEnable=true -c D:\program\RocketMQ\rocketmq\conf\broker.conf
D:\program\RocketMQ\rocketmq\bin>tools.cmd org.apache.rocketmq.example.quickstart.Producer
D:\program\RocketMQ\rocketmq\bin>tools.cmd org.apache.rocketmq.example.quickstart.Consumer
D:\program\RocketMQ\rocketmq-console>mvn clean package -Dmaven.test.skip=true
D:\program\RocketMQ\rocketmq-console\target>java -jar rocketmq-console-ng-2.0.0.jar
4.0.0
pom
rocketmq-provider
rocketmq-consumer
com.java
RocketMQ
1.0-SNAPSHOT
UTF-8
1.8
1.8
2.3.2.RELEASE
2.2.0
org.springframework.boot
spring-boot-dependencies
${springboot.version}
pom
import
org.apache.rocketmq
rocketmq-spring-boot-starter
${rocketmq.version}
org.springframework.boot
spring-boot-maven-plugin
RocketMQ
com.java
1.0-SNAPSHOT
4.0.0
rocketmq-provider
org.springframework.boot
spring-boot-starter-web
org.apache.rocketmq
rocketmq-spring-boot-starter
org.springframework.boot
spring-boot-starter-web-services
org.apache.cxf
cxf-spring-boot-starter-jaxws
3.2.14
com.sun.xml.ws
jaxws-ri
2.3.1
server:
port: 8081
servlet:
context-path: /
rocketmq:
name-server: 127.0.0.1:9876
# name-server: 192.168.1.44:9876;192.168.1.109:9876;192.168.1.124:9876;192.168.1.247:9876;
producer:
group: producer-demo
send-message-timeout: 60000
package com.java;
import com.java.rocketmq.*;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
@SpringBootApplication
public class RocketmqProducerApplication {
public static void main(String[] args) {
ConfigurableApplicationContext run = SpringApplication.run(RocketmqProducerApplication.class, args);
// 发送简单消息
// ProducerService_1 producerService_1 = (ProducerService_1) run.getBean("producerService_1");
// producerService_1.sendMessage();
// 发送同步消息
// ProducerService_2 producerService_2 = (ProducerService_2) run.getBean("producerService_2");
// producerService_2.sendMessage();
// 发送异步消息
// ProducerService_3 producerService_3 = (ProducerService_3) run.getBean("producerService_3");
// producerService_3.sendMessage();
// 发送单向消息
// ProducerService_4 producerService_4 = (ProducerService_4) run.getBean("producerService_4");
// producerService_4.sendMessage();
// 消费端的消费模式:广播模式
// ProducerService_5 producerService_5 = (ProducerService_5) run.getBean("producerService_5");
// producerService_5.sendMessage();
// 消费端的消费模式:负载均衡模式
// ProducerService_6 producerService_6 = (ProducerService_6) run.getBean("producerService_6");
// producerService_6.sendMessage();
// 发送同步顺序消息
// ProducerService_7 producerService_7 = (ProducerService_7) run.getBean("producerService_7");
// producerService_7.sendMessage();
// 发送延迟消息
// ProducerService_8 producerService_8 = (ProducerService_8) run.getBean("producerService_8");
// producerService_8.sendMessage();
// 发送事务消息
// ProducerService_9 producerService_9 = (ProducerService_9) run.getBean("producerService_9");
// producerService_9.sendMessage();
// 过滤消息:TAG方式
// ProducerService_10 producerService_10 = (ProducerService_10) run.getBean("producerService_10");
// producerService_10.sendMessage();
// 过滤消息:SQL方式
ProducerService_11 producerService_11 = (ProducerService_11) run.getBean("producerService_11");
producerService_11.sendMessage();
}
}
RocketMQ
com.java
1.0-SNAPSHOT
4.0.0
rocketmq-consumer
org.springframework.boot
spring-boot-starter-web
org.apache.rocketmq
rocketmq-spring-boot-starter
server:
port: 8082
servlet:
context-path: /
rocketmq:
name-server: 127.0.0.1:9876
# name-server: 192.168.1.44:9876;192.168.1.109:9876;192.168.1.124:9876;192.168.1.247:9876;
consumer:
group_1: consumer-demo1
group_2: consumer-demo2
group_3: consumer-demo3
group_4: consumer-demo4
group_5: consumer-demo5
group_6: consumer-demo6
group_7: consumer-demo7
group_8: consumer-demo8
group_9: consumer-demo9
group_10: consumer-demo10
group_11: consumer-demo11
package com.java;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class RocketmqConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(RocketmqConsumerApplication.class, args);
}
}
package com.java.rocketmq;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.Map;
/**
* 消息生产者service类 发送简单消息
*/
@Component("producerService_1")
public class ProducerService_1 {
@Autowired
private RocketMQTemplate rocketMQTemplate;
/**
* 发送简单消息
*/
public void sendMessage(){
for(int i=0;i<10;i++){
rocketMQTemplate.convertAndSend("rocketmq_1","简单消息"+i);
}
}
}
package com.java.rocketmq;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.stereotype.Component;
/**
* 消息消费者service类 接收简单消息
*/
@RocketMQMessageListener(topic = "rocketmq_1",consumerGroup = "${rocketmq.consumer.group_1}")
@Component
public class ConsumerService_1 implements RocketMQListener {
@Override
public void onMessage(String message) {
System.out.println("接收简单消息:"+message);
}
}
package com.java.rocketmq;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.Map;
/**
* 消息生产者service类 发送同步消息
* 同步消息根据syncSend返回值sendResult的sendStatus属性判断是否成功,然后进行后续业务处理
* 后续业务处理期间代码不会继续往下执行
*/
@Component("producerService_2")
public class ProducerService_2 {
@Autowired
private RocketMQTemplate rocketMQTemplate;
/**
* 发送同步消息
*/
public void sendMessage(){
for(int i=0;i<10;i++){
SendResult sendResult = rocketMQTemplate.syncSend("rocketmq_2","同步消息"+i);
System.out.println(sendResult);
}
}
}
package com.java.rocketmq;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.stereotype.Component;
/**
* 消息消费者service类 接收同步消息
*/
@RocketMQMessageListener(topic = "rocketmq_2",consumerGroup = "${rocketmq.consumer.group_2}")
@Component
public class ConsumerService_2 implements RocketMQListener {
@Override
public void onMessage(String message) {
System.out.println("接收同步消息:"+message);
}
}
package com.java.rocketmq;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.Map;
/**
* 消息生产者service类 发送异步消息
* 异步消息根据onSuccess和onException异步回调方法,然后进行后续业务处理
* 后续业务处理期间代码继续往下执行
*/
@Component("producerService_3")
public class ProducerService_3 {
@Autowired
private RocketMQTemplate rocketMQTemplate;
/**
* 发送异步消息
*/
public void sendMessage(){
for(int i=0;i<10;i++){
rocketMQTemplate.asyncSend("rocketmq_3", "异步消息"+i, new SendCallback() {
@Override
public void onSuccess(SendResult sendResult) {
System.out.println("异步消息发送成功!");
}
@Override
public void onException(Throwable throwable) {
System.out.println("异步消息发送失败!");
}
});
}
}
}
package com.java.rocketmq;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.stereotype.Component;
/**
* 消息消费者service类 接收异步消息
*/
@RocketMQMessageListener(topic = "rocketmq_3",consumerGroup = "${rocketmq.consumer.group_3}")
@Component
public class ConsumerService_3 implements RocketMQListener {
@Override
public void onMessage(String message) {
System.out.println("接收异步消息:"+message);
}
}
package com.java.rocketmq;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.Map;
/**
* 消息生产者service类 发送单向消息
* 不特别关心发送结果的场景 例:日志发送
* 由于不关心结果,sendOneWay方法没有返回值
*/
@Component("producerService_4")
public class ProducerService_4 {
@Autowired
private RocketMQTemplate rocketMQTemplate;
/**
* 发送单向消息
*/
public void sendMessage(){
for(int i=0;i<10;i++){
rocketMQTemplate.sendOneWay("rocketmq_4", "单向消息"+i);
}
}
}
package com.java.rocketmq;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.stereotype.Component;
/**
* 消息消费者service类 接收单向消息
*/
@RocketMQMessageListener(topic = "rocketmq_4",consumerGroup = "${rocketmq.consumer.group_4}")
@Component
public class ConsumerService_4 implements RocketMQListener {
@Override
public void onMessage(String message) {
System.out.println("接收单向消息:"+message);
}
}
package com.java.rocketmq;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.Map;
/**
* 消息生产者service类 消费端的消费模式:广播模式
* 广播模式是每个消费者,都会消费消息
*/
@Component("producerService_5")
public class ProducerService_5 {
@Autowired
private RocketMQTemplate rocketMQTemplate;
/**
* 消费端的消费模式:广播模式
*/
public void sendMessage(){
for(int i=0;i<10;i++){
rocketMQTemplate.convertAndSend("rocketmq_5","广播模式"+i);
}
}
}
package com.java.rocketmq;
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;
/**
* 消息消费者service类 消费模式:广播模式
* messageModel = MessageModel.BROADCASTING为广播模式接收消息
* 改变消费者端口号启动多个消费者实例测试消息接收情况
*/
@RocketMQMessageListener(topic = "rocketmq_5",consumerGroup = "${rocketmq.consumer.group_5}",messageModel = MessageModel.BROADCASTING)
@Component
public class ConsumerService_5 implements RocketMQListener {
@Override
public void onMessage(String message) {
System.out.println("广播模式:"+message);
}
}
package com.java.rocketmq;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/**
* 消息生产者service类 消费端的消费模式:负载均衡模式
* 负载均衡模式是每一个消息只会被某一个消费者消费一次
*/
@Component("producerService_6")
public class ProducerService_6 {
@Autowired
private RocketMQTemplate rocketMQTemplate;
/**
* 消费端的消费模式:负载均衡模式
*/
public void sendMessage(){
for(int i=0;i<10;i++){
rocketMQTemplate.convertAndSend("rocketmq_6","负载均衡模式"+i);
}
}
}
package com.java.rocketmq;
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;
/**
* 消息消费者service类 消费模式:负载均衡模式
* messageModel = MessageModel.CLUSTERING为负载均衡模式接收消息
* 改变消费者端口号启动多个消费者实例测试消息接收情况
*/
@RocketMQMessageListener(topic = "rocketmq_6",consumerGroup = "${rocketmq.consumer.group_6}",messageModel = MessageModel.CLUSTERING)
@Component
public class ConsumerService_6 implements RocketMQListener {
@Override
public void onMessage(String message) {
System.out.println("负载均衡模式:"+message);
}
}
package com.java.rocketmq;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.Map;
/**
* 消息生产者service类 发送同步顺序消息
* broker会管理多个消息队列,syncSendOrderly发送消息时,hashKey相同的消息属于同一个消息队列,同一个队列的消息按顺序依次被消费
*/
@Component("producerService_7")
public class ProducerService_7 {
@Autowired
private RocketMQTemplate rocketMQTemplate;
/**
* 发送同步顺序消息
*/
public void sendMessage(){
// 9条消息分别在3个对列排队,按照步骤依次被消费
rocketMQTemplate.syncSendOrderly("rocketmq_7", "消息队列1,第1步", "消息队列1");
rocketMQTemplate.syncSendOrderly("rocketmq_7", "消息队列1,第2步", "消息队列1");
rocketMQTemplate.syncSendOrderly("rocketmq_7", "消息队列1,第3步", "消息队列1");
rocketMQTemplate.syncSendOrderly("rocketmq_7", "消息队列2,第1步", "消息队列2");
rocketMQTemplate.syncSendOrderly("rocketmq_7", "消息队列2,第2步", "消息队列2");
rocketMQTemplate.syncSendOrderly("rocketmq_7", "消息队列2,第3步", "消息队列2");
rocketMQTemplate.syncSendOrderly("rocketmq_7", "消息队列3,第1步", "消息队列3");
rocketMQTemplate.syncSendOrderly("rocketmq_7", "消息队列3,第2步", "消息队列3");
rocketMQTemplate.syncSendOrderly("rocketmq_7", "消息队列3,第3步", "消息队列3");
}
}
package com.java.rocketmq;
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;
/**
* 消息消费者service类 接收同步顺序消息
* consumeMode = ConsumeMode.CONCURRENTLY 并发多线程接收消息(默认)
* consumeMode = ConsumeMode.ORDERLY 单线程顺序接收消息
*/
@RocketMQMessageListener(topic = "rocketmq_7",consumerGroup = "${rocketmq.consumer.group_7}",consumeMode = ConsumeMode.ORDERLY)
@Component
public class ConsumerService_7 implements RocketMQListener {
@Override
public void onMessage(String message) {
System.out.println("接收同步顺序消息:"+message);
}
}
package com.java.rocketmq;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Component;
/**
* 消息生产者service类 发送延迟消息
* timeout:消息发送超时时间,默认是3秒
* delayLevel=1s 5s 10s 30s 1m 2m 3m 4m 5m 6m 7m 8m 9m 10m 20m 30m 1h 2h
*/
@Component("producerService_8")
public class ProducerService_8 {
@Autowired
private RocketMQTemplate rocketMQTemplate;
/**
* 发送延迟消息
*/
public void sendMessage(){
rocketMQTemplate.syncSend("rocketmq_8", MessageBuilder.withPayload("延迟消息1秒").build(),3000,1);
rocketMQTemplate.syncSend("rocketmq_8", MessageBuilder.withPayload("延迟消息5秒").build(),3000,2);
rocketMQTemplate.syncSend("rocketmq_8", MessageBuilder.withPayload("延迟消息10秒").build(),3000,3);
}
}
package com.java.rocketmq;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.stereotype.Component;
/**
* 消息消费者service类 接收延迟消息
*/
@RocketMQMessageListener(topic = "rocketmq_8",consumerGroup = "${rocketmq.consumer.group_8}")
@Component
public class ConsumerService_8 implements RocketMQListener {
@Override
public void onMessage(String message) {
System.out.println("接收延迟消息:"+message);
}
}
package com.java.rocketmq;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Component;
/**
* 消息生产者service类 发送事务消息
* 半消息:sendMessageInTransaction把消息发送到Broker后无法被消费
* TransactionListenerImpl为事务处理类,executeLocalTransaction方法中处理事务
* 事务处理成功:executeLocalTransaction方法返回RocketMQLocalTransactionState.COMMIT,此时消息可以被消费
* 事务处理失败:executeLocalTransaction方法返回RocketMQLocalTransactionState.ROLLBACK,此时消息被丢弃
* 事务处理情况未知:executeLocalTransaction方法返回RocketMQLocalTransactionState.UNKNOWN,此时消息继续等待
* 事务处理情况未知时,每隔一段时间会执行checkLocalTransaction方法查询事务处理情况,返回值同上,最多查询15次,如果事务处理情况仍然未知则消息被丢弃
* 测试方法:executeLocalTransaction和checkLocalTransaction分别返回不同的结果,观察消息是否被消费端消费
*/
@Component("producerService_9")
public class ProducerService_9 {
@Autowired
private RocketMQTemplate rocketMQTemplate;
/**
* 发送事务消息
*/
public void sendMessage(){
rocketMQTemplate.sendMessageInTransaction("rocketmq_9",MessageBuilder.withPayload("事务消息").build(),null);
}
}
package com.java.rocketmq;
import org.apache.rocketmq.spring.annotation.RocketMQTransactionListener;
import org.apache.rocketmq.spring.core.RocketMQLocalTransactionListener;
import org.apache.rocketmq.spring.core.RocketMQLocalTransactionState;
@RocketMQTransactionListener
public class TransactionListenerImpl implements RocketMQLocalTransactionListener {
@Override
public RocketMQLocalTransactionState executeLocalTransaction(org.springframework.messaging.Message msg, Object arg) {
// ... local transaction process, return bollback, commit or unknown
// System.out.println("事务处理成功,消息可以被消费");
// return RocketMQLocalTransactionState.COMMIT;
// System.out.println("事务处理失败,消息被丢弃");
// return RocketMQLocalTransactionState.ROLLBACK;
System.out.println("事务处理情况未知,消息继续等待");
return RocketMQLocalTransactionState.UNKNOWN;
}
@Override
public RocketMQLocalTransactionState checkLocalTransaction(org.springframework.messaging.Message msg) {
// ... check transaction status and return bollback, commit or unknown
System.out.println("查询事务状态为成功,消息可以被消费");
return RocketMQLocalTransactionState.COMMIT;
// System.out.println("查询事务状态为失败,消息被丢弃");
// return RocketMQLocalTransactionState.ROLLBACK;
// System.out.println("查询事务状态为未知,消息继续等待");
// return RocketMQLocalTransactionState.UNKNOWN;
}
}
package com.java.rocketmq;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.stereotype.Component;
/**
* 消息消费者service类 接收事务消息
*/
@RocketMQMessageListener(topic = "rocketmq_9",consumerGroup = "${rocketmq.consumer.group_9}")
@Component
public class ConsumerService_9 implements RocketMQListener {
@Override
public void onMessage(String message) {
System.out.println("接收事务消息:"+message);
}
}
package com.java.rocketmq;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Component;
/**
* 消息生产者service类 过滤消息:TAG方式
* convertAndSend方法destination参数为-->topic:TAG
*/
@Component("producerService_10")
public class ProducerService_10 {
@Autowired
private RocketMQTemplate rocketMQTemplate;
/**
* 过滤消息:TAG方式
*/
public void sendMessage(){
rocketMQTemplate.convertAndSend("rocketmq_10" + ":" + "TAG1", MessageBuilder.withPayload("过滤消息TAG1").build());
rocketMQTemplate.convertAndSend("rocketmq_10" + ":" + "TAG2", MessageBuilder.withPayload("过滤消息TAG2").build());
rocketMQTemplate.convertAndSend("rocketmq_10" + ":" + "TAG3", MessageBuilder.withPayload("过滤消息TAG3").build());
rocketMQTemplate.convertAndSend("rocketmq_10" + ":" + "TAG4", MessageBuilder.withPayload("过滤消息TAG4").build());
rocketMQTemplate.convertAndSend("rocketmq_10" + ":" + "TAG5", MessageBuilder.withPayload("过滤消息TAG5").build());
rocketMQTemplate.convertAndSend("rocketmq_10" + ":" + "TAG6", MessageBuilder.withPayload("过滤消息TAG6").build());
}
}
package com.java.rocketmq;
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;
/**
* 消息消费者service类 过滤消息:TAG方式
* selectorType = SelectorType.TAG为TAG方式接收消息
* selectorExpression = "TAG1 || TAG2"根据TAG指定需要消费的消息
*/
@RocketMQMessageListener(topic = "rocketmq_10",consumerGroup = "${rocketmq.consumer.group_10}",selectorType = SelectorType.TAG,selectorExpression = "TAG1 || TAG3 || TAG5")
@Component
public class ConsumerService_10 implements RocketMQListener {
@Override
public void onMessage(String message) {
System.out.println("TAG方式:"+message);
}
}
package com.java.rocketmq;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.Map;
/**
* 消息生产者service类 过滤消息:SQL方式
* convertAndSend方法headers参数用来封装需要过滤的参数
*/
@Component("producerService_11")
public class ProducerService_11 {
@Autowired
private RocketMQTemplate rocketMQTemplate;
/**
* 过滤消息:SQL方式
*/
public void sendMessage(){
Map headers1 = new HashMap<>() ;
headers1.put("name", "张三") ;
headers1.put("age", 5) ;
rocketMQTemplate.convertAndSend("rocketmq_11", MessageBuilder.withPayload("过滤消息SQL1").build(), headers1);
Map headers2 = new HashMap<>() ;
headers2.put("name", "李四") ;
headers2.put("age", 10) ;
rocketMQTemplate.convertAndSend("rocketmq_11", MessageBuilder.withPayload("过滤消息SQL2").build(), headers2);
Map headers3 = new HashMap<>() ;
headers3.put("name", "王五") ;
headers3.put("age", 15) ;
rocketMQTemplate.convertAndSend("rocketmq_11", MessageBuilder.withPayload("过滤消息SQL3").build(), headers3);
}
}
package com.java.rocketmq;
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;
/**
* 消息消费者service类 过滤消息:SQL方式
* selectorType = SelectorType.SQL92为SQL方式接收消息
* 启动报错:The broker does not support consumer to filter message by SQL92(broker默认不支持SQL92过滤消息)
* D:\program\RocketMQ\rocketmq\conf\broker.conf中添加SQL方式过滤消息支持enablePropertyFilter=true,重新启动broker
* D:\program\RocketMQ\rocketmq\bin>mqbroker.cmd -n localhost:9876 autoCreateTopicEnable=true -c D:\program\RocketMQ\rocketmq\conf\broker.conf
* selectorExpression = "name='张三' or age>10",根据SQL指定需要消费的消息
* 数字比较:> >= < <= BETWEEN =
* 字符比较:= <> IN
* 逻辑运算符:AND OR NOT
*/
@RocketMQMessageListener(topic = "rocketmq_11",consumerGroup = "${rocketmq.consumer.group_11}",selectorType = SelectorType.SQL92,selectorExpression = "name='张三' or age>10")
@Component
public class ConsumerService_11 implements RocketMQListener {
@Override
public void onMessage(String message) {
System.out.println("SQL方式:"+message);
}
}
https://blog.csdn.net/caoli201314?type=blog