1

I have a Java application named 'X'. In Windows environment, at a given point of time there might be more than one instance of the application. I want a common piece of code to be executed sequentially in the Application 'X' no matter how many instances of the application are running. Is that something possible and can be achieved ? Any suggestions will help.

Example :- I have a class named Executor where a method execute() will be invoked. Assuming there might be two or more instances of the application at any given point of time, how can i have the method execute() run sequential from different instances ? Is there something like a lock which can be accessed from two instances and see if the lock is currently active or not ? Any help ?

C.Champagne
  • 5,381
  • 2
  • 23
  • 35
User
  • 81
  • 2
  • 9
  • You have to run one central stand alone application with 'execute()' method and call it from other application using TCP or any protocol. This 'execute()' should be synchronized – janith1024 Dec 18 '17 at 09:55
  • Probably trying out something like, a central COM Server in C++ with a JNI setter for flag indicating status as active. Let the java application check the flag from COM server, if its active let it wait(some predefined time and retry again) otherwise set the status to COM as active and continue with execute() Only concern, if two instances say X1 and X2 attempt to set the flag at the same point of time ? How can the clash be avoided ? – User Dec 18 '17 at 11:56

4 Answers4

1

I think what you are looking for is a distributed lock (i.e. a lock which is visible and controllable from many processes). There are quite a few 3rd party libraries that have been developed with this in mind and some of them are discussed on this page.

Distributed Lock Service

There are also some other suggestions in this post which use a file on the underlying system as a synchornization mechanism.

Cross process synchronization in Java

AdamPillingTech
  • 456
  • 2
  • 7
0

To my knowledge, you cannot do this that easily. You could implement TCP calls between processes... but well I wouldn't advice it.

You should better create an external process in charge of executing the task and a request all the the tasks to execute by sending a message to a JMS queue that your executor process would consume.

...Or maybe you don't really need to have several processes running in the same time but what you might require is just an application that would have several threads performing things in the same time and having one thread dedicated to the Executor. That way, synchronizing the execute()method (or the whole Executor) would be enough and spare you some time.

C.Champagne
  • 5,381
  • 2
  • 23
  • 35
0

You cannot achieve this with Executors or anything like that because Java virtual machines will be separate.

If you really need to synchronize between multiple independent instances, one of the approaches would be to dedicate internal port and implement a simple internal server within the application. Look into ServerSocket or RMI is full blown solution if you need extensive communications. First instance binds to the dedicated application port and becomes the master node. All later instances find the application port taken but then can use it to make HTTP (or just TCP/IP) call to the master node reporting about activities they need to do.

As you only need to execute some action sequentially, any slave node may ask master to do this rather than executing itself.

A potential problem with this approach is that if the user shuts down the master node, it may be complex to implement approach how another running node could take its place. If only one node is active at any time (receiving input from the user), it may take a role of the master node after discovering that the master is not responding and then the port is not occupied.

Audrius Meškauskas
  • 20,936
  • 12
  • 75
  • 93
0

A distributed queue, could be used for this type of load-balancing. You put one or more 'request messages' into a queue, and the next available consumer application picks it up and processes it. Each such request message could describe your task to process.

This type of queue could be implemented as JMS queue (e.g. using ActiveMQ http://activemq.apache.org/), or on Windows there is also MSMQ: https://msdn.microsoft.com/en-us/library/ms711472(v=vs.85).aspx. If performance is an issue and you can have C/C++ develepors, also the 'shared memory queue' could be interesting: shmemq API

Axel Podehl
  • 4,034
  • 29
  • 41