Distributed Lock and Java EE

I am writing a Java EE application using JBoss AS 6, and I have a resource that requires exclusive access (some interface to a third-party software bit) for the method for the given parameter. I am currently naughty (as the specification forbids it ) and using java.util.concurrent.ReentrantLock to block.

Now I am combining several JBoss application servers, so I need a solution that works on different nodes of the cluster. I think I have at least the following options.

  • Shared Cache (Infinispan)
  • Jgroups
  • File system based locking (maybe bad, but we rely on a common file system anyway)
  • Database
  • Singleton EJB 's?

Ideally, I'm looking for a high-level API, so I can write EJB methods like this

 public class MyEJBBean { private SharedLock lock; public void doSomethingWithSharedResource(String s) { lock.lock(); // blocks until shared resource is not used by anyone else try { // Use shared resource } finally { lock.unlock(); } } 

Am I missing any options? Does anyone have any experience with this locking mechanism that they can use?

+4
source share
2 answers

Ideally, I would suggest wrapping third-party software in a separate application that runs on only one instance. This way you can handle the lock using EJB @Singleton (I believe @Singleton will not help you in your scenario) and expose it using remote EJB / WS. It seems like this piece of software is a little nasty (single-threaded?), So a more convenient EJB interface will be an added advantage.

Think about it - if you can access the library once at a time for the entire system, why distribute it? In any case, any instance can always be used.

If you want to stick to a uniform distributed system (which is not a bad idea at all), I would suggest locking the database using SELECT FOR UPDATE . I have never tried, but I think that issuing such SQL code before using your library (getting a lock) and letting the EJB container complete the transaction (actually releasing the lock) will subsequently do the trick.

+2
source

How about using http://hadoop.apache.org/zookeeper/ ? This is a kind of very lightweight file system on distributed systems and a good solution to implement locking.

See this: http://hadoop.apache.org/zookeeper/docs/r3.1.2/recipes.html#sc_outOfTheBox

+2
source

Source: https://habr.com/ru/post/1345577/


All Articles