• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to control a thread to run in a single instance where the code is deployed in multiple instances

 
Kannappan Somasundaram
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21427
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
Kannappan Somasundaram
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21427
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 2756
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic