使用 Windows 任务计划程序自动执行 WAR 文件

Using Windows Task Scheduler to automate execution of WAR file

我的应用程序是用 Spring、Hibernate (JPA)、JBOSS 9.0.0.GA & JBOSS EAP 6.4 编写的。在POM.xml中我指定了包装为WAR。

我有 2 个功能想要自动化:

一个。 CSV reader - 从 CSV 文件读取并在 DB

中更新 table
package com.fwd.pmap.memberInterfaceFile;

/* all imports */

public class CsvReader
{
    public void importInterfaceFile() throws Exception
    {
        // do processing here
    }
}

b。 CSV 编写器 - 从数据库读取并输出到 CSV 文件

package com.fwd.pmap.memberInterfaceFile;

/* all imports */

public class CsvWriter
{
    public void generateInterfaceFile() throws Exception
    {
        // do processing here
    }
}

如何在每天的特定时间将上述两个功能自动化到 运行?例如:

  1. CSV Reader 到 运行 每天@ 05:00 AM
  2. CSV 作者每天 运行 @ 07:00 AM

Project Structure

最终决定使用 Spring 调度,因为它不涉及大量编码以及 XML。

这是 bean class,我每天早上 5 点和 6 点将 2 个作业安排到 运行:

package com.fwd.pmap.scheduler;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.fwd.pmap.memberInterfaceFile.CsvReader;
import com.fwd.pmap.memberInterfaceFile.CsvWriter;;

@Component
public class MyBean {

    @Scheduled(cron="0 0 5 * * *")
    public void importInterfaceFile()
    {
        CsvReader reader = new CsvReader();
        try {
            reader.importInterfaceFile();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Scheduled(cron="0 0 6 * * *")
    public void generateInterfaceFile()
    {
        CsvWriter writer = new CsvWriter();
        try {
            writer.generateInterfaceFile();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

配置如下 class:

package com.fwd.pmap.scheduler;

import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;

import com.fwd.pmap.scheduler.MyBean;

@Configuration
@EnableScheduling
public class SchedulerConfig implements SchedulingConfigurer {

    @Bean
    public MyBean bean() {
        return new MyBean();
    }

    @Override
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
        taskRegistrar.setScheduler(taskExecutor());
    }

    @Bean(destroyMethod="shutdown")
    public Executor taskExecutor() {
        return Executors.newScheduledThreadPool(4);
    }
}

和主要class执行上面的:

package main;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;

import com.fwd.pmap.scheduler.SchedulerConfig;

public class Main {
    static Logger LOGGER = LoggerFactory.getLogger(Main.class);

    @SuppressWarnings({ "unused", "resource" })
    public static void main(String[] args) {
        AbstractApplicationContext context = new AnnotationConfigApplicationContext(SchedulerConfig.class);
    }
}