问题描述
我有一个MDB EJB,里面有静态块。我在第一次运行应用程序时(即部署时)使用静态块来初始化一些组件。 MDB EJB部署在除Admin_Server之外的单独服务器(My_Server)上。
I have an MDB EJB, with static block inside it. I used the static block to initialize some components only once at the first time the application runs (i.e at deployment). The MDB EJB is deployed on a separate sever (My_Server) other than the Admin_Server.
问题是静态块被调用两次!
The problem is that the static block is called twice!
- 第一次:刚刚部署MDB EJB(tageted到My_Server)。
- 第二次:在JMS队列(与MDB关联)之后收到一条消息。
- The first time: just after deploying the MDB EJB (tageted to the My_Server).
- The second time: after the JMS queue (that the MDB is associated with) receives a message.
另外,我打印了服务器名称和pid,两者都是相同的:
Also, I printed the server name and the pid, and they are same in both:
System.out.println("server name: " + System.getProperty("weblogic.Name"));
System.out.println("pid: " + ManagementFactory.getRuntimeMXBean().getName().split("@")[0]);
我也注意到一些奇怪的行为。基本上,我附上了一个Shutdown Hook(从静态块内部),在关闭应用程序时向我发送电子邮件。
I also noticed some weird behaviors. Basically, I attached a Shutdown Hook (from inside the static block) to send me email on shutdown of the application.
- 我收到通知一次我关闭了My_Server。
- 我在关闭Admin_Server后也收到通知。
如何我可以解决这个问题吗?
How can I solve this issue?
一些额外信息:
Weblogic version: 10.3.0
EJB version: 3.0
推荐答案
只有当您有多个类加载器时才会发生这种情况。我不会在EJB中使用静态初始化器,依赖于EJB生命周期钩子或将初始化器导出到非托管类中。
This only happens when you have multiple classloaders. I would not have static initializers in an EJB, rely on either EJB lifecycle hooks or export the initializer into an unmanaged class.
EJB 3.1添加了@Singleton和@Startup等注释但遗憾的是,在3.0中,您仍然坚持使用基于提供商的解决方案来实现这些保证。
EJB 3.1 added annotations like @Singleton and @Startup, but unfortunately in 3.0 you are stuck with provider-based solutions for these guarantees.
管理员与常规服务器的行为非常普遍,因为WebLogic通常会部署到这两者。有关详细信息,请参见。
Admin vs regular server behavior is quite common as WebLogic usually deploys to both. See this page for more information.
对于Weblogic shutdown hooks,请检查。
For Weblogic shutdown hooks check this documentation page.
这篇关于静态块被调用两次,也许是多个类加载器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!