我有一个单例资源,它在构造函数中创建对象,当应用程序关闭,服务器终止时,我需要释放这些对象。在泽西2号是怎么做到的?
@Path("/")
@Singleton
public class MyResource {
private Map<String, MyObject> cache;
public MyResource() {
cache = new ConcurrentHashMap<>();
// at some point I need to remove all entries
// from the map and close all MyObject objects there
//
// the reason is because MyObject might have files open
// and I need to close the files
//
// where can I do that?
}
...
}发布于 2016-11-18 04:47:23
泽西支持@PreDestroy生命周期挂钩。因此,只要用@PreDestroy注释类中的一个方法,泽西就会在释放资源之前调用它。
import javax.annotation.PreDestroy;
@Path("/")
@Singleton
public class MyResource {
private Map<String, MyObject> cache;
public MyResource() {
}
@PreDestroy
public void preDestroy() {
// do cleanup
}
}https://stackoverflow.com/questions/40669423
复制相似问题