我有套接字连接,它继续读取数据,然后它将通过队列发送它,以便在另一个线程中进行下一步处理。我有时会注意到它只是停止向队列发送数据。我将打印此System.out.println(“\ n \ n发送到QUEUE:”+消息);并停止但我没有看到任何错误捕获任何方法来捕获错误?这可能是什么错误?
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 :(得分: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();
}
}
}
}
}