我已经开始编写一些新的 JBoss 定时服务,旨在使用一些现有的接缝组件。但是由于上下文不存在,我似乎无法访问这些组件。是否可以在 JSF 的典型情况之外使用它们?
一个小片段来演示我想做什么...
@Service
public class MyService extends DefaultTimedService implements TimedObject, DefaultServiceInterface {
@Timeout
public void ejbTimeout(Timer timer) {
MyInterface loader = (MyInterface) Component.getInstance(MyInterface.SEAM_NAME, true);
// throws no context!
}
}
例如抛出以下异常:
java.lang.IllegalStateException: No application context active
at org.jboss.seam.Component.forName(Component.java:1945)
at org.jboss.seam.Component.getInstance(Component.java:2005)
最佳答案
有一种方法有点脏,许多开发人员永远不会使用这种 hack,但它会解决您的问题:
import org.jboss.seam.contexts.Lifecycle;
@Service
public class MyService extends DefaultTimedService implements TimedObject, DefaultServiceInterface {
@Timeout
public void ejbTimeout(Timer timer) {
Lifecycle.beginCall();
MyInterface loader = (MyInterface) Component.getInstance(MyInterface.SEAM_NAME, true);
// will not throw no context!
// also the Component.getInstance(MyInterface.SEAM_NAME, true,true); call
// is another way you could inject that component.
Lifecycle.endCall();
}
}
我曾在一个项目中使用过它,但我找不到其他任何有效的方法。如果有人有其他解决方案,我期待在这里看到它:)。
关于java - 是否可以在 JBoss 定时服务中使用 Seam?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2070262/