第一步引入jar
<dependencies>
<dependency>
<groupId>org.apache.rocketmq</groupId>
<artifactId>rocketmq-spring-boot-starter</artifactId>
<version>2.1.0</version>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
</dependencies>
配置文件配置
server.port=8080
server.servlet.context-path=/rocketmq
rocketmq.name-server=192.168.*.13:9876
rocketmq.producer.group=springbootGroup
生产者配置
@Component
@AllArgsConstructor
public class SpringProducer {
private final RocketMQTemplate rocketMQTemplate;
public void sendMessage(String topic, String msg) {
this.rocketMQTemplate.convertAndSend(topic, msg);
}
public void sendTransactionMessage(String topic, String msg) throws InterruptedException {
String[] tags = new String[]{"TagA", "TagB", "TagC", "TagD", "TagE"};
for (int i = 0; i < 10; i++) {
Message<String> message = MessageBuilder.withPayload(msg)
.setHeader(RocketMQHeaders.TRANSACTION_ID, "TransID_" + i)
.setHeader(RocketMQHeaders.TAGS, tags[i % tags.length])
.setHeader("MyProp", "MyPro_" + i)
.build();
String destination = topic + ":" + tags[i % tags.length];
TransactionSendResult sendResult = rocketMQTemplate.sendMessageInTransaction(destination, message, destination);
System.out.printf("%s\n",sendResult);
Thread.sleep(10);
}
}
}
RocketMQ事务配置
@RocketMQTransactionListener(rocketMQTemplateBeanName = "rocketMQTemplate")
public class MyTransactionListenImpl implements RocketMQLocalTransactionListener {
private ConcurrentHashMap<Object,Message> localTrans = new ConcurrentHashMap<>();
@Override
public RocketMQLocalTransactionState executeLocalTransaction(Message msg, Object arg) {
Object transId = msg.getHeaders().get(RocketMQHeaders.PREFIX + RocketMQHeaders.TRANSACTION_ID);
String destination = arg.toString();
localTrans.put(transId,msg);
System.out.println("executeTransaction msg:"+msg);
org.apache.rocketmq.common.message.Message message = RocketMQUtil.convertToRocketMessage(new StringMessageConverter(), "UTF-8", destination, msg);
System.out.println("message:"+message);
String tags = message.getTags();
if (StringUtils.contains(tags,"TagA")){
return RocketMQLocalTransactionState.COMMIT;
}else if (StringUtils.contains(tags,"TagB")){
return RocketMQLocalTransactionState.ROLLBACK;
}else {
return RocketMQLocalTransactionState.UNKNOWN;
}
}
@Override
public RocketMQLocalTransactionState checkLocalTransaction(Message message) {
String transId = message.getHeaders().get(RocketMQHeaders.PREFIX + RocketMQHeaders.TRANSACTION_ID).toString();
Message originalMessage = localTrans.get(transId);
System.out.println("checkLocalTransaction-->"+originalMessage);
String tags = message.getHeaders().get(RocketMQHeaders.PREFIX + RocketMQHeaders.TAGS).toString();
if (StringUtils.contains(tags,"TagC")){
return RocketMQLocalTransactionState.COMMIT;
}else if (StringUtils.contains(tags,"TagD")){
return RocketMQLocalTransactionState.ROLLBACK;
}else {
return RocketMQLocalTransactionState.UNKNOWN;
}
}
}
消费者配置
@Component
@RocketMQMessageListener(consumerGroup = "MyConsumerGroup",topic = "TestSpringTopic",consumeMode = ConsumeMode.CONCURRENTLY)
public class SpringConsumer implements RocketMQListener<String> {
@Override
public void onMessage(String s) {
System.out.println("消费者消费消息:"+s);
}
}
启动类
@SpringBootApplication
public class RocketApplication {
public static void main(String[] args) {
SpringApplication.run(RocketApplication.class,args);
}
}
访问路径
http://localhost:8080/rocketmq/mq/sendMessage?message=helloworld