简体   繁体   中英

load the value from database when the server is started

I am working on spring and servlet based application. My requirement is to load the data from database and store in ehcache.I have written a listener which is called once the jboss server is started. But the issue is in my application we are using spring hibernate classes to connect to database and load the data from DB. As per the requirement data has to be retrieved from database and store in cache object(ehcache). But when listener class is loaded other xml configuration files(applicationContext.xml..) are not yet loaded where we configure datasource details to connect to database. Below is my code:

Listener class.

import net.sf.ehcache.*;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
/imports
public class MyInitializationListener extends HibernateDaoSupport implements ServletContextListener {

    /** Singleton instance of CacheManager. */
    private static CacheManager singletonManager = null;

    public void contextDestroyed(ServletContextEvent arg0) {
        System.out.println("--ServletContextListener destroyed*--");
    }
    private static CacheManager getInstance() {
        if (singletonManager == null) {
            singletonManager = CacheManager.create();
        }
        return singletonManager;
    }

    private Cache getCache() {
        Cache cache = null;
        cache = MyInitializationListener.getInstance().getCache("myCache");
        return cache;
    }
    // Run this before web application is started

    public void contextInitialized(ServletContextEvent arg0) {
    final Cache cache = getCache();
        final String dbValue = getDBValue();
        //logic here
         }
public String getDBValue() throws DataLayerException{
    //logic to get the value from database

}

Below is the exception:

ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[localhost].[/eas-webservice/ivr]] Exception sending context initialized event to listener instance of class com.data.listener.MyInitializationListener
java.lang.NullPointerException

Did any one face this situation before.What would be the best possible way to connect to database and get the values from database and store in cache when the server is started. Suggestions would be helpful.

--EDITED--

I tried the below way using @PostConstruct to a method to call loadData() when the server is started.But when I checked in log file print statement was not there which states that the method is not being called when the server was started.

@Component
public class MyDataStore {
    @PostConstruct
    public void loadData()
    {
        System.out.println("--In @postconstruct, loadData-");
    }
}

Don't use servlet stuff, just do what you want in a bean with a method annotated @PostConstruct , but that won't necessarily be the 1st bean which is created. If you need to do anything before spring itself starts you'll need to create the application context in a main method manually. Anything you want to happen before spring starts will need to be before that application context is created. If you want some beans to be created before others, it will need to be a dependency in all the other beans, which is really annoying, not aware of any other way to do this.

It is not clear form your question why you need to do cache logic in ServletContextListener exactly, but I can suggest how to access your applicationContext within it.

You can extend org.springframework.web.context.ContextLoaderListener with caching logic and use it to load application context. Also if you need HibernateDaoSupport logic then it can be added using composition instead of inheritance.

So all of this may look like follows:

MyInitializationListener

public class MyInitializationListener extends ContextLoaderListener {

    private static CacheManager singletonManager = null;

    pirvate HibernateDaoSupport hibernateDaoSupport;

    ......

    @Override
    public void contextInitialized(ServletContextEvent event) {
        super.contextInitialized(event);

        ApplicationContext applicationContext = getCurrentWebApplicationContext();

        hibernateDaoSupport = applicationContext.getBeansOfType(HibernateDaoSupport.class).values().iterator().next();

        //Do cahcing logic you need;
    }

    ......

}

web.xml

<!-- spring framework context configuration -->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>

<listener>
    <listener-class>MyInitializationListener</listener-class>
</listener>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM