1

i have a java project, works as a server. when an instance of this project running, i can run another instance.
how can i avoid running of more than one instance on same java project at the same time? (Stop the server when another instance is detected)

Georgian Citizen
  • 3,727
  • 6
  • 38
  • 46
  • 5
    The accepted answer from http://stackoverflow.com/questions/920386/how-to-allow-running-only-one-instance-of-a-java-program-at-a-time should work for you. –  Mar 03 '11 at 13:44

4 Answers4

3
import java.net.ServerSocket; 
..... 
private static final int PORT = 9999;
private static ServerSocket socket;  
public static void main(String[] args) {

    try {
        socket = new ServerSocket(PORT, 0, InetAddress.getByAddress(new byte[] { 127, 0, 0, 1 }));

                {/*here write your own code taht must be run in the main*/}

    } catch (BindException e) {
        System.err.println("**********************************Already running.");
        System.exit(1);
    } catch (IOException e) {
        System.err.println("************************************Unexpected error.");
        e.printStackTrace();
        System.exit(2);
    } catch (Exception e) {
        System.err.println("************************************ Error");
        System.exit(3);
    }
}

i used this code and it work try it

  • 1
    +1 Good idea! The only problem i see with this solution is that it will not allow a different application to use the same port. It is OK if you remember to assign another port for every application though. If you keep the port in a properties file you can also reconfigure the port and avoid problems with third applications. – Costis Aivalis Mar 04 '11 at 12:02
  • @Costis, it's probably a safe assumption, the software in question is a server, there is a good chance there is going to be something listing on a particular port. – vickirk Mar 04 '11 at 15:29
2

Easiest way is to use lock file, this causes problems if the app crashed. Try writing the pid into the lock file, you can check if that pid exists (although not natively maybe in a wrapper shell script).

If you are running server can you not check if a port is open, or better still maybe a jmx instance on a known port.

vickirk
  • 3,979
  • 2
  • 22
  • 37
1

I totally support @vickirk - his approach allows the second "un-needed" instance of your server become "dormant" instead of simply terminating, i.e. periodically run to perform a check if the "active" instance is still actually active/present, and take over if it went down.

In the distrubuted case, if the requirement is to have a single server instance spanning multiple machines, the approach is still to find a common resource that can be locked, physically or logically. For that purpose, I personally use a control database table where an active process writes its PID and "heartbeat", and all others are checking for that "heartbeat" to be fairly recent, and become active if its not.

mazaneicha
  • 8,794
  • 4
  • 33
  • 52
0

you can write simple command line script for app start - that check is server runs before actually run new instance. Just check url with wget for example...

Dmytro
  • 195
  • 5