Announcement Announcement Module
Collapse
No announcement yet.
Populate cache on startup Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Populate cache on startup

    I am trying to populate some values from the database into the cache on Application startup.The cache has to be populated first thing for it to be available for other beans.

    What is the best way to do this?
    1. Should I use ContextLoaderListener and write my cache population logic in contextInitialized?
    OR
    2. Should I use bean init-method?

    Thanks

  • #2
    okay I created a quick implementation using ContextLoaderListener. But I have a problem. Here is my code and config.

    ContextLoaderListener :
    public class MyContextListener extends ContextLoaderListener {
    public void contextInitialized(ServletContextEvent event) {
    ServletContext ctx = event.getServletContext();
    WebApplicationContext context = WebApplicationContextUtils.getWebApplicationContex t(ctx);

    PropManager manager = (PropManager) context.getBean("propManager");
    manager.populateCacheValues();
    }
    public void contextDestroyed(ServletContextEvent arg0) {}
    }
    web.xml
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">

    <web-app>
    <display-name>my-spring-service</display-name>

    <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/config/my-spring-service-config.xml</param-value>
    </context-param>

    <!-- Http Flex Session attribute and binding listener support -->
    <listener>
    <listener-class>flex.messaging.HttpFlexSession</listener-class>
    </listener>

    <listener>
    <listener-class>org.springframework.web.context.ContextLoade rListener</listener-class>
    </listener>

    <listener>
    <listener-class>my.own.MyContextListener</listener-class>
    </listener>

    <servlet>
    <servlet-name>my-spring-service</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherSe rvlet</servlet-class>
    <init-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/config/my-spring-service-config.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
    </servlet>


    <!-- Map all *.service requests to the DispatcherServlet for handling -->
    <servlet-mapping>
    <servlet-name>my-spring-service</servlet-name>
    <url-pattern>/spring/*</url-pattern>
    </servlet-mapping>

    <welcome-file-list>
    <welcome-file>index.htm</welcome-file>
    </welcome-file-list>
    </web-app>
    I am having a problem. I am using Spring-BlazeDS integration, I get an exception
    as the MessageBrokerServlet is being loaded twice.

    org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name '_messageBrokerDefaultHandlerMapping': Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name '_messageBroker': Invocation of init method failed; nested exception is org.springframework.beans.factory.BeanInitializati onException: MessageBroker initialization failed; nested exception is java.lang.RuntimeException: MessageBroker already defined from MessageBrokerServlet with init parameter messageBrokerId = '_messageBroker'
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.doCreateBean(AbstractAu towireCapableBeanFactory.java:527)
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.createBean(AbstractAuto wireCapableBeanFactory.java:456)
    So what should I do?

    Comment

    Working...
    X