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

时间:2016-04-04 06:08:59

标签: java maven scheduler job-scheduling

我的应用程序是用Spring,Hibernate(JPA),JBOSS 9.0.0.GA和JBOSS EAP 6.4。在POM.xml中,我已将包装指定为WAR。

我有两个我想要自动化的功能:

一个。 CSV阅读器 - 从CSV文件和DB中的更新表中读取

package com.fwd.pmap.memberInterfaceFile;

/* all imports */

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

湾CSV Writer - 从DB读取并输出到CSV文件

package com.fwd.pmap.memberInterfaceFile;

/* all imports */

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

如何自动执行上述两项功能,以便每天在特定时间运行?例如:

  1. CSV阅读器每天上午05:00运行
  2. CSV Writer每天上午07:00运行
  3. Project Structure

1 个答案:

答案 0 :(得分:0)

最后决定使用Spring Scheduling,因为它不涉及大量编码和XML。

这是bean类,我安排在凌晨5点运行2个作业。每天早上6点:

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

这是配置类:

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

执行上述的主要类:

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