Meaningless Drivel is fun!*
The moose likes Spring and the fly likes How to start a daemon thread at application startup in a Spring application Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "How to start a daemon thread at application startup in a Spring application" Watch "How to start a daemon thread at application startup in a Spring application" New topic
Author

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

Nikhil Pasupukuntla
Greenhorn

Joined: Jun 04, 2009
Posts: 21
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.


Nikhil Pasupukuntla
SCJP 1.5 | OCPJWCD 1.5
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17257
    
    6

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


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Nikhil Pasupukuntla
Greenhorn

Joined: Jun 04, 2009
Posts: 21
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
Sheriff

Joined: Feb 05, 2001
Posts: 17257
    
    6

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

Joined: Jun 04, 2009
Posts: 21
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to start a daemon thread at application startup in a Spring application