2017-10-27 21 views
2

が、私は春のブートアプリケーションを書いています新しいスケジュールジョブを追加春ブーツ動的

私の要件です - リソース(SRC /メイン/リソース)フォルダでは、私は新しいxmlファイルを追加した場合..私はそれらのファイルを読み込み、必要がありますがそれぞれのURLからいくつかの設定を取得します。それらのURLのために私は毎日データをダウンロードする必要があります。新しいスケジューラージョブはURLといくつかの設定で始まります

新しいジョブはxmlファイルにあるcron式を使用する別のスケジュール時間で実行されます またファイル任意の時点で動的に追加されます これを組み込む方法。

+0

[スケジューリングAPI](https://docs.spring.io/spring/docs/5.0.1.RELEASE/spring-framework-reference/integration.html#scheduling)を使用して、春に利用可能 – JEY

+0

はいJEY ..動的に私は新しいXMLファイルを追加します。私は新しいスケジュールジョブを開始する必要があります。 – Nawaz

答えて

5

あなたが動的にタスクをスケジュールする場合は、特にExecutorServiceを使用することにより、スプリングずにそれを行うことができますSpringとScheduledThreadPoolExecutor

Runnable task =() -> doSomething(); 
ScheduledExecutorService executor = Executors.newScheduledThreadPool(Runtime.getRuntime().availableProcessors()); 
// Schedule a task that will be executed in 120 sec 
executor.schedule(task, 120, TimeUnit.SECONDS); 

// Schedule a task that will be first run in 120 sec and each 120sec 
// If an exception occurs then it's task executions are canceled. 
executor.scheduleAtFixedRate(task, 120, 120, TimeUnit.SECONDS); 

// Schedule a task that will be first run in 120 sec and each 120sec after the last execution 
// If an exception occurs then it's task executions are canceled. 
executor.scheduleWithFixedDelay(task, 120, 120, TimeUnit.SECONDS); 

あなたはTask and Scheduling API

に頼ることができます
public class MyBean { 

    private final TaskScheduler executor; 

    @Autowired 
    public MyBean(TaskScheduler taskExecutor) { 
     this.executor = taskExecutor; 
    } 

    public void scheduling(final Runnable task) { 
     // Schedule a task to run once at the given date (here in 1minute) 
     executor.schedule(task, Date.from(LocalDateTime.now().plusMinutes(1) 
      .atZone(ZoneId.systemDefault()).toInstant())); 

     // Schedule a task that will run as soon as possible and every 1000ms 
     executor.scheduleAtFixedRate(task, 1000); 

     // Schedule a task that will first run at the given date and every 1000ms 
     executor.scheduleAtFixedRate(task, Date.from(LocalDateTime.now().plusMinutes(1) 
      .atZone(ZoneId.systemDefault()).toInstant()), 1000); 

     // Schedule a task that will run as soon as possible and every 1000ms after the previous completion 
     executor.scheduleWithFixedDelay(task, 1000); 

     // Schedule a task that will run as soon as possible and every 1000ms after the previous completion 
     executor.scheduleWithFixedDelay(task, Date.from(LocalDateTime.now().plusMinutes(1) 
      .atZone(ZoneId.systemDefault()).toInstant()), 1000); 

     // Schedule a task with the given cron expression 
     executor.schedule(task, new CronTrigger("*/5 * * * * MON-FRI")); 
    } 
} 

そして、あなたは、設定クラスに@EnableSchedulingをusinによってスケジューリングを可能にすることを忘れないでください

Triggerを実装することで、独自のトリガーを提供することができます。

ディレクトリコンテンツの再生については、WatchServiceを使用できます。次のようなもの:

final Path myDir = Paths.get("my/directory/i/want/to/monitor"); 
final WatchService watchService = FileSystems.getDefault().newWatchService(); 
// listen to create event in the directory 
myDir.register(watchService, StandardWatchEventKinds.ENTRY_CREATE); 
// Infinite loop don't forget to run this in a Thread 
for(;;) { 
    final WatchKey key = watchService.take(); 
    for (WatchEvent<?> event : key.pollEvents()) { 
     WatchEvent<Path> watchEvent = (WatchEvent<Path>) event; 
     Path newFilePath = myDir.resolve(watchEvent.context()); 
     //do something with the newFilePath 
    } 
    // To keep receiving event 
    key.reset(); 
} 

詳しくはWatching a Directory for Changesをご覧ください。

1

あなたは春の注釈の上にそれを行うことができます。

@Scheduled(fixedRate = 360000) 
public void parseXmlFile() { 
    // logic for parsing the XML file. 
} 

方法は無効でなければならないことに注意してください。さらに、あなたのメインクラスでは、あなたはスケジューリング有効にする必要があります。

@SpringBootApplication 
@EnableScheduling 
public class Application { 

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

をここに完全なリファレンスを参照してください。 https://spring.io/guides/gs/scheduling-tasks/

+0

このコードは、サーバの先頭に特定のXMLファイルをロードしますが、後で私は新しいXMLファイルを追加していますそれらの新しいジョブを読み込まない..そしてもう一つxmlファイルがcron式を持っている。その式を使って新しいジョブを起動する方法 – Nawaz

+0

何とか新しいXMLファイルを認識する必要があります。これにより、タイムスタンプ付きの名前(例:dataFile_1486589279.xml)で動作する可能性があります。 Javaでそれを解析し、最大のタイムスタンプでファイルを取得できます。 cronジョブを動的に追加することに関して:私は、cronジョブを動的に追加する方法がわかりません。それが正しいアプローチだと私は疑うでしょう。あなたのロジックにエラーがあり、cronジョブを使ってサーバーを氾濫させたらどうなりますか?それはデバッグするのが非常に難しいでしょう。あなたのユースケースについてもっと知ることができますか? – dave0688

+0

dave0688 ..私は新しいxmlファイルを処理し、データも解析します。それは大したことではありませんので、現在、これらの新しいスケジュールジョブを起動する方法のみが懸念されます。エラー部分が正しく処理されたとします。 – Nawaz