apache / rocketmq-spring

Apache RocketMQ Spring Integration
https://rocketmq.apache.org/
Apache License 2.0
2.11k stars 898 forks source link

The rocketMQTemplate does not exist TransactionListener #395

Open kiritokun07 opened 3 years ago

kiritokun07 commented 3 years ago

1.环境

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.2</version>
        <relativePath/> <!-- lookup parent from repository -->
</parent>

<dependency>
<groupId>org.apache.rocketmq</groupId>
<artifactId>rocketmq-spring-boot-starter</artifactId>
<version>2.2.1</version>
</dependency>

2.我做了什么 简单实现了RocketMQLocalTransactionListener接口的两个方法,都是返回RocketMQLocalTransactionState.COMMIT

@Service
@RocketMQTransactionListener
public class OrderServiceImpl4Listener implements RocketMQLocalTransactionListener {
}

在Controller中发送了一个事务消息

@Resource
    private RocketMQTemplate rocketMQTemplate;
@RequestMapping("/test2")
    public void test2() {
        Message<String> message = MessageBuilder.withPayload("发送了一个事务消息")
                .setHeader(RocketMQHeaders.TRANSACTION_ID,"KEY_").build();
        rocketMQTemplate.sendMessageInTransaction(topic, message, null);
}

3.报错

java.lang.IllegalStateException: The rocketMQTemplate does not exist TransactionListener at org.apache.rocketmq.spring.core.RocketMQTemplate.sendMessageInTransaction(RocketMQTemplate.java:1014) ~[rocketmq-spring-boot-2.2.1.jar:2.2.1] 进入sendMessageInTransaction方法后,因为transactionListener不存在抛出异常了

这篇文章说:In sendMessageInTransaction(), the first parameter transaction name ("test") must be same with the @RocketMQTransactionListener's member field 'transName' 但是由于版本的问题,我没有办法设置transName 事务消息 我也曾试图使用一个新的Template,然后在Controller中注入新的template,但是仍然报错

@ExtRocketMQTemplateConfiguration
public class ExtRocketMQTemplate extends RocketMQTemplate {
}
kiritokun07 commented 3 years ago

消费者也是有的

@Component("MyNameServerConsumer")
@RocketMQMessageListener(
        topic = "${rocketmq.topic.test}",
        consumerGroup = "${rocketmq.consumer.group}"
)
public class MyNameServerConsumer implements RocketMQListener<String> {

    @Override
    public void onMessage(String s) {
        System.out.println("收到消息:s = " + s);
    }
}
Macperter commented 2 years ago

我也遇到了这个问题 我的 rocketmq starter 是 2.2.2 ,最后有解决吗

lszzxc commented 1 year ago

检查一下你的监听器是在哪里,TransactionListener

tyza66 commented 1 year ago

+1

tyza66 commented 1 year ago

检查一下@RocketMQTransactionListener文件最上面的包引入(package)是否和你当前的项目结构相符

tyza66 commented 1 year ago

因为我是从别的工程复制过来的,没注意这里,刚才我发现我的package语句报红了

Azusaaaaaaaa commented 1 year ago

我也遇到了这个问题,请问有人解决了吗

Azusaaaaaaaa commented 1 year ago

我这边的问题好像解决了,出现这种情况的原因是使用@PostConstruct或junit测试,RocketMQTemplate里的TransactionListener没来得及注入进去。我把发消息的实现放到接口中,等项目启动起来后请求接口测试就好使了