2012-02-11 9 views
0

ネットワークソケットを使用するネットワークサーバーでOSGIバンドルを実装しようとしています。 http://www.2shared.com/file/RMXby331/CB_27.htmlネットワークサーバーでOSGIバンドルを展開するときの無限ループ

これは、活性化因子である: これは完全なソースコードです

package org.DX_57.osgi.CB_27.impl; 

import java.util.Properties; 
import org.DX_57.osgi.CB_27.api.CBridge; 
import org.DX_57.osgi.CB_27.impl.EchoServer; 
import org.osgi.framework.BundleActivator; 
import org.osgi.framework.BundleContext; 
import org.osgi.framework.ServiceRegistration; 

public class CBridgeApp implements BundleActivator { 

    public void start(BundleContext bc) throws Exception { 
     ServiceRegistration registerService = bc.registerService(CBridge.class.getName(), new CBridgeImpl(), new Properties()); 
     EchoServer(); 
    } 

    public void stop(BundleContext bc) throws Exception { 
     boolean ungetService = bc.ungetService(bc.getServiceReference(CBridge.class.getName())); 
    } 

    private void EchoServer() { 
     EchoServer method = new EchoServer(); 
    } 
} 

これは、ソースコードがある場合、Javaネットワークサーバー:

package org.DX_57.osgi.CB_27.impl; 

import java.net.*; 
import java.io.*; 

public class EchoServer 
{   
    ServerSocket m_ServerSocket; 


    public EchoServer() 
    { 
     try 
     { 
      // Create the server socket. 
      m_ServerSocket = new ServerSocket(12111); 
     } 
     catch(IOException ioe) 
     { 
      System.out.println("Could not create server socket at 12111. Quitting."); 
      System.exit(-1); 
     } 

     System.out.println("Listening for clients on 12111..."); 

     // Successfully created Server Socket. Now wait for connections. 
     int id = 0; 
     while(true) 
     {       
      try 
      { 
       // Accept incoming connections. 
       Socket clientSocket = m_ServerSocket.accept(); 

       // accept() will block until a client connects to the server. 
       // If execution reaches this point, then it means that a client 
       // socket has been accepted. 

       // For each client, we will start a service thread to 
       // service the client requests. This is to demonstrate a 
       // multithreaded server, although not required for such a 
       // trivial application. Starting a thread also lets our 
       // EchoServer accept multiple connections simultaneously. 

       // Start a service thread 

       ClientServiceThread cliThread = new ClientServiceThread(clientSocket, id++); 
       cliThread.start(); 
      } 
      catch(IOException ioe) 
      { 
       System.out.println("Exception encountered on accept. Ignoring. Stack Trace :"); 
       ioe.printStackTrace(); 
      } 
     } 
    } 

    public static void main (String[] args) 
    { 
     new EchoServer();  
    } 


    class ClientServiceThread extends Thread 
    { 
     Socket m_clientSocket;   
     int m_clientID = -1; 
     boolean m_bRunThread = true; 

     ClientServiceThread(Socket s, int clientID) 
     { 
      m_clientSocket = s; 
      m_clientID = clientID; 
     } 

     public void run() 
     {    
      // Obtain the input stream and the output stream for the socket 
      // A good practice is to encapsulate them with a BufferedReader 
      // and a PrintWriter as shown below. 
      BufferedReader in = null; 
      PrintWriter out = null; 

      // Print out details of this connection 
      System.out.println("Accepted Client : ID - " + m_clientID + " : Address - " + 
          m_clientSocket.getInetAddress().getHostName()); 

      try 
      {         
       in = new BufferedReader(new InputStreamReader(m_clientSocket.getInputStream())); 
       out = new PrintWriter(new OutputStreamWriter(m_clientSocket.getOutputStream())); 

       // At this point, we can read for input and reply with appropriate output. 

       // Run in a loop until m_bRunThread is set to false 
       while(m_bRunThread) 
       {      
        // read incoming stream 
        String clientCommand = in.readLine(); 

        System.out.println("Client Says :" + clientCommand); 


        if(clientCommand.equalsIgnoreCase("quit")) 
        { 
         // Special command. Quit this thread 
         m_bRunThread = false; 
         System.out.print("Stopping client thread for client : " + m_clientID); 
        } 
        else 
        { 
         // Echo it back to the client. 
         out.println(clientCommand); 
         out.flush(); 
        } 
       } 
      } 
      catch(Exception e) 
      { 
       e.printStackTrace(); 
      } 
      finally 
      { 
       // Clean up 
       try 
       {      
        in.close(); 
        out.close(); 
        m_clientSocket.close(); 
        System.out.println("...Stopped"); 
       } 
       catch(IOException ioe) 
       { 
        ioe.printStackTrace(); 
       } 
      } 
     } 
    } 
} 

私は、バンドルを展開しようGlassfishサーバー上でアプリケーションサーバーがハングしますが、Javaクライアントを使用してJavaネットワークサーバーに接続できます。無限ループがあるようです。私はコードを修正するために助けが必要です。

お祈り申し上げます

答えて

2

あなたのバンドル・アクティベーター開始方法あなたが無限ループを使用してサービスのコンストラクタを呼び出しているので、返すことはありません。良い方法は、バンドル活性剤からできるだけ早く戻ることです。

public class EchoServer { 
    private volatile boolean started; 
    public void start() { 

     new Thread(new Runnable() { 
      @Override 
      public void run() { 
       started = true; 

       try { 
        m_ServerSocket = new ServerSocket(12111); 
       } catch(IOException ioe) { 
        System.out.println("Could not create server socket at 12111. Quitting."); 
        System.exit(-1); 
       } 
       System.out.println("Listening for clients on 12111..."); 

       // Successfully created Server Socket. Now wait for connections. 
       int id = 0; 

       while (started) { 
        try { 

         Socket clientSocket = m_ServerSocket.accept(); 
         ClientServiceThread cliThread = new ClientServiceThread(clientSocket, id++); 
         cliThread.start();  
        } catch(IOException ioe) { 
         System.out.println("Exception encountered on accept. Ignoring. Stack Trace :"); 
         ioe.printStackTrace(); 
        } 
       } 
      } 
     }).start(); 
    } 
    public void stop() { 
     started = false; 
    } 
} 

アクティベーター

public class CBridgeApp implements BundleActivator { 
    private EchoServer method; 
    public void start(BundleContext bc) throws Exception { 
     ... 

     method = new EchoServer(); 
     method.start(); 
    } 
    public void stop(BundleContext bc) throws Exception { 
     ... 

     method.stop(); 
    } 
} 
+0

非常に良い答え:ここで

はあなたのコードを書き換える方法を考えです。私はコードの一部を理解していません。不足している(.....)部品を追加しますか?ありがとうございました。 –

+1

バンドルアクティベータでは、_CBridge_のサービス参照を取得してそこから削除する必要がある理由がわからないため、不足している部分を追加する必要があるかどうかを判断することをお勧めします。サービスは更新されましたが、コンパイルできるかどうかはチェックしませんでした。 – szhem

+0

良いニュースはコードをコンパイルでき、Glassfishでコードを正常にアップロードできることです。 Javaネットワーククライアントは、サーバーと正常に通信できます。悪いニュース - バンドルをアンデプロイしてもう一度デプロイすると、Glassfishサーバーがクラッシュします。これはエラーログです:http://pastebin.com/S76AGMzfソケットを作成できません。古いソケット接続がきれいになっていない可能性はありますか? –

関連する問題