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.
                     }
                 }
       }
}

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.
                     }
                 }
       }
}

Uhm I dont know but this looks wrong:

ex.printStackTrace(System.out);

should it not be:

ex.printStackTrace();

Edited 4 Years Ago by DavidKroukamp: n/a

Dear Cor,
So beside that what is causing my things to stop any idea?

Dear Cor,
So beside that what is causing my things to stop any idea?

I would rather not suggest anything on code i cant run, but if no errors are being thrown during compilation or runtime then there is a logical error... Use some printlns to see which methods are being called and what each variable you need is at each stage, sure that will find the problem...

Dear Cor,
That is what I am doing it just stop after this System.out.println("\n\nSending TO QUEUE : "+message); so meaning that calling this databaseQueue.add(message); having problem. But I do not know what problem it is having for calling this is the thread is dead is that possible? How to check if that thread is dead?

Dear Cor,
That is what I am doing it just stop after this System.out.println("\n\nSending TO QUEUE : "+message); so meaning that calling this databaseQueue.add(message); having problem. But I do not know what problem it is having for calling this is the thread is dead is that possible? How to check if that thread is dead?

if(ThreadName.isAlive()) {
//thread is alive still
}

There's no obvious reason why adding a non-null element to a LinkedBlockingQueue would ever fail (except maybe out of memory?). Fix your printStackTrace so we can see if there's an exception.

Dear Cor,
This how I called the thread new Thread(new DatabaseProcessor()).start(); . Is it possible after some heavy processing it dies out? I tried like this

if(DatabaseProcessor.isAlive()) {

It gives me this error

commServer.java:94: cannot find symbol
symbol : method isAlive()

Edited 3 Years Ago by Dani: Formatting fixed

Dear James,
The reason I keep it as ex.printStackTrace(System.out); is because I am running this java via a java wrapper so what ever error will be printed automatically into the wrapper log file. I do get other error in the wrapper log file like connection reset but at the point where it stop to send into the queue there is no error raise that confuses me altogether. The think is that the socket keep running but only it nothing is happening in the database processor part.

Dear Cor,
This how I called the thread new Thread(new DatabaseProcessor()).start(); . Is it possible after some heavy processing it dies out? I tried like this

if(DatabaseProcessor.isAlive()) {

It gives me this error


commServer.java:94: cannot find symbol
symbol : method isAlive()

you must declare a new instance for thread so you can use the instance method isAlive:

Thread t=new Thread(new DatabaseProcessor());
                if(t.isAlive()) {
                    
                }

Edited 4 Years Ago by DavidKroukamp: n/a

If you want to check a thread then you call isAlive on the Thread, not on its Runnable, ie

Thread t = new Thread(new ConnectionHandler()).start();
if(t.isAlive()) { ...

but nothing that happens in the database handler should stop the connection handler from enqueueing requests.


Did you change anything else (eg start multiple database threads) that could affect this? It may be a good idea to give the ConnectionHandler thread a higher priority than the actual database processing.

Dear James,
Ok this how I did now. It can print the message whether the thread is alive so I let it run will check when it stops then see whether it prints that message or not. I stop the multiple threads ready. How to give the higher priority?

public class commServer {
   private LinkedBlockingQueue<String> databaseQueue = new LinkedBlockingQueue<String>();
   private LinkedBlockingQueue<String> eMailQueue = new LinkedBlockingQueue<String>();

   Thread t1;

   class ConnectionHandler implements Runnable {

    private Socket receivedSocketConn1;
    ConnectionHandler(Socket receivedSocketConn1) {
      this.receivedSocketConn1=receivedSocketConn1;
    }
     
     
      // gets data from an inbound connection and queues it for databse update

      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;  
		           	   //n = n + (char) m;
		           	  //  n = new StringBuffer().append((char)m).toString();
		         	  int i = message.indexOf("GET");
								if(i != -1) { 
									break;
								}
					  
		         	  if (nextChar == '#')
		         	  {
			             System.out.println("\n\nSending PA : "+message);
			             w.write("$PA\r\n");
		                 w.flush(); 
		                 System.out.println("\n\nSending TO QUEUE : "+message);
		                 if(t1.isAlive()) {
		                 	System.out.println("\n\n DatabaseProcessor.isAlive : "+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 ");
		     
		         System.out.println("\n\nDevice Closed The Connection Properly");
		      } 
		      catch (Exception ex)  
		      { 
		           System.out.println( "\n\n\n THE device had exception problem"+" "+ receivedSocketConn1.getInetAddress() +":"+receivedSocketConn1.getPort()+" IS CONNECTED ");
		     
		           System.out.println("MyError:Exception has been caught in in the main first try");
		           ex.printStackTrace(System.out);
		      }      
		      finally
		      {
		        try 
		       	{
		            System.out.println( "\n\n\n THE device is in finally"+" "+ receivedSocketConn1.getInetAddress() +":"+receivedSocketConn1.getPort()+" IS CONNECTED ");
		     
		            if ( w != null ) 
			        {
			          	w.close();
			        }
			        else 
			        {
			        	System.out.println("MyError:w is null in finally close");
			        }
		        }
		        catch(IOException ex){
		           System.out.println("MyError:IOException has been caught in w in finally close");
		           ex.printStackTrace(System.out);
		        }
		        
		      }

      }

   }

 

   class DatabaseProcessor implements Runnable {

   }

   public static void main(String[] args) {

      // It's bad O.O. practice to put all your code in static main method.

      // Create a new instance of the class, and put the code there

      new commServer();

   }

 

   commServer() { // default constructor

      //t1 = new Thread(new DatabaseProcessor()).start();
      t1 = new Thread(new DatabaseProcessor());
      t1.start();

      new Thread(new MailProcessor()).start();

      try 
      {
			   final ServerSocket serverSocketConn = new ServerSocket(9000);				
			   while (true) 
					{
						try 
						{
					            Socket socketConn1 = serverSocketConn.accept();
                                new Thread(new ConnectionHandler(socketConn1)).start();			            
						}
						catch(Exception e)
						{
							System.out.println("MyError:Socket Accepting has been caught in main loop."+e.toString());
						    e.printStackTrace(System.out);
						}
					}
      } 
      catch (Exception e) 
      {
         System.out.println("MyError:Socket Conn has been caught in main loop."+e.toString());
         e.printStackTrace(System.out);
         //System.exit(0); 
      }
      databaseQueue.add(null);

      eMailQueue.add(null);

   }

How to give the higher priority?

I presume you have already looked at the API documentation for Thread? Look again, you will find a suitable method.

Dear James,
Ok this how I did now. It can print the message whether the thread is alive so I let it run will check when it stops then see whether it prints that message or not. I stop the multiple threads ready. How to give the higher priority?

public class commServer {
   private LinkedBlockingQueue<String> databaseQueue = new LinkedBlockingQueue<String>();
   private LinkedBlockingQueue<String> eMailQueue = new LinkedBlockingQueue<String>();

   Thread t1;

   class ConnectionHandler implements Runnable {

    private Socket receivedSocketConn1;
    ConnectionHandler(Socket receivedSocketConn1) {
      this.receivedSocketConn1=receivedSocketConn1;
    }
     
     
      // gets data from an inbound connection and queues it for databse update

      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;  
		           	   //n = n + (char) m;
		           	  //  n = new StringBuffer().append((char)m).toString();
		         	  int i = message.indexOf("GET");
								if(i != -1) { 
									break;
								}
					  
		         	  if (nextChar == '#')
		         	  {
			             System.out.println("\n\nSending PA : "+message);
			             w.write("$PA\r\n");
		                 w.flush(); 
		                 System.out.println("\n\nSending TO QUEUE : "+message);
		                 if(t1.isAlive()) {
		                 	System.out.println("\n\n DatabaseProcessor.isAlive : "+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 ");
		     
		         System.out.println("\n\nDevice Closed The Connection Properly");
		      } 
		      catch (Exception ex)  
		      { 
		           System.out.println( "\n\n\n THE device had exception problem"+" "+ receivedSocketConn1.getInetAddress() +":"+receivedSocketConn1.getPort()+" IS CONNECTED ");
		     
		           System.out.println("MyError:Exception has been caught in in the main first try");
		           ex.printStackTrace(System.out);
		      }      
		      finally
		      {
		        try 
		       	{
		            System.out.println( "\n\n\n THE device is in finally"+" "+ receivedSocketConn1.getInetAddress() +":"+receivedSocketConn1.getPort()+" IS CONNECTED ");
		     
		            if ( w != null ) 
			        {
			          	w.close();
			        }
			        else 
			        {
			        	System.out.println("MyError:w is null in finally close");
			        }
		        }
		        catch(IOException ex){
		           System.out.println("MyError:IOException has been caught in w in finally close");
		           ex.printStackTrace(System.out);
		        }
		        
		      }

      }

   }

 

   class DatabaseProcessor implements Runnable {

   }

   public static void main(String[] args) {

      // It's bad O.O. practice to put all your code in static main method.

      // Create a new instance of the class, and put the code there

      new commServer();

   }

 

   commServer() { // default constructor

      //t1 = new Thread(new DatabaseProcessor()).start();
      t1 = new Thread(new DatabaseProcessor());
      t1.start();

      new Thread(new MailProcessor()).start();

      try 
      {
			   final ServerSocket serverSocketConn = new ServerSocket(9000);				
			   while (true) 
					{
						try 
						{
					            Socket socketConn1 = serverSocketConn.accept();
                                new Thread(new ConnectionHandler(socketConn1)).start();			            
						}
						catch(Exception e)
						{
							System.out.println("MyError:Socket Accepting has been caught in main loop."+e.toString());
						    e.printStackTrace(System.out);
						}
					}
      } 
      catch (Exception e) 
      {
         System.out.println("MyError:Socket Conn has been caught in main loop."+e.toString());
         e.printStackTrace(System.out);
         //System.exit(0); 
      }
      databaseQueue.add(null);

      eMailQueue.add(null);

   }

check here:http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#setPriority(int) and here:http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#MAX_PRIORITY

Dear James,
Ok I transform this //new Thread(new ConnectionHandler(socketConn1)).start();
into this

c1 = new Thread(new ConnectionHandler(socketConn1));
c1.setPriority(10);

and where at right top I have declare Thread c1. So based on your experience what could be the cause. What I notice is that before it fail to write all the previous messages are processed well without any significant error either.

Dear James,
Is that 10 value is ok or must I set it higher? No I wont know any difference till it stop. The stopping is very random at times after 1 hour sometimes 4 or more. So I really need to counter it.

10 is OK, but it's better practice to use the values provided by the Thread class - eg Thread.MAX_PRIORITY
So let's see how that goes...

Dear James,
Ok I changed it ready. So buy giving it a higher priority how will this help to over the problem?

The only reason I can think of for your problem is that the other threads are consuming all the CPU time causing the connection handler to grind to halt. By giving it higher priority that won't happen. It's just a blind guess however.

Dear James,
But the problem now connection handler works perfectly fine it can take data and only when come to enqueue process is having problem. The connection handler is never halted is working non stop as usual only one is not working now is the databaseprocessor thread.

I'm confused. You said "I notice at times it just stop sending data to the queue". That's nothing to do with the database thread. It's only the connection handler that sends data to the queue.

Dear James,
Sorry if I confused you. Ok based on the connection handler snippet codes below. It keep connecting with the devices grab their data and then do this all this as usual so right after this codes is the databaseQueue.add(message); and it should be System.out.println("\n\nSent TO QUEUE : "+message); and this is never being printed out. So based on that I judge that something is wrong with databaseQueue.add(message)?

Process Codes

System.out.println("\n\nSending PA : "+message);
			             w.write("$PA\r\n");
		                 w.flush(); 
		                 System.out.println("\n\nSending TO QUEUE : "+message);

Yes, if it prints the "sending" but not the "sent" then something is going wrong in between those two calls. But I can't guess what because it's just an add, that's a thread-safe method, and you don't have any exceptions thrown. So the only idea I have is that your thread is getting squeezed out, hence the priority suggestion.
Maybe you can instrument the database thread a bit more and see if that is hanging or throwing any exceptions, because even if you can't add to the queue the database thread should continue running until the queue is empty

Dear James,
Yes I also do not know how to diagnose this if the whole thing fails is fine.So which thread is being squeezed here the databaseprocessor right not the connection handler right. But if give connectionhandler more priority how will that help? What do you mean by instrument the database thread a bit more ?

I just guessed that the database thread could be taking all the CPU time thus squeezing out the connection handler and slowing it right down. I suspect it's not that, but I can't see any other reason for an add to hang.If the connection handler has higher priority that can't happen. By "instrument" I mean adding print statements to trace the execution and variables so as top reveal any problems.

Dear James,
Yes if you see I add as many print statement in even the databaseprocessor but it never cross to there stopped right way at the adding the queue. So there is a bug revolving here right well lets see if the priority gives any solution to it. So far you have been using queue for many other application right did you have anything similar?

I use them all the time - decoupling a producer and a consumer like that is a very basic very common design pattern. I've never had a problem with them. You could google to see if anyone else has had problems...

Dear James,
The best part I do not know how to describe my problem via a key word unless I put the whole scenario then only it will be understand. Like you know it well cause you are guru in these. Possible also due to lack of resource could a be a problem too cause my whole of database processing part is very long and a lot of tasks involve in it. But at least some error message could give some clue right.

Dear James,
I got some updates after putting the check on thread alive what happens is that after the failure it shows still for few transaction isAlive there after it shows nothing meaning is dead and today is shows the next message that is the System.out.println("\n\nSent TO QUEUE : "+message);. Any idea what is going is it my guess is right that the thread is dead?

This article has been dead for over six months. Start a new discussion instead.