1

I have two projects, the first one is a maven project developed with Spring framework ( Version 4 ) and it contains the web services Restful which is used to communicate with the front end application ( Developping with Angular 4) and the other one is a spring boot project concerning a batch traitement for scheduling tasks and it has been done by using the spring batch approach.

The idea is to use a web service in the first project who will have the role of running the file Jar generate from the spring-batch project with the possibility to passing a dynamic arguments.

DYNAMIC ARGUMENTS for my project is a CronExpression, this input (argument) should be dynamic.

I already used the RunTime.exec( "java", "-jar", "MyFile.jar", "Arg1" ) but it doesn't work. So after a search I found I nother way by using proccessBuilder and it work fine but just for a static argument.

My goal is to run my jar for once and to be all the time on running and at the same time i should pass a dynamic arguments to run my batch traitement.

I would like to suggest me the best way to do it.

Thank you !

  • It is possible to pass dynamic arguments to the `ProcessBuilder` using `ProcessBuilder.command(String... command)` method. But the question is not clear, you want to run the job manually and in a scheduled manner at the same time? You either run it manually each time or use a scheduler for that. Or am I missing a detail? – Mahmoud Ben Hassine Nov 05 '18 at 09:53
  • thank you for your response ! I would like to run my JAR each time I call the web service from my maven project using different input value on each call and to keep my program batch ( spring boot ) always running ! – Othmane Bouchara Nov 06 '18 at 10:03

1 Answers1

0

You can get the parameters from the rest controller and use them to launch the job using the ProcessBuilder API. Here is an example:

import org.springframework.batch.core.launch.JobOperator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class JobLaunchingController {

    @RequestMapping(value = "/", method = RequestMethod.POST)
    @ResponseStatus(HttpStatus.ACCEPTED)
    public void launch(@RequestParam("name") String name) throws Exception {
        ProcessBuilder processBuilder = new ProcessBuilder();
        processBuilder.command("java", "-jar", "myjob.jar", "name=" + name);
        processBuilder.start();
    }
}

Hope this helps.

Mahmoud Ben Hassine
  • 28,519
  • 3
  • 32
  • 50
  • Your solution it works very well, but the thing that I didn't pay attention to was my spring boot project, It was a WEB project and that's why the execution of JAR file wasn't able to run in a dynamic mode every time a call the web service with the new input. The solution it was to add in my main class of the batch program the annotation@EnableAutoConfiguration(excule{EmbeddedServeltContainerAutoConfiguration.class,WebMvcAutoConfiguration.class}) also in the app.properties add spring.main.web-environment=false. Thank you Mahmoud for you response and the time you have devoted to me ! – Othmane Bouchara Nov 09 '18 at 15:16