如何获取所有EJB定时器?

发布于 2024-10-09 07:24:22 字数 165 浏览 0 评论 0原文

在 EJB 3.1 中,我可以在为特定 bean 获取的 TimerService 实例上使用 TimerService#getTimers() 来获取该 bean 的所有计时器。

然而,我真正需要的是这个的系统范围版本。即我想要 EJB 容器中所有计时器的列表。

这样的事情可能吗?

In EJB 3.1 I can get all timers for a specific bean using TimerService#getTimers() on the TimerService instance obtained for that bean.

What I actually need however is a system-wide version of this. I.e. I would like to have a list of all Timers in the EJB container.

Is something like that possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

苏大泽ㄣ 2024-10-16 07:24:22

EJB 3.1 中根本没有为此提供官方 API。

如果您仅使用注释和/或接口来标记超时方法,则可以对类路径上的所有类进行运行时遍历,并检查此注释或接口是否存在。这至少会给你理论上可以有与之关联的计时器的bean。

不幸的是,这仍然不会为您提供这些豆子的实际计时器。在 EJB 3.1 中,只能从会话上下文请求时间信息,您似乎已经知道会话上下文对于每个 bean 都是私有的。这意味着只有 Bean 本身才能看到它拥有的计时器。

作为 Nayan 建议的替代方案,您可以让您的 bean 实现一个为您提供此信息的方法。然后,您可以使用接口或自定义注释来标记此方法。

在系统范围内扫描所有计时器类时,您首先尝试发现所有可以具有与其关联的计时器的 bean,然后尝试查找它们是否具有所需的注释或接口。如果他们没有后一件事,您可以记录警告。这种方法的优点是计时器漏掉的可能性较小。

还有另一种方法,但非常脆弱,是侵入容器保存的用于存储计时器信息的任何私有结构。对于持久计时器,至少有您可以检查的持久存储,并且容器中的某个位置必须有您想要的结构。它必须在那里,因为容器本身必须意识到这一点。通常,容器有一些私有 API 来实现这一点,您可以通过反射技巧来侵入它。

容器也可能为此提供专有的 API。例如,在 Java EE 5 中,不可能在 Servlet 容器中进行编程登录,但 JBoss AS 有一个专有的 API,可以让您做到这一点。

There simply is no official API for this in EJB 3.1.

If you're only using annotations and/or interfaces to mark your timeout method, you could do a run-time walk over all classes on the class path and check if this annotation or interface is present. This will at least give you the beans which theoretically can have timers associated with them.

Unfortunately, this still won't give you the actual timers for those beans. In EJB 3.1, the time information can only be requested from the session context, which as you already seem to know is private for each bean. This means that only the bean itself can see what timers it has.

As an alternative to what Nayan is suggesting, you could let your beans implement a method that gives you this info. You can then mark this method with either an interface or a custom annotation.

In your system-wide sweep over all timer classes you first try to discover all beans that can have timers associated with them, and of those you then try to find whether they have the required annotation or interface. If they don't have this latter thing, you can log a warning. The advantage of this method is that it's less likely that timers slip through the cracks.

Yet another method, but a very brittle one, is to hack into whatever private structure the container holds to store the timer information. For persistent timers there is at least the persist store which you can check, and somewhere in the container there must be the structure you're after. It must be there as the container itself has to be aware of this. Often a container has some private API to get to this and you can hack into that via reflective tricks.

It can also be that containers offer a proprietary API for this. E.g. in Java EE 5 it's impossible to do a programmatic login in the Servlet container, but JBoss AS has a proprietary API that allows you to do exactly that.

-小熊_ 2024-10-16 07:24:22

EJB 3.2 有一个名为“getAllTimers()”的方法。

https://docs.oracle.com/ javaee/7/api/javax/ejb/TimerService.html#getAllTimers--

说明:
返回与调用者 bean 打包在同一模块中的 bean 关联的所有活动计时器。其中包括以编程方式创建的计时器和自动创建的计时器。

EJB 3.2 has a method called "getAllTimers()".

https://docs.oracle.com/javaee/7/api/javax/ejb/TimerService.html#getAllTimers--

Description:
Returns all active timers associated with the beans in the same module in which the caller bean is packaged. These include both the programmatically-created timers and the automatically-created timers.

迷途知返 2024-10-16 07:24:22

下面是获取系统所有现有定时器信息的示例代码片段。

//---

private void viewSystemTimers(){

        Collection<Timer> allTimers = sessionContext.getTimerService().getTimers();

        Iterator<Timer> iterator = allTimers.iterator();

        while (iterator.hasNext()) {

            Timer timer = (Timer) iterator.next();

            System.out.println("SYSTEM TIMER : "+timer.getInfo());
        }
    }

//---

Below is the sample code fragment for fetching information of all the existing timers of the system.

//---

private void viewSystemTimers(){

        Collection<Timer> allTimers = sessionContext.getTimerService().getTimers();

        Iterator<Timer> iterator = allTimers.iterator();

        while (iterator.hasNext()) {

            Timer timer = (Timer) iterator.next();

            System.out.println("SYSTEM TIMER : "+timer.getInfo());
        }
    }

//---
初熏 2024-10-16 07:24:22

您只能看到自己班级的计时器。使用 EJB 调度程序,您无法列出应用程序的所有计时器。

@Resource
private TimerService timerService;

for (Timer timer : timerService.getTimers()) {
    // do anything
}

使用 Quartz,您可以通过 Scheduler.getJobNames/triggerNames 获取应用程序的所有作业/触发器。

You can only see timers of your own class. With the EJB Scheduler you can't list all timers of your application.

@Resource
private TimerService timerService;

for (Timer timer : timerService.getTimers()) {
    // do anything
}

With Quartz you can via Scheduler.getJobNames/triggerNames for all jobs/triggers of you application.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文