.get(); IntegrationFlowContext integrationFlowContext = this.beanFactory.getBean(IntegrationFlowContext.class); integrationFlowContext.registration(standardIntegrationFlow) .autoStartup(false) .register();
StandardIntegrationFlowRegistrationBuilder(IntegrationFlow integrationFlow) { this.integrationFlowRegistration = new StandardIntegrationFlowRegistration(integrationFlow); this.integrationFlowRegistration.setBeanFactory(StandardIntegrationFlowContext.this.beanFactory); this.integrationFlowRegistration.setIntegrationFlowContext(StandardIntegrationFlowContext.this); }
/** * Associate provided {@link IntegrationFlow} with an {@link StandardIntegrationFlowRegistrationBuilder} * for additional options and farther registration in the application context. * @param integrationFlow the {@link IntegrationFlow} to register * @return the IntegrationFlowRegistrationBuilder associated with the provided {@link IntegrationFlow} */ @Override public StandardIntegrationFlowRegistrationBuilder registration(IntegrationFlow integrationFlow) { return new StandardIntegrationFlowRegistrationBuilder(integrationFlow); }
@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 testIncludesDynamic() { Graph graph = this.server.getGraph(); assertThat(graph.getNodes().size()).isEqualTo(32); IntegrationFlow flow = f -> f.handle(m -> { }); IntegrationFlowRegistration reg = this.flowContext.registration(flow).register(); graph = this.server.rebuild(); assertThat(graph.getNodes().size()).isEqualTo(34); this.flowContext.remove(reg.getId()); graph = this.server.rebuild(); assertThat(graph.getNodes().size()).isEqualTo(32); }
@Test public void testDynamicAdapterFlow() { IntegrationFlowRegistration flowRegistration = this.integrationFlowContext.registration(new MyFlowAdapter()).register(); PollableChannel resultChannel = this.beanFactory.getBean("flowAdapterOutput", PollableChannel.class); Message<?> receive = resultChannel.receive(1000); assertNotNull(receive); assertEquals("flowAdapterMessage", receive.getPayload()); flowRegistration.destroy(); }
private void register(StandardIntegrationFlowRegistrationBuilder builder) { IntegrationFlow integrationFlow = builder.integrationFlowRegistration.getIntegrationFlow(); String flowId = builder.integrationFlowRegistration.getId(); Lock registerBeanLock = null; try { registerBeanLock = this.registerFlowsLock; registerBeanLock.lock(); flowId = generateBeanName(integrationFlow, null); builder.id(flowId); integrationFlow = (IntegrationFlow) registerBean(integrationFlow, flowId, null); builder.integrationFlowRegistration.setIntegrationFlow(integrationFlow); builder.additionalBeans.forEach((key, value) -> registerBean(key, value, theFlowId)); builder.integrationFlowRegistration.start();
@Test public void testDisabledBeansOverride() { assertThatThrownBy( () -> this.integrationFlowContext .registration(f -> f.channel(c -> c.direct("doNotOverrideChannel"))) .register()) .isExactlyInstanceOf(BeanCreationException.class) .hasCauseExactlyInstanceOf(BeanDefinitionOverrideException.class) .hasMessageContaining("Invalid bean definition with name 'doNotOverrideChannel'"); }
/** * Destroy the {@link IntegrationFlow} bean (as well as all its dependant beans) * and clean up all the local cache for it. */ @Override public void destroy() { this.integrationFlowContext.remove(this.id); }
@Override public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { if (bean instanceof StandardIntegrationFlow) { return processStandardIntegrationFlow((StandardIntegrationFlow) bean, beanName); } else if (bean instanceof IntegrationFlow) { return processIntegrationFlowImpl((IntegrationFlow) bean, beanName); } if (bean instanceof IntegrationComponentSpec) { processIntegrationComponentSpec(beanName, (IntegrationComponentSpec<?, ?>) bean); } return bean; }
/** * Destroy an {@link IntegrationFlow} bean (as well as all its dependant beans) * for provided {@code flowId} and clean up all the local cache for it. * @param flowId the bean name to destroy from */ @Override public void remove(String flowId) { if (this.registry.containsKey(flowId)) { IntegrationFlowRegistration flowRegistration = this.registry.remove(flowId); flowRegistration.stop(); removeDependantBeans(flowId); this.beanDefinitionRegistry.removeBeanDefinition(flowId); } else { throw new IllegalStateException("An IntegrationFlow with the id " + "[" + flowId + "] doesn't exist in the registry."); } }
/** * 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(); }
/** * Add an object which will be registered as an {@link IntegrationFlow} dependant bean in the * application context. Usually it is some support component, which needs an application context. * For example dynamically created connection factories or header mappers for AMQP, JMS, TCP etc. * @param bean an additional arbitrary bean to register into the application context. * @return the current builder instance */ @Override public StandardIntegrationFlowRegistrationBuilder addBean(Object bean) { return addBean(null, bean); }
private String generateBeanName(Object instance, String prefix) { return generateBeanName(instance, prefix, null, false); }
@Override public Message<?> receive() { return receiveAndConvert(Message.class); }
/** * 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; }
private void registerComponent(Object component, String beanName) { registerComponent(component, beanName, null); }
/** * Obtain a {@link MessagingTemplate} with its default destination set to the input channel * of the {@link IntegrationFlow}. * <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 MessageChannel}, the * {@link IllegalStateException} is thrown. * @return the {@link MessagingTemplate} instance */ @Override public MessagingTemplate getMessagingTemplate() { if (this.messagingTemplate == null) { this.messagingTemplate = new MessagingTemplate(getInputChannel()) { @Override public Message<?> receive() { return receiveAndConvert(Message.class); } @Override public <T> T receiveAndConvert(Class<T> targetClass) { throw new UnsupportedOperationException("The 'receive()/receiveAndConvert()' " + "isn't supported on the 'IntegrationFlow' input channel."); } }; this.messagingTemplate.setBeanFactory(this.beanFactory); } return this.messagingTemplate; }
private void removeDependantBeans(String parentName) { String[] dependentBeans = this.beanFactory.getDependentBeans(parentName); for (String beanName : dependentBeans) { removeDependantBeans(beanName); this.beanDefinitionRegistry.removeBeanDefinition(beanName); // TODO until https://jira.spring.io/browse/SPR-16837 String[] aliases = this.beanDefinitionRegistry.getAliases(beanName); for (String alias : aliases) { this.beanDefinitionRegistry.removeAlias(alias); } } }
.get(); IntegrationFlowContext integrationFlowContext = this.beanFactory.getBean(IntegrationFlowContext.class); integrationFlowContext.registration(standardIntegrationFlow) .autoStartup(false) .register();