Initialize Java EE Application Cache on Startup

I am writing a Java EE application that computes a lot of things while reading from files. This process is time consuming and I want it to be automatically cached every time I deploy the application.

So, I was thinking about creating a static class and storing the cache results in a static hash map.

But any ideas on how to automate the deployment and initialize this cache? Should I manually visit this application and initialize the cache or is there a better way out?

+4
source share
1 answer

Assuming you have a webapp, the easiest way is to use the ServletContextListener to initialize the application at 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> 
+5
source

Source: https://habr.com/ru/post/1313006/


All Articles