• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Executing webservlet after Spring FilterDispatcher Servlet initialized

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
We use Spring MVC annotations.
I have a StartUpServlet as well. My requirement is to load this servlet after the FilterDispatcher servlet is initialized.

Basically, we would like to initialize the Google Guava cache and load the cache with the data from the table.

I can have a static block and load the data. But the problem is the class where we do the cache initialization and cache loading might be garbage collected if we never access it for long time.
So, though of having the cache loading class instance refernece inside a servlet so that it will never be garbage collected. But the servlet gets loaded before the FrameworkServlet dispatcher initialized and Dependency injections are working.

Looking for help on this ......

Thanks in Advance,
Baskar.S
 
Ranch Hand
Posts: 123
Firefox Browser Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If the intent is to preload some data to improve system performance or user experience, then it will help to set your cache to never expire.

This can be done also in ehcache using the 'eternal' setting.

- k


----------------------------------------------------------------------------------------
[SpringSource Certified Spring Professional (Spring Certification) - Practice Tests]
[SpringSource Certified Spring Web Application Developer (Spring Certification) - Practice Tests]
 
Baskar Sikkayan
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Thanks for your reply.
Our goal is to cache codes table and city detail table. We use google guava to do this.




We will have to load this cache while starting the App server.



Hope, this listener class will never be garbage collected, hence the MemCacheFactory object will never be garbage collected and my problem is solved.


Thanks,
Baskar.S

 
Kathleen Angeles
Ranch Hand
Posts: 123
Firefox Browser Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How is the ApplicationContext loaded in the application?

Below is an idea.

=======================================================================

1. Define the static data in a Spring singleton bean instead of in a listener.
2. Keeping the Spring singleton bean alive, and never garbage collected:

If the data is for the whole application, then it is better to put the mentioned Spring singleton bean in the root application context of your web application, as compared to a servlet context.

ContextLoaderListener can be used to load the suggested Spring singleton bean, into the root application context.



=======================================================================

This idea above relies on if the root application context Spring container will stay alive during the entire life of the web application, and that the Spring container will never garbage-collect the Spring singleton bean instance in it, while the Spring container is alive.
 
Rancher
Posts: 2759
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The way you are doing this is very anti-Spring. In Spring based apps, you don't need to have Singletons because you it;s much easier to create single instances of classes. Why aren't you simply declaring codesCache and cityCache as Spring beans? You can easily implement factory beans, one for each that can initialize the 2 caches. You can then autowire the 2 caches wherever you need them. Also, this ensures that your initialization routines will be called by spring at the correct point of the lifecycle. Also, you can use lifecycle annotations on your classes to do other init and cleanup

It seems to me like you are trying to jam a ServiceLocator pattern onto a framework that already has Dependency Injection. You have DI.. use DI.
 
No matter. Try again. Fail again. Fail better. This time, do it with this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic