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

spring集成:消息不会从生产者端点传递到消费者端点,spring,spring-integration,Spring,Spring Integration,我编写了一个程序,其中生产者端点向“inputChannel”发送消息,消费者端点从inputChannel读取消息并向ackChannel发送回复 查找下面的代码片段 @Component public class ProducerEndpoint { @ServiceActivator(outputChannel = "inputChannel") public Message<String> produceMessage(String me

我编写了一个程序,其中生产者端点向“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);
    }

}
但是,当我将消息直接发送到inputChannel时,它由ConsumerMessage方法接收,并将reply发送到ReceiveAcknowledge方法

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

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

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

}

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

您似乎误解了Spring集成的概念

直接调用
@ServiceActivator
方法与消息传递无关

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

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

producerEndpoint.produceMessage("Hello World");
@Component
@MessagingGateway(name = "myGateway", defaultRequestChannel = "inputChannel")
public interface ProducerEndpoint {

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

}