在我的Java程序Chrome extension's host(或原生应用)中,每次Chrome扩展程序调用主机时,都会创建 Applet 类的新实例
如何防止这种情况发生?我的意思是我需要为所有主机扩展主机调用设置一个Applet对象,如何实现?
这是我的计划:
import javax.swing.JOptionPane;
public class Applet {
static Applet myApplet;
public Applet(){
System.err.println("new instance created!");
}
public static void main(String[] args) {
try {
if (myApplet == null)
myApplet = new Applet();
myApplet.readMessage();
myApplet.sendMessage("{\"data\": \"somee data\"}");
} catch (Exception ex) {
System.err.println("error");
JOptionPane.showMessageDialog(null, ex.getMessage());
}
}
public String readMessage() {
String msg = "";
try {
int c, t = 0;
for (int i = 0; i <= 3; i++) {
t += Math.pow(256.0f, i) * System.in.read();
}
for (int i = 0; i < t; i++) {
c = System.in.read();
msg += (char) c;
}
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "error in reading message from JS");
}
return msg;
}
public void sendMessage(String msgdata) {
try {
int dataLength = msgdata.length();
System.out.write((byte) (dataLength & 0xFF));
System.out.write((byte) ((dataLength >> 8) & 0xFF));
System.out.write((byte) ((dataLength >> 16) & 0xFF));
System.out.write((byte) ((dataLength >> 24) & 0xFF));
// Writing the message itself
System.out.write(msgdata.getBytes());
System.out.flush();
} catch (IOException e) {
JOptionPane.showMessageDialog(null, "error in sending message to JS");
}
}
}
我相信没有必要添加任何扩展名或background.js代码,但如果您需要查看这些代码,请告诉我。
非常感谢。
答案 0 :(得分:3)
首先 - 使用chrome.runtime.connectNative
代替chrome.runtime.sendNativeMessage
(example)。
在本机应用程序(Java)中,使用无限循环来继续接收消息。目前,您只接收和发送消息。之后,没有其他任何事情发生,所以您的应用程序可以理解退出。 stdio native messaging protocol非常简单:
您的程序应包含实现上述协议的(无限)循环。这是一个具有所需流程的程序结构:
class Applet {
public static void main(String[] args) {
new Applet(); // Initialize application.
}
Applet() {
while (true) {
readMessage();
// ... and save the state for later processing if needed
// Send the reply:
sendMessage("{\"data\": \"somee data\"}");
}
}
// TODO: Implement readMessage and sendMessage (see question).
}
答案 1 :(得分:1)
您需要将对象更改为Singleton。以下代码将以同步方式创建Singleton对象,如果它为null并返回该对象。
public class Applet{
// Object of the class which is going to be Singleton object
// It's necessary to declare it as static, otherwise it wont work
private static Applet applet;
// Private constructor to prevent other classes from initializing this class
private Applet(){}
public static Applet getInstance(){
if( applet == null ){
// Synchronized to prevent more than one initialization
// when two or more methods accessing this method for the first time parallely
synchronized(Applet.class){
if( applet == null ){
applet = new Applet();
}
}
}
return applet;
}
public static void main(String[] args){
Applet.getInstance().readMessage();
}
public String readMessage(){
// Some operations
}
}
答案 2 :(得分:0)
- 确保只创建一个类的一个实例
- 提供对象的全局访问权限
这样的事情:
public class Applet {
private static Applet myApplet = null;
// Private constructor
private Applet(){
System.err.println("New instance created!"); // Should only occur once
}
// Use this to get the single instance of Applet?
public static Applet getInstance() {
if(myApplet == null) {
myApplet = new Applet();
}
return myApplet;
}
}
有关Singleton模式的更多信息,请查看this。