org.gnome.unique
public class Application extends Object
The basic algorithm is to create an Application instance with the unique name corresponding to your prgram, and then check to see if there is another already running. If so, then there is no need for this instance and it should terminate.
app = new Application("com.example.Program", null); if (app.isRunning()) { System.exit(1); }
There is a message-passing facility built into LibUnique which allows you to send basic commands to the other running instance before you terminate this one.
app.sendMessage(Command.ACTIVATE, null);
Keep in mind that this is not really meant as a generic all-powerful n to m interprocess communication mechanism. In fact, the entire LibUnique library is mostly about ensuring you only have one master copy of an application running. But once you've established that, the messaging subsystem can be used to convey simple requests to of that unique instance.
Modifier and Type | Class and Description |
---|---|
static interface |
Application.MessageReceived
The signal emitted when another instance sends a message to the unique
instance.
|
Constructor and Description |
---|
Application(String name,
String id)
Construct an Application object for the specified unique service.
|
Modifier and Type | Method and Description |
---|---|
void |
connect(Application.MessageReceived handler)
Hookup a
Application.MessageReceived handler. |
String |
getName()
Get the application name that was used when this Application was
constructed.
|
boolean |
isRunning()
Is some other instance of this program (ie the application with
this name) already running?
|
Response |
sendMessage(Command cmd,
MessageData data)
Send a message to the other instance (assuming there is one).
|
public Application(String name, String id)
By convention, the name chosen to identify a unique application should follow the application naming conventions used by DBus (these are similar to the domain name -esque conventions used in Java package space). Some examples are:
"org.gnome.Nautilus"
"org.gnome.Vino.Preferences"
"com.operationaldynamics.Slashtime"
public void connect(Application.MessageReceived handler)
Application.MessageReceived
handler.public String getName()
public boolean isRunning()
If so, then really you probably want to be terminating this program. If not, then carry on with your normal application initialization and start-up.
public Response sendMessage(Command cmd, MessageData data)
This method blocks.
If you have specific information to convey to the other instance, you can create a MessageData object and set its payload accordingly. You can also create custom Commands through subclassing, although that's rarely necessary.
You can get a number of reponses back from the other instance. You
should reasonably expect OK
. Indeed, if it's not ok
there isn't much you can do about it.