2017-05-03 11 views
1

私は定期的に複数のジョブを実行するスケジューリングプロジェクトに取り組んでいます。 私は以下の例のようにcronスケジューリングを使用しています。ジョブは問題なく正常に実行されています。 しかし、要件のために私は計算し、DBのスケジュールされたジョブの次の実行時間を維持したいと思います。 以下の設定のジョブの次回または前回の火災時間を取得するソリューションはありますか?次の実行時間を取得する方法Spring Scheduling?

設定例:

import java.util.Date; 
import org.springframework.beans.factory.annotation.Qualifier; 
import org.springframework.scheduling.annotation.EnableScheduling; 
import org.springframework.scheduling.annotation.Scheduled; 
import org.springframework.scheduling.support.CronTrigger; 
import org.springframework.stereotype.Component; 

@Component 
@EnableScheduling 
public class DemoServiceBasicUsageCron 
{ 
    @Scheduled(cron="1,3,30,32 * * * * ?") 
    public void demoServiceMethod() 
    { 
     System.out.println("Curent date time is - "+ new Date()); 
    } 

} 

答えて

0

私はあなたがクォーツを使用している願っています。このようなことを試すことができます。

CronExpression exp = new CronExpression("1,3,30,32 * * * * ?"); 
exp.getNextValidTimeAfter(new Date()); 
+0

こんにちはPraneeth、残念ながら現在のデザインでは石英を使用していません。 –

0

CronSequenceGeneratorを使用できます。

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

import javax.annotation.PostConstruct; 
import java.util.Date; 

@Component 
public class MyJob { 

    public static final String CRON_EXPRESSION = "0 0 5 * * *"; 

    @PostConstruct 
    public void init() { 
     CronSequenceGenerator cronTrigger = new CronSequenceGenerator(CRON_EXPRESSION); 
     Date next = cronTrigger.next(new Date()); 

     System.out.println("Next Execution Time: " + next); 
    } 

    @Scheduled(cron = CRON_EXPRESSION) 
    public void run() { 
     // Your custom code here 
    } 
} 
関連する問題