Single instance running of client application

I have a single instance of a jaca client. I want to force it to only run one instance. What is the easiest way to handle that in? Give it a special name of the process and then check that no other processes are running with that name?
How do I do that easiest or is there a better way?
mdolandAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CEHJCommented:
A common trick here, that (importantly) works cross-VM, is to have the one and only client create a throwaway socket on a given port. Further instances won't be able to do that, as the port will already be bound and therefore they won't be allowed to run.
mdolandAuthor Commented:
What is the pro / con compared to creating a named process?
CEHJCommented:
How are you going to do that in Java?
Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

krakatoaCommented:
>What is the pro . . .<

Seems to be that you'd only need to catch the SocketException. Not sure whether there would be some latency issues with that however.
mdolandAuthor Commented:
Good point. There are some ways, but none are particularly tempting.

This port-trix. Is it a particularly client port you try to open? What if it is used, or are you using below 1024-ports? Or UDP maybe?
CEHJCommented:
It's not bullet-proof. You're going to have to try to ensure other legitimate software doesn't need to use the port
krakatoaCommented:
Alternatively you could use the classic singleton approach, or rename or delete the .class file from within the single client and rename or recreate it again in a finally statement  - that should take care of things.
mdolandAuthor Commented:
The background in my particular case is that the environment will only be WIndows.  Also, the environment is very hetrogenous with almost no control of the hosts. To me the socket solution is intresting, but maybe worth to compare to other ways to handle it.

-> krakatoa, could you explain your solution in more detail please? Singleton is usually in the same process. In this case I want to avoid someone starting up a new java-process. What do you mean? What will happen if the client die? Can I restart it with your approach?

What about writing a "lock-file" to a subfolder of the current OS user? The file is openened in writing mode. potentially the process id could be written in it also if it would have to be verified in some other way, but that should not be necesarry.  Would it work?
CEHJCommented:
What about writing a "lock-file" to a subfolder of the current OS user?

Yes, that would work and is kind of similar to the socket. You need to ensure the lock file gets deleted come what may (use shutdown hooks)
mdolandAuthor Commented:
Does it really need to be deleted? If I open the file for writing it will be locked, right? If the process then dies controlled or uncontrolled. Won't the file-lock be dropped then?
krakatoaCommented:
I'm not sure why you don't just go for a 'standard' approach - make the constructor private and the instance static so it gets created on load, then return the (new) object instance via a public static method, if and only if the instance is presently null.
Jim CakalicSenior Developer/ArchitectCommented:
File and process handling abilities are typically OS-specific. I've used ServerSocket (as mentioned) to ensure only one process instance. Here's a link that explains:
http://www.rgagnon.com/javadetails/java-0288.html

Google java prevent multiple instances serversocket and you'll find plenty of examples.

Regards,
Jim

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CEHJCommented:
If I open the file for writing it will be locked, right?
Exclusive locks are (iirc) only advisory on some OSs, so ... not necessarily

If the process then dies controlled or uncontrolled. Won't the file-lock be dropped then?
See above

It's the presence of a file that will be the signal, not whether it has a write lock
CEHJCommented:
I'm not sure why you don't just go for a 'standard' approach
Because that will only work for instances in the same VM
krakatoaCommented:
I suppose I can't imagine a scenario where you would  want only just one client out of a possible set of (distrubuted) clients, to access a service . . . that sounds simply like two machines talking . . . . . but no doubt I'm disadvantaged in my perception of what's required.
CEHJCommented:
I suppose I can't imagine a scenario where you would  want only just one client out of a possible set of (distrubuted) clients
afaics it's one client on one particular box, but even that is slightly mysterious, i must admit
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.