rocketmqTemplate 事务消息
****************************
引入 jar 包
<dependency>
<groupId>org.apache.rocketmq</groupId>
<artifactId>rocketmq-spring-boot-starter</artifactId>
<version>2.0.3</version>
</dependency>
****************************
相关接口、注解
RocketmqTemplate:发送事务消息
public class RocketMQTemplate extends AbstractMessageSendingTemplate<String> implements InitializingBean, DisposableBean {
***********************
发送事务消息
public TransactionSendResult sendMessageInTransaction(String txProducerGroup, String destination, Message<?> message, Object arg) throws MessagingException {
//txProducerGroup为发送组,destination为消息发送的topic,message为消息体,arg为传递给本地函数参数
事务消息接口:创建本地方法、本地事务状态回调函数
public interface RocketMQLocalTransactionListener {
RocketMQLocalTransactionState executeLocalTransaction(Message var1, Object var2);
RocketMQLocalTransactionState checkLocalTransaction(Message var1);
}
本地事务执行状态
public enum RocketMQLocalTransactionState {
COMMIT,
ROLLBACK,
UNKNOWN;
private RocketMQLocalTransactionState() {
}
}
事务消息注解:标注在RocketMQLocalTransactionListener接口的实现类上,发送事务消息时会自动调用本地方法
@Target({ElementType.TYPE, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface RocketMQTransactionListener {
String txProducerGroup() default "rocketmq_transaction_default_global_name";
int corePoolSize() default 1;
int maximumPoolSize() default 1;
long keepAliveTime() default 60000L;
int blockingQueueSize() default 2000;
String accessKey() default "${rocketmq.producer.access-key}";
String secretKey() default "${rocketmq.producer.secret-key}";
}
***************************
示例
*****************
producer 端
TransactionProducerService
@Service
public class TransactionProducerService {
@Resource
private RocketMQTemplate rocketMQTemplate;
public void sendInTransaction(){
for(int i=0;i<100;i++){
TransactionSendResult result=rocketMQTemplate.sendMessageInTransaction("transaction",
"topic-3", MessageBuilder.withPayload("瓜田李下 事务消息"+i).build(), i);
System.out.println(result);
}
}
}
@Component
@RocketMQTransactionListener(txProducerGroup = "transaction")
class LocalExecutor implements RocketMQLocalTransactionListener {
private ConcurrentHashMap<Integer, RocketMQLocalTransactionState> map=new ConcurrentHashMap<>();
@Override
public RocketMQLocalTransactionState executeLocalTransaction(Message message, Object o) {
map.put(message.hashCode(),RocketMQLocalTransactionState.UNKNOWN);
int i=Integer.parseInt(o.toString());
if(i==2){
System.out.println("本地事务出错,回滚事务消息");
map.put(message.hashCode(),RocketMQLocalTransactionState.ROLLBACK);
}else {
map.put(message.hashCode(),RocketMQLocalTransactionState.COMMIT);
}
return map.get(message.hashCode());
}
@Override
public RocketMQLocalTransactionState checkLocalTransaction(Message message) {
return map.get(message.hashCode());
}
}
*****************
consumer 端
ConsumerService3
@Service
@RocketMQMessageListener(consumerGroup = "consumerGroup-3",topic = "topic-3",selectorExpression = "*")
public class ConsumerService3 implements RocketMQListener<String> {
@Override
public void onMessage(String s) {
System.out.println("消费时间:"+System.currentTimeMillis());
System.out.println(s);
}
}
***************************
控制台输出
说明:id为2的消息发送失败,事务回滚