Spring Integration several messageHandlers

1.2k views Asked by At

I am new to the Spring Integration and I couldn't find any similar post here. Right now I have one messageHandler which calls specific URI and writes to the channel.

@Bean
    @Scope("prototype")
    public MessageHandler httpGateway() {
        if (checkURLs()) {
            LOG.error("REST URLS are not configured");
            return null;
        }
        CredentialsProvider credsProvider = createCredentials();
        CloseableHttpClient httpclient = createHttpClient(credsProvider);
        HttpComponentsClientHttpRequestFactory httpRequestFactory = new HttpComponentsClientHttpRequestFactory(httpclient);
        HttpRequestExecutingMessageHandler httpHandler = createHttpRequestHandler(httpRequestFactory, requestURL);
        return httpHandler;
    }

private HttpRequestExecutingMessageHandler createHttpRequestHandler(HttpComponentsClientHttpRequestFactory httpRequestFactory, String request) {
        HttpRequestExecutingMessageHandler httpHandler = null;
        try {
            httpHandler = new HttpRequestExecutingMessageHandler(new URI(request));
        } catch (URISyntaxException e) {
            LOG.error(e.toString(), e);
        }
        httpHandler.setExpectedResponseType(String.class);
        httpHandler.setRequestFactory(httpRequestFactory);
        httpHandler.setHttpMethod(HttpMethod.GET);
        return httpHandler;
    }

and IntegrationFlows

@Bean
public IntegrationFlow esbFlow(MessageHandler httpGateway) {
    if (checkURLs()) {
        LOG.error("REST URLS are not configured create flow without fetching");
        return null;
    }
    return IntegrationFlows
            .from(integerMessageSource(), c -> c.poller(Pollers.cron(pollerCron)))
            .channel(TRIGGER_CHANNEL)
            .handle(httpGateway)
            .filter(p -> p instanceof String, f -> f.discardChannel(ESB_JSON_ERROR_CHANNEL))
            .channel(ESB_JSON_PARSING_CHANNEL)
            .get();
}

Now, i have to extended this function so that one additional URL will be called. As far as I understand, MessageHandler can always call just one URL and handle function in IntegrationFlow only one MessageHandler.

1

There are 1 answers

0
Gary Russell On

Use a publish subscribe channel and subscribe two subflows to it; see the DSL reference.

BTW, @Scope("prototype") is not applicable to spring integration beans.