【问题标题】:spring integration: message is not passed to the consumer endpoint from producer endpoint春季集成:消息未从生产者端点传递到消费者端点
【发布时间】:2021-04-02 13:53:15
【问题描述】:

我写了一个程序,生产者端点向'inputChannel'发送消息,消费者端点从inputChannel读取消息并将回复发送到ackChannel。

找到下面的代码sn-p。

@Component
public class ProducerEndpoint {

    @ServiceActivator(outputChannel = "inputChannel")
    public Message<String> produceMessage(String message) {
        return MessageBuilder.withPayload("Message Received").build();
    }

    @ServiceActivator(inputChannel = "ackChannel")
    public void receiveAcknowledgement(String message) {
        System.out.println("From Consumer : " + message);
    }

}

消费者ednpoint

@Component
public class ConsumerEndpoint {
    
    @ServiceActivator(inputChannel = "inputChannel", outputChannel = "ackChannel", requiresReply="true")
    public Message<String> consumeMessage(Message<String> message) {
        System.out.println("From Producer : " + message);
        return MessageBuilder.withPayload("Message Received").build();
    }
}

当我使用produceMessage 方法向生产者端点发送消息时,它没有到达消费者'consumeMessage' 方法。我在这里有什么遗漏吗?

producerEndpoint.produceMessage("Hello World");

但是当我直接向inputChannel发送消息时,它是由consumeMessage方法接收并发送到receiveAcknowledgement方法的回复。

当我将 ProducerEndpoint 建模为如下所示的 MessagingGateway 时,一切正常。

@Component
@MessagingGateway(name = "myGateway", defaultRequestChannel = "inputChannel")
public interface ProducerEndpoint {

    @Gateway(requestChannel = "inputChannel", replyTimeout = 2, requestTimeout = 200)
    public void produceMessage(String message);
    

}

当我直接调用方法时,服务激活器不能将消息发送到输出通道吗?

【问题讨论】:

    标签: spring spring-integration


    【解决方案1】:

    您似乎误解了 Spring Integration 的概念。

    直接调用@ServiceActivator 方法对消息传递没有任何作用。

    您需要使用网关或其他消息传递机制将消息发送到端点。

    可以使用 @Publish 注释任意方法,以将方法调用的结果作为消息发布。

    https://docs.spring.io/spring-integration/docs/current/reference/html/message-publishing.html#message-publishing

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-01-29
      • 2015-09-18
      • 1970-01-01
      • 2022-09-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多