问题描述
我正在创建一个包装陈旧供应商API的RESTful Web服务。一些外部配置将是必需的,并将以文件或rdbms的形式存储在服务器上。我在Glassfish 3.1.2中使用Jersey 1.11.1。
I am creating a RESTful Web Service that wraps an antiquated vendor API. Some external configuration will be required and will be stored on the server either in a file or rdbms. I'm using Jersey 1.11.1 in Glassfish 3.1.2. This configuration data is all in String key/value format.
我的第一个问题是 - 我可以在哪里存储全局变量/实例变量,以便它们在请求并可用于所有资源?如果这是一个纯粹的Servlet应用程序,我会使用ServletContext来实现这一点。
My first question is this - where can I store global/instance variables in Jersey so that they will be persisted between requests and available to all resources? If this was a pure Servlet application I would use the ServletContext to accomplish this.
问题的第二部分是如何在Jersey服务器加载后加载配置?同样,我的Servlet的比喻是找到与init()方法等价的东西。
The second part to the question is how can I load my configuration once the Jersey server has loaded? Again, my Servlet analogy would be to find the equivalent to the init() method.
推荐答案
Singleton @Startup
EJB符合您的要求。
@Singleton @Startup
EJB matches your requirements.
@Singleton
@Startup // initialize at deployment time instead of first invocation
public class VendorConfiguration {
@PostConstruct
void loadConfiguration() {
// do the startup initialization here
}
@Lock(LockType.READ) // To allow multiple threads to invoke this method
// simultaneusly
public String getValue(String key) {
}
}
@Path('/resource')
@Stateless
public class TheResource {
@EJB
VendorConfiguration configuration;
// ...
}
编辑: em>根据Graham的评论添加注释
Added annotation as per Graham's comment
这篇关于如何在Jersey / Glassfish中加载和存储全局变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!