| Author |
How to control a thread to run in a single instance where the code is deployed in multiple instances
|
Kannappan Somasundaram
Greenhorn
Joined: Apr 03, 2012
Posts: 2
|
|
Hi Everyone,
I need to update a cache, the thread should run only in one instance for updating the cache. The code is deployed in multiple instances in production. The cache which i update is a distributed one. To avoid multiple instances running the same query for updating the i would like to know if there is way here to acheive it?
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16680
|
|
Thread synchronization are for threads within the same JVM -- not across JVMs. For that type of synchronization, you should consider other technologies, like having network connection, using a common database, or any other form or inter-application communication.
On the other hand, there is an open source product, made by terracotta, which uses network connections to synchronize but does so while maintain the semantics of thread synchronization.
Henry
|
Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
|
 |
Kannappan Somasundaram
Greenhorn
Joined: Apr 03, 2012
Posts: 2
|
|
I am putting the exact scenario below,
I have a common database accessed by two applications. When i update/insert in the database the the other application needs to be aware of it. For this what i am doing is, a thread runs every five minutes to compare the objects from DB and the object from cache in the other application. The cache is distributed one and updating the cache will update all instances. I want this thread to be running in single instance since the object comparison is a time consuming since we are checking for multiple conditions.
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16680
|
|
Kannappan Somasundaram wrote:
I have a common database accessed by two applications. When i update/insert in the database the the other application needs to be aware of it. For this what i am doing is, a thread runs every five minutes to compare the objects from DB and the object from cache in the other application. The cache is distributed one and updating the cache will update all instances. I want this thread to be running in single instance since the object comparison is a time consuming since we are checking for multiple conditions.
As already mentioned, Java thread synchronization is designed to only synchronize threads within a single JVM. If you need to cross many JVMs, you can either (1) uses the database, or (2) use a product like Terracotta.
Henry
|
 |
Jayesh A Lalwani
Bartender
Joined: Jan 17, 2008
Posts: 1262
|
|
Kannappan Somasundaram wrote:I am putting the exact scenario below,
I have a common database accessed by two applications. When i update/insert in the database the the other application needs to be aware of it. For this what i am doing is, a thread runs every five minutes to compare the objects from DB and the object from cache in the other application. The cache is distributed one and updating the cache will update all instances. I want this thread to be running in single instance since the object comparison is a time consuming since we are checking for multiple conditions.
So, then run the thread only in one process. Denote one of the processes as the "master" and the master process should start the housekeeping thread, whereas all other processes don't start the housekeeping thread
|
 |
 |
|
|
subject: How to control a thread to run in a single instance where the code is deployed in multiple instances
|
|
|