可以在运行时为@Schedule注释更改ejb参数吗? [英] Possible to change ejb parameter at runtime for @Schedule annotation?

查看:186
本文介绍了可以在运行时为@Schedule注释更改ejb参数吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于有ejb经验的人来说,这可能是一个愚蠢的问题...

我想为使用Java的其中一个EJB bean动态读取和更改分钟参数EE调度程序通过 @Schedule 注释。任何人都知道如何在运行时执行此操作,而不是像下面那样在类中进行硬编码?如果我以编程方式执行此操作,我仍然可以使用 @Schedule 注释吗?

  @Schedule(dayOfWeek =0-5,hour =0/2,minute =0/20,timezone =America / Los_Angeles)
private void checkInventory(){


解决方案

@Schedule 用于在部署期间由容器创建的自动定时器。

另一方面,您可以使用 docs.oracle.com/javaee/6/api/javax/ejb/TimerService.html> TimerService ,它允许您在运行时定义<$应该调用c $ c> @Timeout 方法。



这可能是您的有趣材料: Java EE 6教程 - 使用计时器服务



<编辑:只是为了让这个答案更完整。如果这是一个的问题,它有可能而不是答案 - 是的,它是。



一种用 @Schedule 创建的自动定时器更改参数的方法。尽管如此,它还是非常非凡的 - 它取消了自动计时器并创建了一个类似的程序化计时器:

  //自动定时器 - 每5秒运行
//这是一个自动(@Schedule)和编程(@Timeout)定时器超时方法
//同时(这是不寻常的)
@Schedule (小时=*,分钟=*,秒=* / 5)
@Timeout
public void myScheduler(Timer timer){

// This可能会检查基于ie timer.getInfo()。
firstSchedule = ...

if(!firstSchedule){
//计划程序的普通代码
} else {

/ /获取实际的时间表示。
//这是第一次调用,所以它等于@Schedule中的那个(...)
ScheduleExpression expr = timer.getSchedule();

//你现在的新表达式
expr.second(* / 7);

//定时器是使用@Resource TimerService注入的TimerService对象。

//根据修改的表达式创建新的计时器。
timers.createCalendarTimer(expr);

//取消使用@Schedule注释创建的计时器。
timer.cancel();
}
}

再一次 - 我个人绝对不会使用这样的代码并且绝不希望在实际项目中看到类似的内容:-)计时器是:


  • 自动,并通过对值进行硬编码或通过在ejb-jar.xml中定义它们来 @Schedule 创建

  • programmatic ,并由运行时可能具有不同值的应用程序代码创建。


Probably a silly question for someone with ejb experience...

I want to read and change the minute parameter dynamically for one of my EJB beans that uses the Java EE scheduler via the @Schedule annotation. Anyone know how to do this at runtime as opposed to hardcoding it in the class like below? If i were to do it programatically could i still use the @Schedule annotation?

 @Schedule(dayOfWeek = "0-5", hour = "0/2", minute = "0/20", timezone = "America/Los_Angeles")
 private void checkInventory() {
 }

解决方案

@Schedule is for automatic timers created by the container during deployment.

On the other hand, you can use TimerService which allows you to define at runtime when the @Timeout method should be called.

This might be interesting material for your: The Java EE 6 Tutorial - Using the Timer Service.

EDIT: Just to make this answer more complete. If it's a matter of is it possible than the answer would be - yes, it is.

There is a way to "change the parameters" of automatic timer created with @Schedule. Nevertheless it's quite extraordinary - it cancels the automatic timer and creates a similar programmatic one:

// Automatic timer - run every 5 seconds
// It's a automatic (@Schedule) and programmatic (@Timeout) timer timeout method
// at the same time (which is UNUSUAL)
@Schedule(hour = "*", minute = "*", second = "*/5")
@Timeout
public void myScheduler(Timer timer) {

    // This might be checked basing on i.e. timer.getInfo().
    firstSchedule = ...

    if (!firstSchedule) {
        // ordinary code for the scheduler
    } else {

        // Get actual schedule expression.
        // It's the first invocation, so it's equal to the one in @Schedule(...)
        ScheduleExpression expr = timer.getSchedule();

        // Your new expression from now on
        expr.second("*/7");

        // timers is TimerService object injected using @Resource TimerService.

        // Create new timer based on modified expression.
        timers.createCalendarTimer(expr);

        // Cancel the timer created with @Schedule annotation.
        timer.cancel();
    }
}

Once again - personally, I would never use such code and would never want to see anything like this in a real project :-) Either the timer is:

  • automatic and is created by @Schedule by hard-coding the values or by defining them in ejb-jar.xml,
  • programmatic and is created by the application code which can have different values in the runtime.

这篇关于可以在运行时为@Schedule注释更改ejb参数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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