• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

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

 
Greenhorn
Posts: 2
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
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

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
 
Create symphonies in seed and soil. For 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