0
votes

I written a program, where producer endpoint send messages to 'inputChannel' and consumer endpoint read the message from inputChannel and send the reply to ackChannel.

Find the below code snippet.

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

}

Consumer 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();
    }
}

When I send message to producer endpoint using produceMessage method, it is not reaching to consumer 'consumeMessage' method. Am I missing anything here?

producerEndpoint.produceMessage("Hello World");

But When I send the message directly to inputChannel, it is received by consumeMessage method and reply sent to receiveAcknowledgement method.

Things are working fine, when i modelled ProducerEndpoint as MessagingGateway like below.

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

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

}

Can't a service activator send the message to outputchannel when i call the method directly?

1

1 Answers

1
votes

It seems you have misunderstood Spring Integration concepts.

Calling a @ServiceActivator method directly does nothing with regard to messaging.

You need to use a gateway or another messaging mechanism to send a message to an endpoint.

You can annotate arbitrary methods with @Publish to publish the results of a method call as a message.

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