2016-04-04 157 views
1

我的應用程序是用Spring,Hibernate(JPA),JBOSS 9.0.0.GA & JBOSS EAP 6.4編寫的。在POM.xml中,我已經將包裝指定爲WAR。使用Windows任務計劃程序自動執行WAR文件

我有2個功能我想自動化:

a。 CSV閱讀器 - 從CSV文件中讀取並更新DB中的表格

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閱讀每天運行@上午5點00
  2. CSV作家每天@ 07:00

Project Structure

+0

他們應該在你的應用程序中運行嗎?如果是這樣看看[Quartz Scheduler](https://www.quartz-scheduler.org/).... – khmarbaise

+0

@khmarbaise我不喜歡在應用程序中編寫調度,這就是爲什麼我正在探索是否這通過Windows任務計劃程序是可能的。 應用程序最終會部署在服務器上(也運行EAP),我想從服務器本身設置計劃任務。 – Maruli

+0

我認爲可以通過Windows任務計劃程序來安排腳本或應用程序的執行,但是在應用程序中安排函數是不可能的。 – Hohenheim

回答

0

最後決定使用Spring調度運行作爲它不涉及大量的編碼和XML。

這是bean類,我安排2個作業在凌晨5點&上午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); 
    } 
} 
相關問題