开发者

Initialize Java EE Application Cache on Startup

开发者 https://www.devze.com 2023-01-04 04:46 出处:网络
I am writing a Java EE application which calculates a lot of things by reading from files. This process takes a lot of time and I want it to be cached automatically everytime the application is deploy

I am writing a Java EE application which calculates a lot of things by reading from files. This process takes a lot of time and I want it to be cached automatically everytime the application is deployed.

So, I was thinking of making a static class and storing my cache results in a static hashmap of some sort.

But any ideas on how to automate deployment and initialize that cache? Do I have to manually visit that application and initialize t开发者_StackOverflow中文版he cache or is there a better way out?


Assuming you have a webapp, the easiest thing to do is use a ServletContextListener to initialize the app on startup.

http://java.sun.com/javaee/6/docs/api/javax/servlet/ServletContextListener.html

public class MyListener implements ServletContextListener {

   public void contextInitialized(ServletContextEvent sce) {
      // initialize cache here
   }

   public void contextDestroyed(ServletContextEvent sce) {
      // shut down logic?
   }
}

And then in your web.xml:

<listener>
   <listener-class>com.x.MyListener</listener-class>
</listener>
0

精彩评论

暂无评论...
验证码 换一张
取 消