使用Schedule Expression参数化EJB调度程序 [英] Parameterize EJB scheduler with Schedule Expression

查看:180
本文介绍了使用Schedule Expression参数化EJB调度程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用EJB 3.1和jboss-eap-6.4,我想为ejb调度程序的小时,分​​钟和秒钟设置一些动态参数,如下所示:



非参数代码 - 每5分钟运行30秒;



  @Singleton 
@Startup
public class TriggerJob {
@EJB
//一些db注入

@PostConstruct
public void onStartup(){
尝试{
prepare();
} catch(CertificateVerificationException e){
e.printStackTrace();
}
}

@Schedule(second =30,minute =* / 5,hour =*,persistent = false)
public void prepare(){
//我的预定任务
}
}

上述代码正常执行。



动态参数代码 - 每5分钟30秒钟运行一次



  @Singleton 
@Startup
public class TriggerJob {

@EJB
// some injections

private boolean runningFlag = false;

@Resource
private TimerService timerService;

public void setTimerService(TimerService timerService){
this.timerService = timerService;
}

@Timeout
public void timerTimeout(){
try {
prepare();
} catch(CertificateVerificationException e){
e.printStackTrace();
$


private void postCunstruct() at,false);
}

private ScheduleExpression createSchedule(){
ScheduleExpression expression = new ScheduleExpression();
expression.hour(*)
.minute(* / 5)
.second(30);
返回表达式;
}

public void prepare(){
//我的预定任务
}
}
/ pre>

上述代码无法正常执行,通常它会在一秒钟内执行多次。



另外,我已经阅读了其他一些不帮助我的问题:



EJB 3.x中@Schedule方法的动态参数



使用定时服务 - Java EE 6教程



任何帮助将不胜感激。

解决方案

相反,使用编程调度, exmaple:

  @Singleton 
@Startup
public class TriggerJob {

@EJB
//一些注入

@Resource
private TimerService ti merService;

@PostConstruct
public void init(){
createTimer();
//以下代码解决我的启动问题
try {
prepare();
} catch(CertificateVerificationException e){
e.printStackTrace();
}
}

@Timeout
public void timerTimeout(){
try {
prepare();
} catch(CertificateVerificationException e){
e.printStackTrace();
}
}

private void createTimer(){
ScheduleExpression scheduleExpression = new ScheduleExpression();
scheduleExpression.second(30)。minute(* / 5)。hour(*);
TimerConfig timerConfig = new TimerConfig();
timerConfig.setPersistent(false);
timerService.createCalendarTimer(scheduleExpression,timerConfig);
}

public void prepare(){
//我的预定任务
}
}
/ pre>

I am using EJB 3.1 and jboss-eap-6.4 and I want to set some dynamic parameters for hour, minute and second of ejb scheduler as follows:

Non-parametric code - which run in 30th second of every 5 minutes :

@Singleton
@Startup
public class TriggerJob {
    @EJB
   //some db injections           

    @PostConstruct
    public void onStartup() {
        try {
            preparation();
        } catch (CertificateVerificationException e) {
            e.printStackTrace();
        }
    }

    @Schedule(second = "30", minute = "*/5", hour = "*", persistent = false)
    public void preparation() {
    //my scheduled tasks
    }
}

The above code executes properly.

Dynamic Parametric code - which should run in 30th second of every 5 minutes:

@Singleton
@Startup
public class TriggerJob {

    @EJB
    //some injections

    private boolean runningFlag = false;

    @Resource
    private TimerService timerService;

    public void setTimerService(TimerService timerService) {
        this.timerService = timerService;
    }

    @Timeout
    public void timerTimeout() {
        try {
            preparation();
        } catch (CertificateVerificationException e) {
            e.printStackTrace();
        }
    }

    @PostConstruct
    private void postCunstruct() {
        timerService.createCalendarTimer(createSchedule(),new TimerConfig("EJB timer service timeout at ",false));
    }

    private ScheduleExpression createSchedule() {
        ScheduleExpression expression = new ScheduleExpression();
        expression.hour("*")
                .minute("*/5")
                .second("30");
        return expression;
    }

    public void preparation(){
    // my scheduled tasks
    }
}

The above code does not execute correctly, usually it executes multiple times at a second.

Also, I have read some other questions which did not help me:

Dynamic parameters for @Schedule method in an EJB 3.x

Using the Timer Service - The Java EE 6 Tutorial

Any help would be appreciated.

解决方案

Instead, use programmatic scheduling, here is an exmaple :

@Singleton
@Startup
public class TriggerJob{

    @EJB
    //some injections

    @Resource
    private TimerService timerService;

    @PostConstruct
    public void init() {
        createTimer();
        //the following code resolve my startup problem
        try {
        preparation();
        } catch (CertificateVerificationException e) {
            e.printStackTrace();
        }
    }

    @Timeout
    public void timerTimeout() {
        try {
        preparation();
        } catch (CertificateVerificationException e) {
        e.printStackTrace();
        }
    }

    private void createTimer() {
        ScheduleExpression scheduleExpression = new ScheduleExpression();
        scheduleExpression.second("30").minute("*/5").hour("*");
        TimerConfig timerConfig = new TimerConfig();
        timerConfig.setPersistent(false);
        timerService.createCalendarTimer(scheduleExpression, timerConfig);
        }

    public void preparation(){
        // my scheduled tasks
    }
}

这篇关于使用Schedule Expression参数化EJB调度程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆