I have socket connection which keep reading data and then it will send it via a queue for next processing in another thread. I notice at times it just stop sending data to the queue. I will print this System.out.println("\n\nSending TO QUEUE : "+message); and stop but I do not see any error being capture any method to capture the errors? What could also be the possible error here?
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);
}
}
}
}
Database processing queue thread snippet code.
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.
}
}
}
}