spring 集成:消息未从生产者端点传递到消费者端点

spring integration: message is not passed to the consumer endpoint from producer endpoint

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

找到下面的代码片段。

@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);
    }

}

消费积分

@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 集成概念。

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

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

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

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