Springboot2.0 集成Rocketmq实现事物消息
废话不多说,直接进入正题
1.引入相关Maven依赖:
注意:当前的RELEASE.VERSION=2.0.1
org.apache.rocketmq
rocketmq-spring-boot-starter
${RELEASE.VERSION}
2.配置生产者:
(1).application.yml配置如下:
rocketmq:
name-server: '127.0.0.1:9876'
producer:
group: my-group
send-message-timeout: 300000
compress-message-body-threshold: 4096
max-message-size: 4194304
retry-times-when-send-async-failed: 0
retry-next-server: true
retry-times-when-send-failed: 2
(2).发送事物消息:
@Resource
private RocketMQTemplate rocketMQTemplate;
{
Message msg = MessageBuilder.withPayload(s).build();
// rocketMQTemplate.getProducer().setVipChannelEnabled(false);
// test1事务组,对应RocketMQLocalTransactionListener中的事务生产者组名称
rocketMQTemplate.sendMessageInTransaction("test1", "ts", msg, null);
}
实现事务监听:
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.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import com.alibaba.fastjson.JSON;
@RocketMQTransactionListener(txProducerGroup = "test1")
public class TransactionListenerImpl implements RocketMQLocalTransactionListener {
@Override
public RocketMQLocalTransactionState executeLocalTransaction(Message msg, Object arg) {
// ... local transaction process, return bollback, commit or unknown
System.out.println("本地事务和消息发送:" + JSON.toJSONString(msg));
return RocketMQLocalTransactionState.UNKNOWN;
}
@Override
public RocketMQLocalTransactionState checkLocalTransaction(Message msg) {
// ... check transaction status and return bollback, commit or unknown
System.out.println("回查信息:" + JSON.toJSONString(msg));
return RocketMQLocalTransactionState.COMMIT;
}
}
注意liunx上面需要开放端口:9876、10909、10910、10911,不然会出现消费端获取失败,连不上nameserv和broker
3.消费者配置:
application.yml:
rocketmq:
name-server: '127.0.0.1:9876'
代码实现:
对消费者实现ack机制:
import java.util.List;
import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.apache.rocketmq.spring.core.RocketMQPushConsumerLifecycleListener;
import org.springframework.stereotype.Component;
@Component
@RocketMQMessageListener(topic = "ts", consumerGroup = "my-consumerGroup")
public class RocketmqConsumer implements RocketMQListener
,RocketMQPushConsumerLifecycleListener{
private int count = 0;
@Override
public void prepareStart(DefaultMQPushConsumer consumer) {
consumer.registerMessageListener(new MessageListenerConcurrently() {
@Override
public ConsumeConcurrentlyStatus consumeMessage(List
msgs, ConsumeConcurrentlyContext context) {
for (MessageExt messageExt : msgs) {
System.out.println("重试次数:" + messageExt.getReconsumeTimes());
// 注意可以在此处判断重试次数,实现入库插入,记录相关消息,进行下面的业务逻辑处理
if(count == 0) {
count++;
return ConsumeConcurrentlyStatus.RECONSUME_LATER;
}
System.out.println("接受到的消息:" + new String(messageExt.getBody()));
return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
}
return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
}
});
}
@Override
public void onMessage(String message) {
实现RocketMQPushConsumerLifecycleListener监听器之后,此方法不调用
}
}