我如何 运行 file.JAR 使用控制器 RESTFUL 传递动态参数(Spring 框架)

How can i run a file.JAR passing a dynamic arguments using controller RESTFUL ( Spring Framework )

我有两个项目,第一个是使用 Spring 框架(第 4 版)开发的 Maven 项目,它包含用于与前端应用程序通信的 Web 服务 Restful (使用 Angular 4 开发)另一个是 spring 引导项目,涉及调度任务的批处理特性,它是使用 spring 批处理方法完成的。

想法是在第一个项目中使用 Web 服务,该服务将扮演 运行ning 从 spring-batch 项目生成的文件 Jar 的角色,并有可能传递一个动态参数。

动态参数 我的项目是一个 CronExpression,这个输入(参数)应该是动态的。

我已经使用了 RunTime.exec( "java", "-jar", "MyFile.jar", "Arg1" ) 但它不起作用。所以在搜索之后,我发现我使用 proccessBuilder 的另一种方式,它工作正常,但只适用于静态参数。

我的目标是 运行 我的 jar 一次,并且一直在 运行ning 上,同时我应该将动态参数传递给 运行 我的批次特性。

我想建议我最好的方法。

谢谢!

您可以从其余控制器获取参数,并使用它们启动使用 ProcessBuilder API 的作业。这是一个例子:

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();
    }
}

希望对您有所帮助。