Discussions
Categories
- 385.5K All Categories
- 5.1K Data
- 2.5K Big Data Appliance
- 2.5K Data Science
- 453.4K Databases
- 223.2K General Database Discussions
- 3.8K Java and JavaScript in the Database
- 47 Multilingual Engine
- 606 MySQL Community Space
- 486 NoSQL Database
- 7.9K Oracle Database Express Edition (XE)
- 3.2K ORDS, SODA & JSON in the Database
- 585 SQLcl
- 4K SQL Developer Data Modeler
- 188K SQL & PL/SQL
- 21.5K SQL Developer
- 46 Data Integration
- 46 GoldenGate
- 298.4K Development
- 4 Application Development
- 20 Developer Projects
- 166 Programming Languages
- 295K Development Tools
- 150 DevOps
- 3.1K QA/Testing
- 646.7K Java
- 37 Java Learning Subscription
- 37.1K Database Connectivity
- 201 Java Community Process
- 108 Java 25
- 22.2K Java APIs
- 138.3K Java Development Tools
- 165.4K Java EE (Java Enterprise Edition)
- 22 Java Essentials
- 176 Java 8 Questions
- 86K Java Programming
- 82 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
- 208 Java User Groups
- 25 JavaScript - Nashorn
- Programs
- 667 LiveLabs
- 41 Workshops
- 10.3K Software
- 6.7K Berkeley DB Family
- 3.6K JHeadstart
- 6K Other Languages
- 2.3K Chinese
- 207 Deutsche Oracle Community
- 1.1K Español
- 1.9K Japanese
- 474 Portuguese
How to use singleinstance service with a JWS application

843802
Member Posts: 39,816
Hello,
I have a Swing application and i am using Java web start to deploy on the user machines.
I want to know how to implement the single instance service. so that only one instance os the application is running.
Where should i use the singleinstance class provided by JNLP api.
for ex:
package test;
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.border.EmptyBorder;
import java.util.Date;
// classes of the web-start API, used in this example.
import javax.jnlp.SingleInstanceListener;
import javax.jnlp.SingleInstanceService;
import javax.jnlp.ServiceManager;
import javax.jnlp.UnavailableServiceException;
/** A test of the SingleInstanceService using the web-start API.
@author Andrew Thompson
@version 2007/1/8
*/
public class SingleInstanceApplication
extends JFrame
implements SingleInstanceListener {
/** A simple editing area. */
JTextArea document;
/** Assemble the GUI. */
SingleInstanceApplication() {
super("JNLP API single instance service");
try {
SingleInstanceService singleInstanceService =
(SingleInstanceService)ServiceManager.
lookup("javax.jnlp.SingleInstanceService");
// add the listener to this application!
singleInstanceService.addSingleInstanceListener(
(SingleInstanceListener)this );
} catch(UnavailableServiceException use) {
use.printStackTrace();
System.exit(-1);
}
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
document = new JTextArea(
"Try openning another version of this application\n");
document.setEditable(false);
JPanel main = new JPanel(new BorderLayout());
main.add(new JScrollPane(document));
main.setBorder( new EmptyBorder(8,8,8,8) );
getContentPane().add(main);
pack();
setSize(400,300);
setLocationRelativeTo(null);
}
/** Specified by the SingleInstanceListener interface
@param args The command line parameters used for this invocation */
public void newActivation(String[] args) {
StringBuffer sb = new StringBuffer();
for (int ii=0; ii<args.length; ii++) {
sb.append("'" + args[ii] + "' ");
}
String message = "Got new args: " + sb.toString();
// this usually serves to alert the user the app.
// wants attention. On Win. it will flash the
// apps. icon in the task bar.
JOptionPane.showMessageDialog(this, message);
// also add the new args and time to the document.
document.append( new Date() + "\t" + message + "\n" );
}
/** Construct the GUI and display it. If the user double clicked
a file to start the application, begin measures to load that file. */
public static void main(String[] args) {
SingleInstanceApplication app =
new SingleInstanceApplication();
app.setVisible(true);
}
}
in my progrma there are many clasess where should i use this.
Pls help new bie!!
Thanks and Regards
I have a Swing application and i am using Java web start to deploy on the user machines.
I want to know how to implement the single instance service. so that only one instance os the application is running.
Where should i use the singleinstance class provided by JNLP api.
for ex:
package test;
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.border.EmptyBorder;
import java.util.Date;
// classes of the web-start API, used in this example.
import javax.jnlp.SingleInstanceListener;
import javax.jnlp.SingleInstanceService;
import javax.jnlp.ServiceManager;
import javax.jnlp.UnavailableServiceException;
/** A test of the SingleInstanceService using the web-start API.
@author Andrew Thompson
@version 2007/1/8
*/
public class SingleInstanceApplication
extends JFrame
implements SingleInstanceListener {
/** A simple editing area. */
JTextArea document;
/** Assemble the GUI. */
SingleInstanceApplication() {
super("JNLP API single instance service");
try {
SingleInstanceService singleInstanceService =
(SingleInstanceService)ServiceManager.
lookup("javax.jnlp.SingleInstanceService");
// add the listener to this application!
singleInstanceService.addSingleInstanceListener(
(SingleInstanceListener)this );
} catch(UnavailableServiceException use) {
use.printStackTrace();
System.exit(-1);
}
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
document = new JTextArea(
"Try openning another version of this application\n");
document.setEditable(false);
JPanel main = new JPanel(new BorderLayout());
main.add(new JScrollPane(document));
main.setBorder( new EmptyBorder(8,8,8,8) );
getContentPane().add(main);
pack();
setSize(400,300);
setLocationRelativeTo(null);
}
/** Specified by the SingleInstanceListener interface
@param args The command line parameters used for this invocation */
public void newActivation(String[] args) {
StringBuffer sb = new StringBuffer();
for (int ii=0; ii<args.length; ii++) {
sb.append("'" + args[ii] + "' ");
}
String message = "Got new args: " + sb.toString();
// this usually serves to alert the user the app.
// wants attention. On Win. it will flash the
// apps. icon in the task bar.
JOptionPane.showMessageDialog(this, message);
// also add the new args and time to the document.
document.append( new Date() + "\t" + message + "\n" );
}
/** Construct the GUI and display it. If the user double clicked
a file to start the application, begin measures to load that file. */
public static void main(String[] args) {
SingleInstanceApplication app =
new SingleInstanceApplication();
app.setVisible(true);
}
}
in my progrma there are many clasess where should i use this.
Pls help new bie!!
Thanks and Regards
Comments
-
To work for your project, the SIS has to be implemented on the class that is the main(). To implement the SIS for a class, it needs to declare it implements the SingleInstanceListener and define the new activation method. Also, it needs to have the SingleInstanceService added to it via. the methods addSingleInstanceListener(). It is not strictly necessary to call addSingleInstanceListener() from the main() class, but it does make a lot of sense.
Does that answer your question?
Edited by: AndrewThompson64 on Dec 15, 2007 1:28 AM
Edited by: AndrewThompson64 on Dec 15, 2007 1:48 AM -
Thank you very much.
Its solved my problem.
Assigned the Duke stars -
This example looks great. Unfortunately it seems to not work with JDK 1.6.0_03. Integrating this code with my app still allows me to launch multiple instances. Then I looked at Andrew's page which has the jnlps and jars ready to go as examples.... and same problem. I can run multiple instances of this example app from Andrew's page. Bummer. I really wanted this to work.
-
That's probably due to a known bug that was introduced in 1.6.0_03. Bug ID 6631056.
This discussion has been closed.