Java 如何使用spring引导批处理运行.bat文件

Java 如何使用spring引导批处理运行.bat文件,java,spring,spring-boot,spring-batch,batch-processing,Java,Spring,Spring Boot,Spring Batch,Batch Processing,我有一个通过windows任务调度器运行的批处理文件,它将每隔10小时重复执行一次jar文件 现在,我尝试使用spring引导批处理进程作为任务调度器运行同一批处理文件。但我没有找到解决办法 我如何解决这个问题 你试过这个吗 SpringBootScheduler可以调度您的任务,并接受类似CRON的表达式来调度重复任务 您可以使用Java API在schedule方法中触发批处理(.bat)文件,尝试如下操作: Runtime.getRuntime().exec("file.bat"); 您

我有一个通过windows任务调度器运行的批处理文件,它将每隔10小时重复执行一次jar文件

现在,我尝试使用spring引导批处理进程作为任务调度器运行同一批处理文件。但我没有找到解决办法

我如何解决这个问题

你试过这个吗

SpringBootScheduler可以调度您的任务,并接受类似CRON的表达式来调度重复任务


您可以使用Java API在schedule方法中触发批处理(.bat)文件,尝试如下操作:

Runtime.getRuntime().exec("file.bat");
您可以实现如下简单的

@Component
public class RunCommandScheduledTask {

    private final ProcessBuilder builder;
    private final Logger logger; //  Slf4j in this example
    // inject location/path of the bat file 
    @Inject
    public RunCommandScheduledTask(@Value("${bat.file.path.property}") String pathToBatFile) {
        this.builder = new ProcessBuilder(pathToBatFile);
        this.logger = LoggerFactory.getLogger("RunCommandScheduledTask");
    }
    // cron to execute each 10h 
    @Scheduled(cron = "0 */10 * * *")
    public void runExternalCommand() {
        try {
            final Process process = builder.start();
            if (logger.isDebugEnabled()) {
                // pipe command output and proxy it into log
                try (BufferedReader out = new BufferedReader(
                        new InputStreamReader(process.getInputStream(), StandardCharsets.ISO_8859_1))) {
                    String str = null;
                    for (;;) {
                        str = out.readLine();
                        if (null == str)
                            break;
                        logger.debug(str);
                    }
                }
            }
            process.waitFor();
        } catch (IOException exc) {
            logger.error("Can not execute external command", exc);
        } catch (InterruptedException exc) {
            Thread.currentThread().interrupt();
        }
    }

}

.....
@SpringBootApplication
@EnableScheduling
public class Application {

    public static void main(String[] args) throws Exception {
        SpringApplication.run(Application.class);
    }
}

您想进行批处理还是计划?听起来你想做调度,所以Spring批处理在这方面帮不了你。我有一个Java可运行jar文件,我正在尝试使用Spring引导过程执行jar。前面,我曾经在.bat和windows调度器的帮助下执行jar。