2017-11-13 22 views
0

メインアプリケーションとバックグラウンドアプリケーションをそれぞれ独自のJVMに入れたいと思っています。
メインを起動するとき、バックグラウンドが実行中かどうかをチェックし、バックグラウンドが起動していない場合は起動する必要があります。
私は複数のメインを稼働させていますが、私はバックグラウンドアプリケーションを1つしか必要としません。
すべてのメインが閉じられた後でも、バックグラウンドは稼働し続けても問題ありません。これは、バックグラウンドアプリケーションを作る私の最初のですウィンドウ上のJavaのバックグラウンドアプリケーション

、私は知らない。

  • dettachedアプリケーションを起動する方法。他のアプリケーションが
  • を実行している場合

私は設計段階ではまだだ(私はソケットを使用する必要があります)2つのアプリケーションがそれらの間で通信する方法

  • はどのようにチェックするために(つまりは一緒に終了しません)リモートでも関係のあるコメントや提案は大歓迎です。
    サンプルコードまたはリンクを提供できる場合は、私は感謝します。

  • 答えて

    1

    私の理解では、SingleInstanceアプリケーションを探していて、バックグラウンドプロセスを実行するために終了したくないのですか?

    私は理解していません第3の質問ですが、私はあなたに最初と2番目の質問にまっすぐ答えを与えるつもりです。

    public class Main extends Application { 
    
        @Override 
        public void start(Stage primaryStage) throws Exception{ 
    
         checkIfRunning(); //Check first if the Application is aready running. 
    
         Parent root = FXMLLoader.load(getClass().getResource("fxml/main.fxml")); 
         primaryStage.setTitle("Hello World"); 
         Scene scene = new Scene(root); 
         primaryStage.setScene(scene); 
         primaryStage.show(); 
    
         Platform.setImplicitExit(false); //Prevent the Application from Terminating when it's close 
    
        } 
    
        public static void main(String[] args) { 
         launch(args); 
        } 
    
    
        /**The function for checking if the application is already running start here**/ 
        private static final int PORT = 9999; 
        private static ServerSocket socket; 
        private static void checkIfRunning() { 
         try { 
          //Bind to localhost adapter with a zero connection queue 
          socket = new ServerSocket(PORT,0, InetAddress.getByAddress(new byte[] {127,0,0,1})); 
         } 
         catch (BindException e) { 
          System.err.println("Application already running."); 
          System.exit(1); 
         } 
         catch (IOException e) { 
          System.err.println("Unexpected error when checking if application is already running."); 
          e.printStackTrace(); 
          System.exit(2); 
         } 
        } 
    } 
    

    試してみると、アプリケーションを終了するとまだ実行されており、複数のアプリケーションを起動することはできません。

    +0

    私の3番目の質問は、アプリケーション全体でパラメータ(またはデータ)を渡す方法でした。アイデアはソケットを使用するので、データの送受信にも使用します。 – Happy

    +0

    また、ソケットまたはデータグラムを使用してアプリケーション間で通信することもできます。 – Polar

    関連する問題