java JBoss 启动时 EJB 中的调用方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2995511/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Call method in EJB on JBoss startup
提问by Elijah
I'm looking for an entry point in an EJB deployed on JBoss.
我正在寻找部署在 JBoss 上的 EJB 的入口点。
Servlets have the load-on-startuptag to use in its web.xml.
Servlet 具有 load-on-startup要在其web.xml.
I'm searching for similar init()functionality for an EJB.
我正在init()为 EJB寻找类似的功能。
回答by ewernli
That didn't exist for EJB until 3.1. With EJB 3.1 you can use a singleton bean to simulate that:
直到 3.1,EJB 才存在这种情况。使用 EJB 3.1,您可以使用单例 bean 来模拟:
From Application Startup / Shutdown Callbacks:
@Startup
@Singleton
public class FooBean {
@PostConstruct
void atStartup() { ... }
@PreDestroy
void atShutdown() { ... }
}
Otherwise, you will need to rely on the good old trick to use a ServletContextInitializer.
否则,您将需要依靠古老的技巧来使用ServletContextInitializer.
There are some application-specific extension, e.g. lifecycle listenerfor Glassfish. Maybe there's such a thing for JBoss.
有一些特定于应用程序的扩展,例如Glassfish 的生命周期侦听器。也许 JBoss 有这样的事情。
But if I were you I would try to rely on standard features as much as possible. The problem with non-standard extension is that you never know exactly what can be done or not, e.g. can you start transaction or not, etc.
但如果我是你,我会尽量依赖标准功能。非标准扩展的问题在于您永远不知道可以做什么或不可以做什么,例如您是否可以开始交易等。
回答by Thijs
This article describes seven different ways of invoking functionality at server startup. Not all will work with JBoss though.
本文介绍了在服务器启动时调用功能的七种不同方式。不过,并非所有人都可以使用 JBoss。
Seven ways to get things started. Java EE Startup Classes with GlassFish and WebLogic
回答by lmika
If you're targeting JBoss AS 5.1, and you don't mind using the JBoss EJB 3.0 Extensions, you can build a service bean to bootstrap your EJB. If your service implements an interface annotated with the @Managementannotation and declares a method with the signature public void start() throws Exception, JBoss will call this method when it starts the service. You can then call a dedicated init()method on the EJB you want to initialize:
如果您的目标是 JBoss AS 5.1,并且您不介意使用JBoss EJB 3.0 扩展,您可以构建一个服务 bean 来引导您的 EJB。如果你的服务实现了一个带有@Management注解的接口,并声明了一个带有签名的方法public void start() throws Exception,JBoss会在服务启动时调用这个方法。然后,您可以init()在要初始化的 EJB 上调用专用方法:
@Service
public class BeanLauncher implements BeanLauncherManagement
{
@EJB private SessionBeanLocal sessionBean;
@Override
public void start() throws Exception
{
sessionBean.init();
}
}
@Management
public interface BeanLauncherManagement
{
public void start() throws Exception;
}
More information on this, including additional life-cycle events, can be found here.
可以在此处找到更多关于此的信息,包括其他生命周期事件。
回答by Nayan Wadekar
Managed Beans can be used to do some process at JBoss startup, you have to add entry of that managed bean in configuration file.
托管 Bean 可用于在 JBoss 启动时执行一些过程,您必须在配置文件中添加该托管 Bean 的条目。
回答by Andy
You should be able to add the following line to the top of the method you want to run at startup:
您应该能够将以下行添加到要在启动时运行的方法的顶部:
@Observer("org.jboss.seam.postInitialization")

