Skip to content

Class Jun

hxs edited this page Nov 27, 2019 · 9 revisions

Jun

Package jun
File Jun.java

Jun is the class with which you can run a single instance of the application.
This class implements the singleton pattern, so to get the Jun instance you need to use the getInstance() method.

Example: Jun.getInstance().

View the examples

Methods


void tryLock(String path)

throws IOException, ApplicationException, SystemException

This method try to lock the instance. It search in the path of the file passed at the input, to check that there is no other instance running.
Use this at the start of your application.

Parameters:

  • path, path to the lock file

Exceptions:

  • IOException, exception thrown if an I/O error is returned while working on the file
  • ApplicationException, exception thrown if an instance of the application is already running
  • SystemException, exception thrown if the searching for the pid return an error

Example:

try {
  Jun.getInstance().tryLock("file.lock");
} catch (ApplicationException e) {
  System.out.println("This application is already running.");
  System.exit(-1);
}

void unlock(String path)

This method unlock the file, and allows the execution of a new instance.
Use this method in the final part of the main code.

Parameters:

  • path, path to the lock file to be unlocked

Example:

Jun.getInstance().unlock("file.lock");

void forceLock(String path)

throws IOException, FileLockException

This method force the lock on the file.

Parameters:

  • path, path to the lock file to be forced

Exceptions:

  • IOException, exception thrown if an I/O error is returned while working on the file
  • FileLockException, exception thrown if an error is returned while locking the file

Example:

Jun.getInstance().forceLock("file.lock");

void tryLock()

throws IOException, ApplicationException, SystemException

This method try to lock the instance. It search in the default file, to check that there is no other instance running.
Use this at the start of your application.

The default file is 'single.instance.app' file in the current working directory.

Exceptions:

  • IOException, exception thrown if an I/O error is returned while working on the file
  • ApplicationException, exception thrown if an instance of the application is already running
  • SystemException, exception thrown if the searching for the pid return an error

Example:

try {
  Jun.getInstance().tryLock();
} catch (ApplicationException e) {
  System.out.println("This application is already running.");
  System.exit(-1);
}

void unlock()

This method unlock the default file, and allows the execution of a new instance.
Use this method in the final part of the main code.

The default file is 'single.instance.app' file in the current working directory.

Example:

Jun.getInstance().unlock();

void forceLock()

throws IOException, FileLockException

This method force the lock on the default file.

The default file is 'single.instance.app' file in the current working directory.

Exceptions:

  • IOException, exception thrown if an I/O error is returned while working on the file
  • FileLockException, exception thrown if an error is returned while locking the file

Example:

Jun.getInstance().forceLock();