@Test void testRuntimeBeanRegistration(@Autowired IntegrationFlowContext context) throws Exception { Set<ObjectName> query = this.server.queryNames(new ObjectName("dsl:type=MessageChannel,*"), null); assertThat(query).hasSize(3); query = this.server.queryNames(new ObjectName("dsl:type=MessageHandler,*"), null); assertThat(query).hasSize(2); query = this.server.queryNames(new ObjectName("dsl:type=MessageSource,*"), null); assertThat(query).hasSize(0); IntegrationFlow dynamicFlow = IntegrationFlows.from(() -> "foo", e -> e.poller(p -> p.fixedDelay(1000))) .channel("channelTwo") .nullChannel(); IntegrationFlowContext.IntegrationFlowRegistration registration = context.registration(dynamicFlow) .id("dynamic") .register(); query = this.server.queryNames(new ObjectName("dsl:type=MessageChannel,*"), null); assertThat(query).hasSize(4); query = this.server.queryNames(new ObjectName("dsl:type=MessageHandler,*"), null); assertThat(query).hasSize(3); query = this.server.queryNames(new ObjectName("dsl:type=MessageSource,*"), null); assertThat(query).hasSize(1); registration.destroy(); query = this.server.queryNames(new ObjectName("dsl:type=MessageChannel,*"), null); assertThat(query).hasSize(3); query = this.server.queryNames(new ObjectName("dsl:type=MessageHandler,*"), null); assertThat(query).hasSize(2); query = this.server.queryNames(new ObjectName("dsl:type=MessageSource,*"), null); assertThat(query).hasSize(0); }
private void register(StandardIntegrationFlowRegistrationBuilder builder) { IntegrationFlow integrationFlow = builder.integrationFlowRegistration.getIntegrationFlow(); String flowId = builder.integrationFlowRegistration.getId(); Lock registerBeanLock = null; try { builder.integrationFlowRegistration.setIntegrationFlow(integrationFlow); builder.integrationFlowRegistration.start();
@Test public void testFluxMessageChannelCleanUp() throws InterruptedException { FluxMessageChannel flux = MessageChannels.flux().get(); CountDownLatch finishLatch = new CountDownLatch(1); IntegrationFlow testFlow = f -> f .<String>split(__ -> Flux.fromStream(IntStream.range(0, 100).boxed()), null) .channel(flux) .aggregate(a -> a.releaseStrategy(m -> m.size() == 100)) .handle(__ -> finishLatch.countDown()); IntegrationFlowContext.IntegrationFlowRegistration flowRegistration = this.integrationFlowContext.registration(testFlow) .register(); flowRegistration.getInputChannel().send(new GenericMessage<>("foo")); assertTrue(finishLatch.await(10, TimeUnit.SECONDS)); assertTrue(TestUtils.getPropertyValue(flux, "publishers", Map.class).isEmpty()); flowRegistration.destroy(); }
private void register(StandardIntegrationFlowRegistrationBuilder builder) { IntegrationFlow integrationFlow = builder.integrationFlowRegistration.getIntegrationFlow(); String flowId = builder.integrationFlowRegistration.getId(); Lock registerBeanLock = null; try { builder.integrationFlowRegistration.setIntegrationFlow(integrationFlow); builder.integrationFlowRegistration.start();
/** * Register an {@link IntegrationFlow} and all the dependant and support components * in the application context and return an associated {@link IntegrationFlowRegistration} * control object. * @return the {@link IntegrationFlowRegistration} instance. */ @Override public IntegrationFlowRegistration register() { String id = this.integrationFlowRegistration.getId(); Assert.state(!this.idAsPrefix || StringUtils.hasText(id), "An 'id' must be present to use 'useFlowIdAsPrefix'"); if (this.idAsPrefix) { StandardIntegrationFlowContext.this.useFlowIdAsPrefix.put(id, this.idAsPrefix); } StandardIntegrationFlowContext.this.register(this); return this.integrationFlowRegistration; }
/** * Obtain a {@link MessagingTemplate} with its default destination set to the input channel * of the {@link IntegrationFlow} for provided {@code flowId}. * <p> Any {@link IntegrationFlow} bean (not only manually registered) can be used for this method. * <p> If {@link IntegrationFlow} doesn't start with the * {@link org.springframework.messaging.MessageChannel}, the * {@link IllegalStateException} is thrown. * @param flowId the bean name to obtain the input channel from * @return the {@link MessagingTemplate} instance */ @Override public MessagingTemplate messagingTemplateFor(String flowId) { return this.registry.get(flowId) .getMessagingTemplate(); }
/** * Specify an {@code id} for the {@link IntegrationFlow} to register. * Must be unique per context. * The registration with this {@code id} must be destroyed before reusing for * a new {@link IntegrationFlow} instance. * @param id the id for the {@link IntegrationFlow} to register * @return the current builder instance */ @Override public StandardIntegrationFlowRegistrationBuilder id(String id) { this.integrationFlowRegistration.setId(id); return this; }
StandardIntegrationFlowRegistrationBuilder(IntegrationFlow integrationFlow) { this.integrationFlowRegistration = new StandardIntegrationFlowRegistration(integrationFlow); this.integrationFlowRegistration.setBeanFactory(StandardIntegrationFlowContext.this.beanFactory); this.integrationFlowRegistration.setIntegrationFlowContext(StandardIntegrationFlowContext.this); }
.register(); String flowRegistrationId = flowRegistration.getId(); BeanFactoryHandler bean = this.beanFactory.getBean(flowRegistrationId + BeanFactoryHandler.class.getName() + "#0", bean = this.beanFactory.getBean(flowRegistrationId + "." + "anId.handler", BeanFactoryHandler.class); MessagingTemplate messagingTemplate = flowRegistration.getMessagingTemplate(); messagingTemplate.setReceiveTimeout(10000); startsWith(flowId + ".")); flowRegistration.destroy();
assertNotNull(this.integrationManagementConfigurer.getHandlerMetrics("bridge")); flowRegistration.destroy();
@Test public void testDynamicHttpEndpoint() throws Exception { IntegrationFlow flow = IntegrationFlows.from(Http.inboundGateway("/dynamic") .requestMapping(r -> r.params("name")) .payloadExpression("#requestParams.name[0]")) .<String, String>transform(String::toLowerCase) .get(); IntegrationFlowContext.IntegrationFlowRegistration flowRegistration = this.integrationFlowContext.registration(flow).register(); this.mockMvc.perform( get("/dynamic") .with(httpBasic("admin", "admin")) .param("name", "BAR")) .andExpect( content() .string("bar")); flowRegistration.destroy(); this.mockMvc.perform( get("/dynamic") .with(httpBasic("admin", "admin")) .param("name", "BAZ")) .andExpect( status() .isNotFound()); }
@Test public void testWithAnonymousMessageProducerSpecStart() { final AtomicBoolean started = new AtomicBoolean(); class MyProducer extends MessageProducerSupport { @Override protected void doStart() { started.set(true); super.doStart(); } } class MyProducerSpec extends MessageProducerSpec<MyProducerSpec, MyProducer> { MyProducerSpec(MyProducer producer) { super(producer); } } MyProducerSpec spec = new MyProducerSpec(new MyProducer()); QueueChannel channel = new QueueChannel(); IntegrationFlow flow = IntegrationFlows.from(spec.id("fooChannel")) .channel(channel) .get(); IntegrationFlowRegistration flowRegistration = this.integrationFlowContext.registration(flow).register(); assertTrue(started.get()); flowRegistration.destroy(); }
@Test public void testRegistrationDuplicationRejected() { String testId = "testId"; StandardIntegrationFlow testFlow = IntegrationFlows.from(Supplier.class) .get(); IntegrationFlowRegistration flowRegistration = this.integrationFlowContext .registration(testFlow) .id(testId) .register(); try { this.integrationFlowContext .registration(testFlow) .id(testId) .register(); } catch (Exception e) { assertThat(e, instanceOf(IllegalArgumentException.class)); assertThat(e.getMessage(), containsString("with flowId '" + testId + "' is already registered.")); } flowRegistration.destroy(); }
@Test public void testFtpOutboundFlow() { IntegrationFlow flow = f -> f .handle(Ftp.outboundAdapter(sessionFactory(), FileExistsMode.FAIL) .useTemporaryFileName(false) .fileNameExpression("headers['" + FileHeaders.FILENAME + "']") .remoteDirectory("ftpTarget")); IntegrationFlowRegistration registration = this.flowContext.registration(flow).register(); String fileName = "foo.file"; Message<ByteArrayInputStream> message = MessageBuilder .withPayload(new ByteArrayInputStream("foo".getBytes(StandardCharsets.UTF_8))) .setHeader(FileHeaders.FILENAME, fileName) .build(); registration.getInputChannel().send(message); RemoteFileTemplate<FTPFile> template = new RemoteFileTemplate<>(sessionFactory()); FTPFile[] files = template.execute(session -> session.list(getTargetRemoteDirectory().getName() + "/" + fileName)); assertEquals(1, files.length); assertEquals(3, files[0].getSize()); registration.destroy(); }
@Test public void testDynamicHttpEndpoint() throws Exception { IntegrationFlow flow = IntegrationFlows.from(WebFlux.inboundGateway("/dynamic") .requestMapping(r -> r.params("name")) .payloadExpression("#requestParams.name[0]")) .<String, String>transform(String::toLowerCase) .get(); IntegrationFlowContext.IntegrationFlowRegistration flowRegistration = this.integrationFlowContext.registration(flow).register(); this.webTestClient.get().uri("/dynamic?name=BAR") .headers(headers -> headers.setBasicAuth("guest", "guest")) .exchange() .expectBody(String.class) .isEqualTo("bar"); flowRegistration.destroy(); this.webTestClient.get().uri("/dynamic?name=BAZ") .headers(headers -> headers.setBasicAuth("guest", "guest")) .exchange() .expectStatus() .isNotFound(); }
@Test public void testSftpOutboundFlow() { IntegrationFlow flow = f -> f.handle(Sftp.outboundAdapter(sessionFactory(), FileExistsMode.FAIL) .useTemporaryFileName(false) .fileNameExpression("headers['" + FileHeaders.FILENAME + "']") .remoteDirectory("sftpTarget")); IntegrationFlowRegistration registration = this.flowContext.registration(flow).register(); String fileName = "foo.file"; registration.getInputChannel().send(MessageBuilder.withPayload("foo") .setHeader(FileHeaders.FILENAME, fileName) .build()); RemoteFileTemplate<ChannelSftp.LsEntry> template = new RemoteFileTemplate<>(sessionFactory()); ChannelSftp.LsEntry[] files = template.execute(session -> session.list(getTargetRemoteDirectory().getName() + "/" + fileName)); assertEquals(1, files.length); assertEquals(3, files[0].getAttrs().getSize()); registration.destroy(); }
/** * Register an {@link IntegrationFlow} and all the dependant and support components * in the application context and return an associated {@link IntegrationFlowRegistration} * control object. * @return the {@link IntegrationFlowRegistration} instance. */ @Override public IntegrationFlowRegistration register() { String id = this.integrationFlowRegistration.getId(); Assert.state(!this.idAsPrefix || StringUtils.hasText(id), "An 'id' must be present to use 'useFlowIdAsPrefix'"); if (this.idAsPrefix) { StandardIntegrationFlowContext.this.useFlowIdAsPrefix.put(id, this.idAsPrefix); } StandardIntegrationFlowContext.this.register(this); return this.integrationFlowRegistration; }
/** * Specify an {@code id} for the {@link IntegrationFlow} to register. * Must be unique per context. * The registration with this {@code id} must be destroyed before reusing for * a new {@link IntegrationFlow} instance. * @param id the id for the {@link IntegrationFlow} to register * @return the current builder instance */ @Override public StandardIntegrationFlowRegistrationBuilder id(String id) { this.integrationFlowRegistration.setId(id); return this; }