如何防止每次调用主机时都创建类的新实例? [英] How to prevent creating a new instance of the class every time the host is called?
问题描述
在我的Java程序中,每一个 Chrome扩展程序的主机(或本机应用程序)当我的Chrome扩展程序调用主机时,就会创建 Applet 类的新实例.
In my Java program which is a Chrome extension's host (or a native app), every time the host is called by my Chrome extension, then a new instance of my Applet class is created.
如何防止这种情况发生?我的意思是,所有主机扩展名-主机调用都需要有一个Applet对象,该如何实现?
How to prevent that from happening? I mean I need to have one single object of Applet for all host-extension-host calls, how to achieve that?
这是我的程序:
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代码,但是如果您也需要查看这些代码,请告诉我.
I beleive there is no need for adding any extension or background.js code, but please let me know if you need to see those, too.
非常感谢您.
推荐答案
首先-使用 chrome.runtime.connectNative
而不是 chrome.runtime.sendNativeMessage
(示例).
在您的本机应用程序(Java)中,使用无限循环来不断接收消息.目前,您只接收和发送一条消息.在那之后,什么都没有发生,因此您的应用程序可以退出. stdio本机消息传递协议非常简单:
In your native application (Java), use an infinite loop to keep receiving messages. At present, you're only receiving and sending a message. After that, nothing else happens, so your application understandably exits. The stdio native messaging protocol is very simple:
- 读取32位消息的长度(本机字节顺序).
- 读取JSON编码的消息(长度如前所述).
- 写入32位消息的长度(本机字节顺序).
- 写入JSON编码的消息(长度如前所述).
- 重复直到任一端断开端口.
您的程序应包含一个实现上述协议的(无限)循环.这是具有所需流程的程序结构:
Your program should contain an (infinite) loop that implements the above protocol. This is a program structure that has the desired flow:
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).
}
这篇关于如何防止每次调用主机时都创建类的新实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!