如何使用Kotlin协程在Spring AMQP中使用消息?

时间:2019-11-06 11:58:04

标签: spring-boot kotlin spring-amqp kotlin-coroutines spring-rabbitmq

我已经read自从2.2.0版开始,Spring Boot就支持Kotlin Coroutines。但是,当我使用spring amqp(版本2.2.0)尝试这样做时:

@RabbitListener(...)
@Component
class EventConsumer {

    @RabbitHandler
    suspend fun consumeEvent(e: MyEvent) {
        ...
    }
}

然后发布一个事件:

rabbitTemplate.convertAndSend("my-exchange", "my-routing-key", MyEvent("data"))

我收到以下错误:

org.springframework.amqp.rabbit.support.ListenerExecutionFailedException: Listener method could not be invoked with the incoming message
Endpoint handler details:
Method [public java.lang.Object com.x.y.EventConsumer(com.x.y.MyEvent,kotlin.coroutines.Continuation<? super kotlin.Unit>)]
Bean [com.x.y.EventConsumer@1fce0403]
    at org.springframework.amqp.rabbit.listener.adapter.MessagingMessageListenerAdapter.invokeHandler(MessagingMessageListenerAdapter.java:214) ~[spring-rabbit-2.2.0.RELEASE.jar:2.2.0.RELEASE]
    at org.springframework.amqp.rabbit.listener.adapter.MessagingMessageListenerAdapter.invokeHandlerAndProcessResult(MessagingMessageListenerAdapter.java:143) ~[spring-rabbit-2.2.0.RELEASE.jar:2.2.0.RELEASE]
    at org.springframework.amqp.rabbit.listener.adapter.MessagingMessageListenerAdapter.onMessage(MessagingMessageListenerAdapter.java:132) ~[spring-rabbit-2.2.0.RELEASE.jar:2.2.0.RELEASE]
    at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.doInvokeListener(AbstractMessageListenerContainer.java:1569) ~[spring-rabbit-2.2.0.RELEASE.jar:2.2.0.RELEASE]
    at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.actualInvokeListener(AbstractMessageListenerContainer.java:1488) ~[spring-rabbit-2.2.0.RELEASE.jar:2.2.0.RELEASE]
    at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.invokeListener(AbstractMessageListenerContainer.java:1476) ~[spring-rabbit-2.2.0.RELEASE.jar:2.2.0.RELEASE]
    at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.doExecuteListener(AbstractMessageListenerContainer.java:1467) ~[spring-rabbit-2.2.0.RELEASE.jar:2.2.0.RELEASE]
    at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.executeListener(AbstractMessageListenerContainer.java:1411) ~[spring-rabbit-2.2.0.RELEASE.jar:2.2.0.RELEASE]
    at org.springframework.amqp.rabbit.listener.DirectMessageListenerContainer$SimpleConsumer.callExecuteListener(DirectMessageListenerContainer.java:1035) ~[spring-rabbit-2.2.0.RELEASE.jar:2.2.0.RELEASE]
    at org.springframework.amqp.rabbit.listener.DirectMessageListenerContainer$SimpleConsumer.handleDelivery(DirectMessageListenerContainer.java:995) ~[spring-rabbit-2.2.0.RELEASE.jar:2.2.0.RELEASE]

如果我删除了suspend关键字,那么一切都会按预期进行。

我需要对Spring AMQP进行一些特殊配置才能与Kotlin Coroutines一起使用吗?

0 个答案:

没有答案