Jax-ws @PreDestroy 它什么时候被准确调用?
我有一个使用 @PostConstruct 和 @PreDestory 注释的简单 Web 服务。
@PostConstruct
private void init() {...} //initialize some database connection
@PreDestroy
private void release() {...} //release data base resources
然后客户端会调用一些web方法来进行一些数据库操作。我通过在代码中设置断点进行了简单的测试。 @PostConstruct 工作正常。但@PreDestroy 方法永远不会被调用。
我认为当客户端完成调用 Web 方法时,@PreDestroy
总是会被调用,因为 Web 服务本质上是无状态的。所以最后,实例总是被销毁,在此之前,应该调用我的release方法?这是正确的理解吗?
但在阅读了一些在线资源后,我感到困惑。有人说 @PreDestroy
在未部署时会被调用?
I have a simply web service using @PostConstruct
and @PreDestory
annotations.
@PostConstruct
private void init() {...} //initialize some database connection
@PreDestroy
private void release() {...} //release data base resources
then a client will call some web methods to do some database operations. I did a simply testing by setting break points in the code. The @PostConstruct
works fine. but @PreDestroy
method never get called.
I thought @PreDestroy
will always get called when a client finish calling a web method since web service is stateless by nature. So in the end, the instance is always destroyed and before that, my release method should be called? Is this a correct understanding?
But after reading some online resources, i got confused. some says @PreDestroy
will be called when it's un-deployed?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
@PreDestroy
仅在应用程序服务器决定减小 Method-Ready 池的大小时调用 - 即它确定不需要保留尽可能多的@WebService @Stateless 实例
会话 bean 周围。每次调用@WebMethod
后不会调用它(并且仅当将新实例添加到 Method-ready 池中时才会调用@PostConstruct
,而不一定是在之前每个 Web 方法调用)。如果您需要在每次方法调用之前和之后调用逻辑,您可以按如下方式执行:
可以将此方法添加到您的
@WebService
bean 中,或者使用@Interceptors
作为单独的类添加代码>@PreDestroy
is only called when the application server decides to reduce the size of the Method-Ready pool - i.e. it determines it doesn't need to keep as many instances of your@WebService @Stateless
session bean around. It doesn't get called after each invocation of your@WebMethod
(and@PostConstruct
is only called when a new instance is added to the Method-ready pool, not necessarily before each web method invocation).If you have logic you need called before and after each method invocation you could do it as follows:
This method can be added to your
@WebService
bean or as a separate class using@Interceptors