0

Given I have 2 IntegrationFlows which reference the same SimpleWebServiceOutboundGateway bean:

@Bean
IntegrationFlow getDataA(
        Jaxb2Marshaller xmlMarshaller,
        SimpleWebServiceOutboundGateway webServiceOutboundGateway) {
    return IntegrationFlows.from("getDataA")
            .transform(marshaller(xmlMarshaller))
            .transform(new ResultToStringTransformer())
            .handle(webServiceOutboundGateway)
            .transform(unmarshaller(xmlMarshaller))
            .get();
}

@Bean
IntegrationFlow getDataB(
        Jaxb2Marshaller xmlMarshaller,
        SimpleWebServiceOutboundGateway webServiceOutboundGateway) {
    return IntegrationFlows.from("getDataB")
            .transform(marshaller(xmlMarshaller))
            .transform(new ResultToStringTransformer())
            .handle(webServiceOutboundGateway)
            .transform(unmarshaller(xmlMarshaller))
            .get();
}

@Bean
SimpleWebServiceOutboundGateway webServiceOutboundGateway(HttpComponentsMessageSender httpComponentsMessageSender) {
    SimpleWebServiceOutboundGateway outboundGateway = new SimpleWebServiceOutboundGateway("url");
    outboundGateway.setMessageSender(httpComponentsMessageSender);
    return outboundGateway;
}

When I execute the code I am getting:

Caused by: java.lang.IllegalArgumentException: An AbstractMessageProducingHandler may only be referenced once (webServiceOutboundGateway) - use @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE) on @Bean definition.

When I add @Scope(SCOPE_PROTOTYPE) to webServiceOutboundGateway, I'm getting the same exception

How can I reuse webServiceOutboundGateway in both flows?

Gary Russell
  • 166,535
  • 14
  • 146
  • 179
Patrik Mihalčin
  • 3,341
  • 7
  • 33
  • 68
  • Prototype scope scope should be all you need; however, this looks odd `from("getDataA")`. `.from(String)` means from channel name, but you have from this `IntegrationFlow`. – Gary Russell Apr 30 '20 at 14:32

1 Answers1

0

I just copied your code into a new app, fixed the channel names and putting the gatway in prototype scope worked for me...

@Bean
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
SimpleWebServiceOutboundGateway webServiceOutboundGateway() {
    SimpleWebServiceOutboundGateway outboundGateway = new SimpleWebServiceOutboundGateway("url");
    return outboundGateway;
}
Gary Russell
  • 166,535
  • 14
  • 146
  • 179
  • Hmm, it fails to start for me. I use `org.springframework.integration:spring-integration-ws -> 4.3.14.RELEASE` and `org.springframework.integration:spring-integration-core:4.3.14.RELEASE`. Could it be the reason? – Patrik Mihalčin May 05 '20 at 22:34
  • 1
    Yes, that's the problem; I am not sure why, but `@Scope(scopeName = ConfigurableBeanFactory.SCOPE_PROTOTYPE, proxyMode = ScopedProxyMode.TARGET_CLASS)` solves it. That said, why such an old version? 4.3.x won't be supported for much longer; if you are using Boot 1.5; it went end of life long ago. – Gary Russell May 05 '20 at 23:06