INT-4361: Add a role() hook to Java DSL
JIRA: https://jira.spring.io/browse/INT-4361 * To get a gain of the method chain code flow and avoid extra annotation parsing, add `.role(String)` hook to the `EndpointSpec` * Delegate the provided `role` property to the `AbstractEndpoint` * Register `AbstractEndpoint` as itself `SmartLifecycle` in the `SmartLifecycleRoleController` * Add `destroy()` to the `AbstractEndpoint` and remove it from the `SmartLifecycleRoleController` * Provide some Java 8 code style refactoring * Rework XML parsers and Annotation processors to populate `role` property on the `AbstractEndpoint` * Wrap `roleController` bean extraction to the `NoSuchBeanDefinitionException` catch * Fix several `AbstractEndpoint` implementation to properly call `super.onInit()` which has been missed before
This commit is contained in:
committed by
Gary Russell
parent
a61327766e
commit
f7e75223c7
@@ -57,8 +57,10 @@ import org.springframework.integration.dsl.context.IntegrationFlowContext;
|
||||
import org.springframework.integration.dsl.context.IntegrationFlowRegistration;
|
||||
import org.springframework.integration.endpoint.MessageProducerSupport;
|
||||
import org.springframework.integration.handler.AbstractReplyProducingMessageHandler;
|
||||
import org.springframework.integration.support.SmartLifecycleRoleController;
|
||||
import org.springframework.messaging.Message;
|
||||
import org.springframework.messaging.MessageChannel;
|
||||
import org.springframework.messaging.MessageDeliveryException;
|
||||
import org.springframework.messaging.MessageHandler;
|
||||
import org.springframework.messaging.MessagingException;
|
||||
import org.springframework.messaging.PollableChannel;
|
||||
@@ -85,6 +87,9 @@ public class ManualFlowTests {
|
||||
@Autowired
|
||||
private BeanFactory beanFactory;
|
||||
|
||||
@Autowired
|
||||
private SmartLifecycleRoleController roleController;
|
||||
|
||||
@Test
|
||||
public void testWithAnonymousMessageProducerStart() {
|
||||
final AtomicBoolean started = new AtomicBoolean();
|
||||
@@ -99,8 +104,8 @@ public class ManualFlowTests {
|
||||
};
|
||||
QueueChannel channel = new QueueChannel();
|
||||
IntegrationFlow flow = IntegrationFlows.from(producer)
|
||||
.channel(channel)
|
||||
.get();
|
||||
.channel(channel)
|
||||
.get();
|
||||
this.integrationFlowContext.registration(flow).register();
|
||||
assertTrue(started.get());
|
||||
}
|
||||
@@ -127,8 +132,8 @@ public class ManualFlowTests {
|
||||
MyProducerSpec spec = new MyProducerSpec(new MyProducer());
|
||||
QueueChannel channel = new QueueChannel();
|
||||
IntegrationFlow flow = IntegrationFlows.from(spec.id("foo"))
|
||||
.channel(channel)
|
||||
.get();
|
||||
.channel(channel)
|
||||
.get();
|
||||
this.integrationFlowContext.registration(flow).register();
|
||||
assertTrue(started.get());
|
||||
}
|
||||
@@ -306,6 +311,51 @@ public class ManualFlowTests {
|
||||
assertEquals("test", receive.getPayload());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRoleControl() {
|
||||
String testRole = "bridge";
|
||||
|
||||
PollableChannel resultChannel = new QueueChannel();
|
||||
|
||||
IntegrationFlowRegistration flowRegistration =
|
||||
this.integrationFlowContext
|
||||
.registration(flow -> flow
|
||||
.bridge(e -> e.role(testRole))
|
||||
.channel(resultChannel))
|
||||
.register();
|
||||
|
||||
MessagingTemplate messagingTemplate =
|
||||
this.integrationFlowContext.messagingTemplateFor(flowRegistration.getId());
|
||||
|
||||
messagingTemplate.send(new GenericMessage<>("test"));
|
||||
|
||||
Message<?> receive = resultChannel.receive(1000);
|
||||
assertNotNull(receive);
|
||||
assertEquals("test", receive.getPayload());
|
||||
|
||||
this.roleController.stopLifecyclesInRole(testRole);
|
||||
|
||||
try {
|
||||
messagingTemplate.send(new GenericMessage<>("test2"));
|
||||
}
|
||||
catch (Exception e) {
|
||||
assertThat(e, instanceOf(MessageDeliveryException.class));
|
||||
assertThat(e.getMessage(), containsString("Dispatcher has no subscribers for channel"));
|
||||
}
|
||||
|
||||
this.roleController.startLifecyclesInRole(testRole);
|
||||
|
||||
messagingTemplate.send(new GenericMessage<>("test2"));
|
||||
|
||||
receive = resultChannel.receive(1000);
|
||||
assertNotNull(receive);
|
||||
assertEquals("test2", receive.getPayload());
|
||||
|
||||
flowRegistration.destroy();
|
||||
|
||||
assertTrue(this.roleController.getEndpointsRunningStatus(testRole).isEmpty());
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@EnableIntegration
|
||||
public static class RootConfiguration {
|
||||
|
||||
Reference in New Issue
Block a user