Discussions
Categories
- 197.1K All Categories
- 2.5K Data
- 546 Big Data Appliance
- 1.9K Data Science
- 450.7K Databases
- 221.9K General Database Discussions
- 3.8K Java and JavaScript in the Database
- 31 Multilingual Engine
- 552 MySQL Community Space
- 479 NoSQL Database
- 7.9K Oracle Database Express Edition (XE)
- 3.1K ORDS, SODA & JSON in the Database
- 555 SQLcl
- 4K SQL Developer Data Modeler
- 187.2K SQL & PL/SQL
- 21.3K SQL Developer
- 296.3K Development
- 17 Developer Projects
- 139 Programming Languages
- 293K Development Tools
- 110 DevOps
- 3.1K QA/Testing
- 646.1K Java
- 28 Java Learning Subscription
- 37K Database Connectivity
- 158 Java Community Process
- 105 Java 25
- 22.1K Java APIs
- 138.2K Java Development Tools
- 165.3K Java EE (Java Enterprise Edition)
- 19 Java Essentials
- 162 Java 8 Questions
- 86K Java Programming
- 81 Java Puzzle Ball
- 65.1K New To Java
- 1.7K Training / Learning / Certification
- 13.8K Java HotSpot Virtual Machine
- 94.3K Java SE
- 13.8K Java Security
- 204 Java User Groups
- 24 JavaScript - Nashorn
- Programs
- 466 LiveLabs
- 39 Workshops
- 10.2K Software
- 6.7K Berkeley DB Family
- 3.5K JHeadstart
- 5.7K Other Languages
- 2.3K Chinese
- 175 Deutsche Oracle Community
- 1.1K Español
- 1.9K Japanese
- 233 Portuguese
Ensure single instance of Java application

807580
Member Posts: 33,048 Green Ribbon
Hi,
I'd like to make sure that only one instance of my Java application can be run and I've done some search around. The recommended approach seems to be using ServerSocket that occupies a certain port.
On the other hand I've seen that JConsole could display the names of all Java processes that are running. This is perhaps another approach to the issue? JConsole is said to be a pure Java tool, so I was wondering how this is implemented. I've googled a bit but still could not find out how. Could someone please give me a hint?
Thanks a lot!
I'd like to make sure that only one instance of my Java application can be run and I've done some search around. The recommended approach seems to be using ServerSocket that occupies a certain port.
On the other hand I've seen that JConsole could display the names of all Java processes that are running. This is perhaps another approach to the issue? JConsole is said to be a pure Java tool, so I was wondering how this is implemented. I've googled a bit but still could not find out how. Could someone please give me a hint?
Thanks a lot!
Comments
-
You could take a look at the source code. This forum thread seems to indicate where to find it:
[http://forums.sun.com/thread.jspa?threadID=788974|http://forums.sun.com/thread.jspa?threadID=788974] -
SwordDevil wrote:Not especially. How it finds local Java processes is primarily platform-specific, and messy. Or it uses JMX, if it can. That's a possibility, but I'm not seeing a clear advantage over the simple server socket approach.
Hi,
I'd like to make sure that only one instance of my Java application can be run and I've done some search around. The recommended approach seems to be using ServerSocket that occupies a certain port.
On the other hand I've seen that JConsole could display the names of all Java processes that are running. This is perhaps another approach to the issue? JConsole is said to be a pure Java tool. -
Use some kind of locking mechanism. This method uses a simple lock on a file and cleans up behind itself reliably and works across all platforms...
private static boolean lockInstance(final String lockFile) { try { final File file = new File(lockFile); final RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rw"); final FileLock fileLock = randomAccessFile.getChannel().tryLock(); if (fileLock != null) { Runtime.getRuntime().addShutdownHook(new Thread() { public void run() { try { fileLock.release(); randomAccessFile.close(); file.delete(); } catch (Exception e) { log.error("Unable to remove lock file: " + lockFile, e); } } }); return true; } } catch (Exception e) { log.error("Unable to create and/or lock file: " + lockFile, e); } return false; }
Or if you can think of another locking mechanism through DB interaction, that would be equally effective as well. -
kilyas wrote:Not in the case of a power cut.
Use some kind of locking mechanism. This method uses a simple lock on a file and cleans up behind itself reliably -
Using a ServerSocket is basically a one liner. It is the simplest approach IMHO.
-
Does this lock mechanism work when e.g. the user kills the app using Task Manager? I've never tested this myself although some discussions say it may fail.
-
SwordDevil wrote:So what, in your mind, is wrong with the ServerSocket approach? Why do all "newbies" immediately discard this approach and then go out and try all these other complex, unreliable, obscure, and/or otherwise questionable practices?
Does this lock mechanism work when e.g. the user kills the app using Task Manager? I've never tested this myself although some discussions say it may fail. -
ServerSocket seems to be a reliable approach, and it's simple, and I like it. I'm just curious how JConsole detects all running Java processes.
-
SwordDevil wrote:I think one has to be careful in that "pure Java" != "platform independence" even though at first glance you might think otherwise. Some code could be "pure Java" in that it doesn't actually call any native code (directly) but it could still be platform dependent in doing things like for example examining process lists on an OS. Of course that code could also detect which OS and do the "right thing" for such activity based on that information but it's not completely platform independent in the "run anywhere" philosophy.
ServerSocket seems to be a reliable approach, and it's simple, and I like it. I'm just curious how JConsole detects all running Java processes. -
SwordDevil wrote:It varies from platform to platform.
ServerSocket seems to be a reliable approach, and it's simple, and I like it. I'm just curious how JConsole detects all running Java processes.
This discussion has been closed.