2

在我的Java程序Chrome extension's host(或本機應用程序)中,每當主機由我的Chrome擴展程序調用時,就會創建一個類的新實例。如何防止每次調用主機時創建類的新實例?

如何防止發生?我的意思是我需要爲所有主機 - 分機 - 主機呼叫設置一個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"); 
     } 
    } 
} 

我beleive沒有必要添加任何擴展或background.js代碼,但請讓我知道如果你需要看的,太。

非常感謝。

+0

嘗試使用[單例模式](https://en.wikipedia.org/wiki/Singleton_pattern)? – Gosu

回答

3

首先 - 使用chrome.runtime.connectNative代替chrome.runtime.sendNativeMessageexample)。

在您的本機應用程序(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). 
} 
+0

謝謝Rob! while循環丟失。 –

0

使用Singleton Pattern

  • 確保只有一個類的實例被創建
  • 提供一個全局訪問點的對象

事情是這樣的:

對於更多關於Singleton模式,請查詢this

1

您需要將對象更改爲單例。如果代碼爲空並且返回該對象,則以下代碼將以同步的方式創建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 
    } 

} 
相關問題