0

我正在开发一个基于 Spring 的 Web 应用程序。要求如下:
用户输入日期(字符串格式)。
为该日期安排一项任务(仅一次)

我的代码是:

calendar.setTime(formatter.parse(dateFromForm));
int day = calendar.get(Calendar.DAY_OF_MONTH);
int month = calendar.get(Calendar.MONTH);
.
.
String cornExp = "0 "+minutes+" "+hours+" "+day+" "+month+" "+year;

我想将cornExp价值传递给:

@Scheduled(cron=**cornExp**)  

我怎样才能做到这一点?

4

2 回答 2

0

您可以使用此链接获取分步指南

基本上,您需要一个CronTrigger对象,然后需要以trigger.setCronExpression编程方式从数据库中设置。

于 2015-06-02T07:31:37.947 回答
0

你可以使用ThreadPoolTask​​Scheduler

像这样使用spring Bean和springside项目的代码:

ThreadPoolTaskScheduler threadPoolTaskScheduler;
@PostConstruct
public void start() {
    .
    .
    calendar.setTime(formatter.parse(dateFromForm));
    int day = calendar.get(Calendar.DAY_OF_MONTH);
    int month = calendar.get(Calendar.MONTH);
    .
    .
    final String cornExp = "0 "+minutes+" "+hours+" "+day+" "+month+" "+year;

    threadPoolTaskScheduler = new ThreadPoolTaskScheduler();
    threadPoolTaskScheduler.setThreadNamePrefix("SpringCronJob");
    threadPoolTaskScheduler.initialize();

    threadPoolTaskScheduler.schedule(new Runable(){
        @Override
        public void run() {
            //run task
            //...
        }
    }
         , new CronTrigger(cornExp));
}

@PreDestroy
public void stop() {
    ScheduledExecutorService scheduledExecutorService = threadPoolTaskScheduler.getScheduledExecutor();
    scheduledExecutorService.shutdown();
}
于 2015-06-02T07:44:39.820 回答