File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Threads and Synchronization and the fly likes Socket Server only processing one request, then it just queues them up Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Socket Server only processing one request, then it just queues them up" Watch "Socket Server only processing one request, then it just queues them up" New topic
Author

Socket Server only processing one request, then it just queues them up

Charles Burton
Ranch Hand

Joined: Jan 31, 2011
Posts: 39

First, I have to code to java 1.4 as that is the version available on our systems (Can't upgrade). So I created an application that monitors several things at once on our systems and am in the processing of creating a server that will listen for socket connections from this application to post that information in a JTextArea(Using System.out.println(); for testing). I used most of the code available here (I'll repost it here for clarity) InFormit.

The following is the code I used to create the server.


The request queue


The thread requester



and finally the interface




Now I start it all from main and my package is name popup2

ALL THE CODE AFTER THIS POINT IS HAND JAMMED. I have no way of moving it over via copy/paste



There is more going on in main, but all it does is start various threads based on information that it reads from a config. These threads are what reports to the server, I'll give an example of one. The next part is where I implement RequestHandler


Here is my output flusher. It's a class that I created to send a string from one of my other classes to the output socket.



And finally here is an example of a class that is sending information to the FlushOutput class, multiple threads of these will be opened and connected to the abstract server on an as needed basis. The parser is actually about 250 lines of code, but I'll just post the relevant part here. The final product of most of the code is irrelevant other than it's a String.




So what happens when I run this is everything compiles and runs as expected with one little problem. It prints the first thread to call FlushOuput to the Request handler then nothing. I get messages to my terminal that look like this:

Recieved a new connection from "hostname": hostname
Found an available thread!
Shutting down....
Shutting down the request threads...
[0]: Attempting to kill thread
[1]: Attempting to kill thread

When it should look like this and it does for the first instance after that it just repeats the previous lines over and over.

Recieved a new connection from "hostname": hostname
Found an available thread!
Shutting down....
Shutting down the request threads...
[0]: Attempting to kill thread
[1]: Processing Request...
[1]: Attempting to kill thread
[0]: Thread shutting down...
[In the READER]
[1]: Finished Processing Request
[1]: Thread shutting down




Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19670
    
  18

Wow. Excellent start post.

But I think the networking part is irrelevant, and the main issue is thread-related. That's why I'm going to move this thread to our Threads and Synchronization forum.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Charles Burton
Ranch Hand

Joined: Jan 31, 2011
Posts: 39

Thanks, I was hoping that I got it right =). This problem as had me banging my head against the wall for a couple of days now.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4174
    
  21

From the output, it looks like "Shutting down..." gets printed, and that the request queue gets shutdown. This would kill the working threads and prevent requests from being processed. As far as I can see, the only way that you can get to the Shutting down... output (and queue#shutdown()) is to have the running flag in the server set to false, which in turn looks like only happens when the stopServer() method is called. So it looks like something is calling stopServer(), perhaps before you want it to. None of the code you show actually calls that method. So how does the server get shut down?


Steve
Charles Burton
Ranch Hand

Joined: Jan 31, 2011
Posts: 39

Thanks! I commented out the line this.requestQueue.shutdown(); at the end of the AbstractServer class. It now behaves as expected, however I've been working on it for a little too long today and can't tell if that's going to create issues with the queue filling up over time.
Chris Hurst
Ranch Hand

Joined: Oct 26, 2003
Posts: 416
    
    2

Shouldn't running be volatile or start / stop synchronized (just quickly scanned the code by eye) for "happens before ordering" , inter thread visibility of running.


"Eagles may soar but weasels don't get sucked into jet engines" SCJP 1.6, SCWCD 1.4, SCJD 1.5,SCBCD 5
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Socket Server only processing one request, then it just queues them up