2012-02-09 23 views
0

我創造了這個Java網絡服務器:如何添加Java網絡服務器到OSGi包

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(); 
       } 
      } 
     } 
    } 
} 

我想插入此代碼到OSGi包。目前我還沒有配置Glassfish和數據源來測試這個包。當我將它部署到Glassfish服務器上時,爲了配置Java服務器來監聽輸入連接,我必須做些什麼?

是否需要在OSGI軟件包激活器中添加Sovelhing以使Java服務器偵聽傳入的網絡連接?

祝福

+0

任何想法或回覆? – 2012-02-10 15:34:09

回答

1

它應該是足夠的start()方法只是創建EchoServer的對象,據我看到它就會開始工作。你可以考慮在一個單獨的線程中運行它,以免阻止你的包的活動。

+0

由於OSGi規範要求bundle激活器的start()(和stop())方法儘可能快地返回,因此您應該*在獨立線程中運行服務器。 – jawi 2013-08-03 13:18:07