如何获取所有EJB计时器? [英] How to get all EJB timers?

查看:119
本文介绍了如何获取所有EJB计时器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在EJB 3.1中,我可以使用TimerService实例获取所有的定时器,使用TimerService#getTimers()获取该Bean的TimerService实例。

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

是一个系统范围的版本。即我想在EJB容器中列出所有计时器。

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.

有可能吗?

推荐答案

EJB 3.1中没有官方的API。

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

如果您只使用注释和/或接口来标记您的超时方法,则可以在类路径上的所有类上执行运行时步骤,并检查是否此注释或界面存在。这至少会给你理论上可以与他们相关联的计时器的bean。

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.

不幸的是,这仍然不会给你实际的这些bean的计时器。在EJB 3.1中,时间信息只能从会话上下文中被请求,如您已经看到的那样,每个bean都是私有的。这意味着只有bean本身可以看到它有什么计时器。

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.

作为Nayan建议的替代方法,你可以让你的bean实现一个给你这个方法信息。然后,您可以使用界面或自定义注释来标记此方法。

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.

另一种方法,但非常脆弱的是将黑客入侵任何私有结构容器保存以存储定时器信息。对于持久的计时器,至少有可以检查的持久存储器,并且容器中的某个位置必须是您之后的结构。它必须在那里,因为容器本身必须意识到这一点。通常,一个容器有一些专用的API可以通过反馈技巧进行处理。

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.

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

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.

这篇关于如何获取所有EJB计时器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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