DirectChannel invokes a single subscriber for each sent Message. For example if you have more than one subscriber, then a message is delivered the exactly one subscriber at any point of time.
How the messages are distributed among multiple subscribers?
Messages are distributed to multiple subscribers in round-robbin fashion by default.
Message 0 is sent to endpoint1, message 1 to endpoint2 and message 3 to endpoint3 etc.,
What if any subscriber (for ex: endpoint2) failed to consume the messages?
Then the message is delivered to other working consumer.
For example, let’s throw a runtime exception from consumer2 and validate the scenario.
@Component
public class ConsumerEndpoint2 {
@ServiceActivator(inputChannel = "directChannel")
@Order(1)
public Message<String> consumeMessage(Message<String> message) {
throw new RuntimeException("Consumer 2 is down");
}
}
Follow below step-by-step procedure to build complete working application.
Step 1: Create new maven project ‘direct-channel-failover-demo’.
Step 2: Update pom.xml with maven dependencies.
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.sample.app</groupId>
<artifactId>direct-channel-failover-demo</artifactId>
<version>1</version>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-parent -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.0</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-integration</artifactId>
</dependency>
</dependencies>
</project>
Step 3: Define consumer endpoints.
ConsumerEndpoint1.java
package com.sample.app.endpoints;
import org.springframework.core.annotation.Order;
import org.springframework.integration.annotation.ServiceActivator;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.messaging.Message;
import org.springframework.stereotype.Component;
@Component
public class ConsumerEndpoint1 {
@ServiceActivator(inputChannel = "directChannel")
@Order(3)
public Message<String> consumeMessage(Message<String> message) {
System.out.println("ConsumerEndpoint1 -> Received message from gateway : " + message.getPayload());
return MessageBuilder.withPayload("Message '" + message.getPayload() + "' received by ConsumerEndpoint1")
.build();
}
}
ConsumerEndpoint2.java
package com.sample.app.endpoints;
import org.springframework.core.annotation.Order;
import org.springframework.integration.annotation.ServiceActivator;
import org.springframework.messaging.Message;
import org.springframework.stereotype.Component;
@Component
public class ConsumerEndpoint2 {
@ServiceActivator(inputChannel = "directChannel")
@Order(1)
public Message<String> consumeMessage(Message<String> message) {
throw new RuntimeException("Consumer 2 is down");
}
}
ConsumerEndpoint3.java
package com.sample.app.endpoints;
import org.springframework.core.annotation.Order;
import org.springframework.integration.annotation.ServiceActivator;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.messaging.Message;
import org.springframework.stereotype.Component;
@Component
public class ConsumerEndpoint3 {
@ServiceActivator(inputChannel = "directChannel")
@Order(2)
public Message<String> consumeMessage(Message<String> message) {
System.out.println("ConsumerEndpoint3 -> Received message from gateway : " + message.getPayload());
return MessageBuilder.withPayload("Message '" + message.getPayload() + "' received by ConsumerEndpoint3")
.build();
}
}
Step 4: Define CustomGateway class.
CustomGateway.java
package com.sample.app.endpoints;
import org.springframework.integration.annotation.Gateway;
import org.springframework.integration.annotation.MessagingGateway;
import org.springframework.messaging.Message;
@MessagingGateway(name = "myGateway", defaultRequestChannel = "directChannel")
public interface CustomGateway {
@Gateway(requestChannel = "directChannel")
public Message<String> print(Message<String> message);
}
Step 5: Define main application class.
App.java
package com.sample.app;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.integration.channel.DirectChannel;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.messaging.Message;
import com.sample.app.endpoints.CustomGateway;
@SpringBootApplication
@Configuration
public class App {
@Autowired
@Qualifier("directChannel")
private DirectChannel directChannel;
@Autowired
private CustomGateway customGateway;
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
@Bean(name = "directChannel")
public DirectChannel channel1() {
DirectChannel channel = new DirectChannel();
return channel;
}
@Bean
public CommandLineRunner demo() {
return (args) -> {
for (int i = 0; i < 10; i++) {
Message<String> message = MessageBuilder.withPayload("Msg " + i).build();
Message<String> result = customGateway.print(message);
System.out.println(result.getPayload());
}
};
}
}
Total project structure looks like below.
Run App.java, you will see below messages in console.
ConsumerEndpoint3 -> Received message from gateway : Msg 0
Message 'Msg 0' received by ConsumerEndpoint3
ConsumerEndpoint3 -> Received message from gateway : Msg 1
Message 'Msg 1' received by ConsumerEndpoint3
2021-04-03 19:44:08.468 INFO 24236 --- [ main] o.s.i.h.s.MessagingMethodInvokerHelper : Overriding default instance of MessageHandlerMethodFactory with provided one.
ConsumerEndpoint1 -> Received message from gateway : Msg 2
Message 'Msg 2' received by ConsumerEndpoint1
2021-04-03 19:44:08.469 INFO 24236 --- [ main] o.s.i.dispatcher.UnicastingDispatcher : An exception was thrown by 'ServiceActivator for [org.springframework.integration.handler.MethodInvokingMessageProcessor@53499d85] (consumerEndpoint2.consumeMessage.serviceActivator)' while handling 'GenericMessage [payload=Msg 3, headers={replyChannel=org.springframework.messaging.core.GenericMessagingTemplate$TemporaryReplyChannel@770d0ea6, errorChannel=org.springframework.messaging.core.GenericMessagingTemplate$TemporaryReplyChannel@770d0ea6, id=6aa559b2-6e97-7a1c-1c80-5fe39cc9e7c3, timestamp=1617459248469}]': error occurred during processing message in 'MethodInvokingMessageProcessor' [org.springframework.integration.handler.MethodInvokingMessageProcessor@53499d85]; nested exception is java.lang.RuntimeException: Consumer 2 is down. Failing over to the next subscriber.
ConsumerEndpoint3 -> Received message from gateway : Msg 3
Message 'Msg 3' received by ConsumerEndpoint3
ConsumerEndpoint3 -> Received message from gateway : Msg 4
Message 'Msg 4' received by ConsumerEndpoint3
ConsumerEndpoint1 -> Received message from gateway : Msg 5
Message 'Msg 5' received by ConsumerEndpoint1
2021-04-03 19:44:08.470 INFO 24236 --- [ main] o.s.i.dispatcher.UnicastingDispatcher : An exception was thrown by 'ServiceActivator for [org.springframework.integration.handler.MethodInvokingMessageProcessor@53499d85] (consumerEndpoint2.consumeMessage.serviceActivator)' while handling 'GenericMessage [payload=Msg 6, headers={replyChannel=org.springframework.messaging.core.GenericMessagingTemplate$TemporaryReplyChannel@476aac9, errorChannel=org.springframework.messaging.core.GenericMessagingTemplate$TemporaryReplyChannel@476aac9, id=ec3f29f0-10c0-3b9a-3ed3-34f34c33d826, timestamp=1617459248470}]': error occurred during processing message in 'MethodInvokingMessageProcessor' [org.springframework.integration.handler.MethodInvokingMessageProcessor@53499d85]; nested exception is java.lang.RuntimeException: Consumer 2 is down. Failing over to the next subscriber.
ConsumerEndpoint3 -> Received message from gateway : Msg 6
Message 'Msg 6' received by ConsumerEndpoint3
ConsumerEndpoint3 -> Received message from gateway : Msg 7
Message 'Msg 7' received by ConsumerEndpoint3
ConsumerEndpoint1 -> Received message from gateway : Msg 8
Message 'Msg 8' received by ConsumerEndpoint1
2021-04-03 19:44:08.472 INFO 24236 --- [ main] o.s.i.dispatcher.UnicastingDispatcher : An exception was thrown by 'ServiceActivator for [org.springframework.integration.handler.MethodInvokingMessageProcessor@53499d85] (consumerEndpoint2.consumeMessage.serviceActivator)' while handling 'GenericMessage [payload=Msg 9, headers={replyChannel=org.springframework.messaging.core.GenericMessagingTemplate$TemporaryReplyChannel@1eba372c, errorChannel=org.springframework.messaging.core.GenericMessagingTemplate$TemporaryReplyChannel@1eba372c, id=b96b647b-deb1-8d20-3f0a-d321f654f552, timestamp=1617459248472}]': error occurred during processing message in 'MethodInvokingMessageProcessor' [org.springframework.integration.handler.MethodInvokingMessageProcessor@53499d85]; nested exception is java.lang.RuntimeException: Consumer 2 is down. Failing over to the next subscriber.
ConsumerEndpoint3 -> Received message from gateway : Msg 9
Message 'Msg 9' received by ConsumerEndpoint3
From the output log, you can confirm following message.
java.lang.RuntimeException: Consumer 2 is down. Failing over to the next subscriber.
You can download complete working application from below link.
No comments:
Post a Comment