是否可以在JBoss定时服务中使用Seam?

Dan*_*ner 4 java service jboss seam

我已经开始编写一些新的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!
    }
}
Run Code Online (Sandbox Code Playgroud)

例如,抛出以下异常:

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)
Run Code Online (Sandbox Code Playgroud)

Bob*_*bby 7

有一种方法有点脏,有很多开发人员不会使用这样的黑客,但它会解决你的问题:

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();
    }
}
Run Code Online (Sandbox Code Playgroud)

我在一个项目中使用它,在那里找不到其他有用的东西.如果有人有另一个解决方案,我期待在这里看到它:).

  • 还有一个提示...... beginCall()不会启动事务,所有EntityManager操作都会安静地失败.如果使用beginCall()和endCall(),则必须处理该事务!通过使用Transaction.instance().begin()和Transaction.instance().commit(),我可以让它真正做一些事情. (3认同)