2016-05-19 3 views
5

を可能クロン持つタスク、このクラスで春4.2スケジュール私はスプリントブーツ1.3を使用して動的更新

@Service 
public class PaymentServiceImpl implements PaymentService { 
    .... 
    @Transactional 
    @Override 
    public void processPayment() { 
     List<Payment> payments = paymentRepository.findDuePayment(); 
     processCreditCardPayment(payments); 
    } 
} 

は、私はすべてのxの瞬間processPaymentを呼び出すしたいと思います。

このxモーメントはデータベースに設定されます。 ユーザーはそれを変更できます。

私はアノテーションを使用できないと思います。

私は多分それは良い方法ではありませんこれにこの

@EntityScan(basePackageClasses = {MyApp.class,  Jsr310JpaConverters.class}) 
@SpringBootApplication 
@EnableCaching 
@EnableScheduling 
public class MyApp { 

    @Autowired 
    private DefaultConfigService defaultConfigService; 

    public static void main(String[] args) { 
     SpringApplication.run(MyApp.class, args); 
    } 

    @Bean 
    public TaskScheduler poolScheduler() { 
     SimpleAsyncTaskExecutor taskScheduler = new SimpleAsyncTaskExecutor(); 

     DefaultConfigDto defaultConfigDto = defaultConfigService.getByFieldName("payment-cron-task"); 
     String cronTabExpression = "0 0 4 * * ?"; 
     if (defaultConfigDto != null && !defaultConfigDto.getFieldValue().isEmpty()) { 
      cronTabExpression = "0 0 4 * * ?"; 
     } 

     appContext.getBean("scheduler"); 

     taskScheduler.schedule(task, new CronTrigger(cronTabExpression)); 
     return scheduler; 
    } 

を開始しました。

提案がありますか?更新するよう

私が質問を見てみると、メイン

public static void main(String[] args) { 
     context = SpringApplication.run(MyApp.class, args); 
} 

答えて

7

@Autowired 
ConfigurableApplicationContext context; 

ようにし、後にプロパティを作成する必要がある場合は、私のコンテキストを取得する場合には分からないらしいですスケジューラ、再起動せずに。

あなたが共有しているコードは、設定がDBから選択されていることを保証しますが、アプリケーションを再起動しなければ更新されません。

次のコードでは、春のコンテキストでデフォルトのスケジューラが利用可能に使用して、動的にDBで利用可能なcronの設定に基づいて次の実行時間を計算します:ここでは

はサンプルコードです:

import java.util.Date; 

import org.slf4j.Logger; 
import org.slf4j.LoggerFactory; 
import org.springframework.boot.SpringApplication; 
import org.springframework.boot.autoconfigure.SpringBootApplication; 
import org.springframework.scheduling.Trigger; 
import org.springframework.scheduling.TriggerContext; 
import org.springframework.scheduling.annotation.EnableScheduling; 
import org.springframework.scheduling.annotation.SchedulingConfigurer; 
import org.springframework.scheduling.config.ScheduledTaskRegistrar; 
import org.springframework.scheduling.support.CronTrigger; 

@SpringBootApplication 
@EnableScheduling 
public class Perses implements SchedulingConfigurer { 
    private static final Logger log = LoggerFactory.getLogger(Perses.class); 

    @Autowired 
    private DefaultConfigService defaultConfigService; 

    @Autowired 
    private PaymentService paymentService; 

    public static void main(String[] args) { 
     SpringApplication.run(Perses.class, args); 
    } 

    private String cronConfig() { 
     String cronTabExpression = "*/5 * * * * *"; 
     if (defaultConfigDto != null && !defaultConfigDto.getFieldValue().isEmpty()) { 
      cronTabExpression = "0 0 4 * * ?"; 
     } 
     return cronTabExpression; 
    } 

    @Override 
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) { 
     taskRegistrar.addTriggerTask(new Runnable() { 
      @Override 
      public void run() { 
       paymentService.processPayment(); 
      } 
     }, new Trigger() { 
      @Override 
      public Date nextExecutionTime(TriggerContext triggerContext) { 
       String cron = cronConfig(); 
       log.info(cron); 
       CronTrigger trigger = new CronTrigger(cron); 
       Date nextExec = trigger.nextExecutionTime(triggerContext); 
       return nextExec; 
      } 
     }); 
    } 
} 
+0

アプリケーションを再起動しなくても変更を反映できるようにするために検索します。 –

+0

Javaコードを動的cronに更新しました。 DBエントリに十分なガードを入れて、だれもそれを壊さないようにしてください。 – Shibashis

+1

このような仕組みが好きです。ソース(外部プロパティファイル、db変更など)が変更されたときにcronを更新する方法はありますか? – Divs

関連する問題