Tag Archive: system

Many times we write an application that should be the only instance of that application running.  That application may monitor inputs, control outputs, send email, or perform logic.  Whatever that application is doing, it should be the only one doing that particular thing at any given time.

The ensure that this happens we use the JANOS.registerProcess(id) method.  This method returns the number of processes that have been registered with that given id.  That id can be any unique identifier given to that application.  It can be a name or a random UUID.

If the registerProcess() method returns a value greater than one then we will exit our application.  This will leave the first instance running.  Here is an example.

        // make sure there is only 1 instance of this application running
        if (1 < JANOS.registerProcess(Application.getAppName())) {
            JANOS.syslog("Another instance of " + Application.getAppName() + " is running");
        }

When we want to reboot the JNIOR from our Java application we call the command line reboot command.  To call to the command line we use the ConsoleProcess class.  Since there will not be any user to confirm the reboot we need to use the -f command to force the reboot.

        try {
            // the -f option forces the reboot without the need for user confirmation
            ConsoleProcess consoleProcess = new ConsoleProcess("reboot -f");
            consoleProcess.waitPrompt();
        } catch (Exception ex) {
            throw new IOException("Error rebooting").initCause(ex);
        }

Sometimes it is vital to ensure that only one copy of an application is running.  To do that JANOS has provided us the ability to register a process with the operating system.  Using the registerProcess(uid) call we can get the number of processes running with the given unique identifier.  This can become an issue if you have multiple run keys set to start the same application.

Here is the demo application.  The one instance application is started with the & parameter.  This tells the console session to run the application as a separate process.  The First time the application run it returns a process count of 1.  The process is allowed to proceed.

Since the application is running as a separate process from the console session we can start another instance now.  The returned count is now 2.  The code checks for a count greater than 1 and decides to exit.

Using this feature will help you be sure that there is only ever one instance of your application running at a time.  Here is the source for the sample application.

package oneinstance;

import com.integpg.system.JANOS;



public class Oneinstance {

    public static void main(String[] args) {
        // we assign a unique identifier to this application.  This can be ANY string.
        String uid = "abcdef";

        // we register the process with JANOS.  this call will return the number of processes 
        //  running with this uid.  The result should be 1 indicating that our process is now running.
        int processCount = JANOS.registerProcess(uid);
        System.out.println("# of processes running using this UID: " + processCount);
        if (processCount > 1) {
            System.out.println("There is another copy of this application already running. exit now.");
            return;
        }

        // now sleep for a while to give us time to start other instances of this application as a test
        try {
            System.out.println("We are allowed to continue as the only instance of this applicaiton");
            Thread.sleep(60000);
        } catch (InterruptedException ex) {
            ex.printStackTrace();
        }
    }

}