2012-02-16 11 views
-1

データの読み取りを継続するソケット接続があり、次に別のスレッドで次の処理のためにキューを介して送信します。私は時々キューにデータを送ることを止めることに気づきます。私はこのSystem.out.printlnを印刷します( "\ n \ nキューに送る:" +メッセージ)。停止しますが、エラーをキャプチャする方法をキャプチャしているエラーは表示されません。ここで起こりうるエラーは何か?キューへのデータの送信が機能しない

class ConnectionHandler implements Runnable { 

    private Socket receivedSocketConn1; 
    ConnectionHandler(Socket receivedSocketConn1) { 
     this.receivedSocketConn1=receivedSocketConn1; 
    } 
    public void run() { 
      BufferedWriter w = null; 
      BufferedReader r = null; 

       String message=""; 
       try { 

       PrintStream out = System.out; 
       BufferedWriter fout = null; 
       w = new BufferedWriter(new OutputStreamWriter(receivedSocketConn1.getOutputStream())); 
       r = new BufferedReader(new InputStreamReader(receivedSocketConn1.getInputStream())); 

       int m = 0, count=0; 
       int nextChar=0; 

       System.out.println("\n\n\n THE device"+" "+ receivedSocketConn1.getInetAddress() +":"+receivedSocketConn1.getPort()+" IS CONNECTED "); 

        while ((nextChar=r.read()) != -1) 
        {     
          message += (char) nextChar; 
          int i = message.indexOf("GET"); 
            if(i != -1) { 
             break; 
            } 

          if (nextChar == '#') 
          { 
          w.flush(); 
           System.out.println("\n\nSending TO QUEUE : "+message); 
           databaseQueue.add(message); 
           System.out.println("\n\nSent TO QUEUE : "+message); 
           message="";    
          } 
        } 
        System.out.println("\n\n\n THE device close connection"+" "+ receivedSocketConn1.getInetAddress() +":"+receivedSocketConn1.getPort()+" IS CONNECTED "); 

        } 
        catch (Exception ex) 
        { 
         ex.printStackTrace(System.out); 
        }  
        finally 
        { 
        try 
        { 

         if (w != null) 
         { 
          w.close(); 
         } 

        } 
        catch(IOException ex){ 
         ex.printStackTrace(System.out); 
        } 

        } 

      } 

     } 

データベース処理キューのスレッドスニペットコード。

class DatabaseProcessor implements Runnable { 




     // updates databaase with data queued by ConnectionHandler 
     Connection dbconn = null; 
     Statement stmt = null; 
     Statement stmt1 = null; 
     Statement stmt2 = null; 
     Date connCreated = null; 
     public void run() 
     { 


     // this is just like the QueueProcessor example I gave you 
     // open database connection 
     createConnection(); 
      while (true) 
      { 

       try 
       { 
        int count=0; 
        String message = ""; 
        message = databaseQueue.take(); 
        System.out.println("\n\nPICKED AT QUEUE : "+message); 
        if (message.equals(null)) { 
         System.out.println("QueueProcessor is shutting down"); 
        break; // exit while loop, ends run() method 
        } 
        //there is more codes but is too long to be put here. 
        } 
       } 
     } 
} 
+0

'databaseQueue'型​​は何ですか?いくつかのコードがありません –

+0

@peter databaseQueueは別のスレッドかもしれません。 – user837306

+0

まだデータ型はありませんか?私はそれがConnectionHandlerとDatabaseProcessorの間の共有インスタンスだと仮定します。 –

答えて

0

は、私は(簡体およびテスト用のものを削除)あなたの例から少しのコードを編集して、私は次の出力を得る:

クライアント(telnetの):

telnet localhost 7777 
Trying ::1... 
Connected to localhost. 
Escape character is '^]'. 
peter 
test 

サーバー:

PICKED AT QUEUE : peter 


PICKED AT QUEUE : test 

コード:

public class Trash { 
    private final static LinkedBlockingQueue<String> databaseQueue = new LinkedBlockingQueue<String>(); 

    public static void main(String[] args) { 

     new Thread(new DatabaseProcessor()).start(); 
     try { 
      ServerSocket serverSocket = new ServerSocket(7777); 
      Socket socket = serverSocket.accept(); 
      new Thread(new ConnectionHandler(socket)).start(); 
      Thread.sleep(10000000); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

    static class ConnectionHandler implements Runnable { 

     private Socket receivedSocketConn1; 

     ConnectionHandler(Socket receivedSocketConn1) { 
      this.receivedSocketConn1 = receivedSocketConn1; 
     } 

     public void run() { 
      try { 
       BufferedReader r = new BufferedReader(new InputStreamReader(receivedSocketConn1.getInputStream())); 
       String message = ""; 
       while (message == null || !message.equals("") || !message.equalsIgnoreCase("quit")) { 
        message = r.readLine(); 
        if (message == null) { 
         continue; 
        } 
        databaseQueue.add(message); 
       } 
      } catch (Exception ex) { 
       ex.printStackTrace(); 
      } 
     } 
    } 

    static class DatabaseProcessor implements Runnable { 
     public void run() { 
      while (true) { 
       try { 
        String message = ""; 
        message = databaseQueue.take(); 
        System.out.println("\n\nPICKED AT QUEUE : " + message); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } 
     } 
    } 
} 
関連する問題