如何防止每次调用主机时都创建 class 的新实例?
How to prevent creating a new instance of the class every time the host is called?
在我的 Java 程序中,它是一个 Chrome extension's host(或本机应用程序),每次主机被我的 Chrome 扩展程序调用时,我的 [= =23=]小程序 class 已创建。
如何防止这种情况发生?我的意思是我需要为所有主机-扩展-主机调用提供一个单独的 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 代码,但如果您也需要查看这些,请告诉我。
非常感谢。
- Ensure that only one instance of a class is created
- Provide a global point of access to the object
像这样:
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;
}
}
有关单例模式的更多信息,请查看 this。
您需要将对象更改为单例。如果 Singleton 对象为 null 并且 return 该对象,则以下代码将以同步方式创建 Singleton 对象。
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
}
}
首先 - 使用 chrome.runtime.connectNative
instead of chrome.runtime.sendNativeMessage
(example).
在您的本机应用程序 (Java) 中,使用无限循环来不断接收消息。目前,您只能接收和发送消息。在那之后,没有其他事情发生,所以你的应用程序可以理解地退出。 stdio native messaging protocol很简单:
- 读取 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).
}
在我的 Java 程序中,它是一个 Chrome extension's host(或本机应用程序),每次主机被我的 Chrome 扩展程序调用时,我的 [= =23=]小程序 class 已创建。
如何防止这种情况发生?我的意思是我需要为所有主机-扩展-主机调用提供一个单独的 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 代码,但如果您也需要查看这些,请告诉我。
非常感谢。
- Ensure that only one instance of a class is created
- Provide a global point of access to the object
像这样:
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;
}
}
有关单例模式的更多信息,请查看 this。
您需要将对象更改为单例。如果 Singleton 对象为 null 并且 return 该对象,则以下代码将以同步方式创建 Singleton 对象。
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
}
}
首先 - 使用 chrome.runtime.connectNative
instead of chrome.runtime.sendNativeMessage
(example).
在您的本机应用程序 (Java) 中,使用无限循环来不断接收消息。目前,您只能接收和发送消息。在那之后,没有其他事情发生,所以你的应用程序可以理解地退出。 stdio native messaging protocol很简单:
- 读取 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).
}