• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to start a daemon thread at application startup in a Spring application

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I am new to Spring and here is what I want to do.
I want to create a daemon thread on application startup which periodically pings the database and gets the data into the memory(collections) to be used up by the web application.
This thread must have access to the other beans (to update the DB for instance).

Here is my problem:
I am planning to use Spring for the dependency injection including for my "daemon bean". However, i can do it easily through a servletcontextlistener to start up my thread. If i do that then I will not be able to leverage the dependency injection if I start my thread this way.

My question is therefore the following:
How to start a daemon thread at application startup time in a Spring application?

Your help would be greatly appreciated.
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would think of another solution. What is the purpose of the daemon thread, and is there already a solution for what you need that doesn't require you creating a daemon thread?

Is this just for a data cache. If so, why not just use a third party caching product like EHCache or others.

Or, maybe your caching can be self created using Spring AOP.

You can always preload data with an init-method in xml or @PostConstruct with annotations.

If you are stuck with just creating a daemon thread, you can just create the code, register it as a Spring bean and have the startup be done in the init-method or @PostConstruct approach I said above. Because this class will be a bean, you just inject your DAO/Repositories directly into it the normal Spring way.

Mark
 
Nikhil Pasupukuntla
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mark,

Thanks for the input. I didnt know of tools like Ehcache. Was reading the documentation and code samples but too much to get it in a day. My reason for creating a daemon was to periodcally(say every 2mins) query the database to get the latest data from it which keeps changing. While Ehcache solves my problem of caching the data but i wanted to know can it refresh the data in the cache periodcially from the database or do i need to force it using a process? I am using Ibatis and Spring for the implementation.
 
Mark Spritzler
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Nikhil Pasupukuntla wrote:Mark,

Thanks for the input. I didnt know of tools like Ehcache. Was reading the documentation and code samples but too much to get it in a day. My reason for creating a daemon was to periodcally(say every 2mins) query the database to get the latest data from it which keeps changing. While Ehcache solves my problem of caching the data but i wanted to know can it refresh the data in the cache periodcially from the database or do i need to force it using a process? I am using Ibatis and Spring for the implementation.



Yes, when you configure your cache regions you can state its freshness in time to go back to the database. Now if the data is a lot of constant changing data, a cache like this might not be any solution with a third party tool or not. However, there are cache products like GemFire that are all data cached solutions, but it is very expensive, but extremely powerful.

If you put data that changes often into some other cache, then you might be spending all your CPU cycles keeping the cache up to date. But then again, maybe for your app every two minutes it runs a .5 second query and updates the cache in .5 second, then you might not notice a slow down.

Good Luck

Mark
 
Nikhil Pasupukuntla
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mark,

Thanks again. EHcache and seems like an easy standard solution for my problem. I see that EHcache integrates easily with Hiberate, its just some simple configurations.I am using ibatis for my project, i dont see any articles for integrating ibatis with EHcache on their website. Could you point me to good tutorials for integrating EHcache with ibatis.
 
reply
    Bookmark Topic Watch Topic
  • New Topic