Jersey 2 资源需要清理
Jersey 2 resource needs to cleanup
我有一个在其构造函数中创建对象的单例资源,当应用程序关闭且服务器终止时,我需要释放这些对象。 Jersey 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?
}
...
}
Jersey 支持 @PreDestroy
生命周期钩子。所以只要在class中用@PreDestroy
注解一个方法,Jersey就会在资源释放前调用它
import javax.annotation.PreDestroy;
@Path("/")
@Singleton
public class MyResource {
private Map<String, MyObject> cache;
public MyResource() {
}
@PreDestroy
public void preDestroy() {
// do cleanup
}
}
我有一个在其构造函数中创建对象的单例资源,当应用程序关闭且服务器终止时,我需要释放这些对象。 Jersey 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?
}
...
}
Jersey 支持 @PreDestroy
生命周期钩子。所以只要在class中用@PreDestroy
注解一个方法,Jersey就会在资源释放前调用它
import javax.annotation.PreDestroy;
@Path("/")
@Singleton
public class MyResource {
private Map<String, MyObject> cache;
public MyResource() {
}
@PreDestroy
public void preDestroy() {
// do cleanup
}
}