Search code examples
javaspringspring-bootspring-batchspring-integration

Do I always need a web server with spring integration?


I'm new to spring-integration and what I want to acheive is lanching a spring-batch job from an external web application.

Batchs are run in two diffrent maners. Cron (outside of the scope of this question) and started manually by a user action like shown in the picture below

enter image description here

Now each batch is configured like the following :

@SpringBootApplication
@EnableBatchProcessing
public class Batch1Application {

    public static void main(String[] args) {
        new SpringApplicationBuilder(Batch1Application.class)
                .web(WebApplicationType.NONE)
                .registerShutdownHook(true)
                .run(args);
    }
}

Currently when the web app receives an action it calls the same sh script used in by cron configuration :

//...
ProcessBuilder processBuilder = new ProcessBuilder(List.of(/*path to batch-1-script.sh*/));
Process process = processBuilder.start();
//...

The script do something like : java -jar batch1.jar

It works fine and contexts of each application is not shared.

Now I want to move to spring-integration. I've found several articles/videos that talk about spring-integration and spring-batch-intergartion, But all use the same web-app to launch jobs using a simple DirectChannel (as context contains all the needed beans).


My question is how to keep each batch listening for messages to come and then start executing ?

Do I need to convert them all to web app and add spring-integration on them ?


Solution

  • Spring Integration is just a library and it fully is not tied to any container specifics. If you need to expose an interaction with your application via HTTP, only then you need to have this application on Web server.

    It is not clear from your description what does that batch-1-script.sh do to start the batch application, but probably you can live this feature and just change its service implementation to emit a message to a channel instead of direct job execution call as it is right now.

    On the other hand you can take any messaging broker in between and publish a message to the topic to let those apps to listener for such a message on their side.

    See possible endpoints in Spring Integration for this solution: https://docs.spring.io/spring-integration/docs/current/reference/html/endpoint-summary.html#spring-integration-endpoints.

    Also, since you say that you can some script at a the moment, it sounds like everything is done on the same host, so probably you can take an advantage from ZeroMQ as a protocol between Web APP and those workers: https://docs.spring.io/spring-integration/docs/current/reference/html/zeromq.html#zeromq