问题描述
在我的Java程序中,该程序是 (或本机应用程序),每次我的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代码,但是如果您也需要查看这些代码,请告诉我。
非常感谢你。
1楼
首先-使用而不是 ( )。
在本机应用程序(Java)中,使用无限循环来保持接收消息。 目前,您只接收和发送一条消息。 之后,什么也没有发生,因此您的应用程序可以退出。 非常简单:
- 读取32位消息长度(本机字节顺序)。
- 读取JSON编码的消息(长度如前所述)。
- 写入32位消息长度(本机字节顺序)。
- 编写JSON编码的消息(长度如前所述)。
- 重复直到任一端断开端口。
您的程序应包含一个实现上述协议的(无限)循环。 这是具有所需流程的程序结构:
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).
}
2楼
您需要将对象更改为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
}
}
3楼
使用 ?
- 确保仅创建一个类的实例
- 提供对对象的全局访问点
像这样:
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模式的更多信息,请检查 。