Reputation: 9205
Server
public void run () {
Socket serversocket = new ServerSocket(port);
while(true) {
new Thread(new ServerThread(serverSocket.accept())).start();
}
}
//serverSocket.close(); etc
ServerThread
public void run() {
BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
String input;
while(true) {
input = in.readLine();
new Thread(new RequestThread(clientSocket, input)).start();
}
}
//close sockets etc in.close() clientSocket.close();
Request Thread
public void run() {
//input was passed from constructor
String output = new SomeProtocol(input);
if(output == null)
break;
//true for auto flush
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
out.println(output);
}//closing out seems to also close the socket, so opted to not to close it, maybe this is the one giving me trouble?
Monitor
public class ServerMonitor implements Runnable{
private ServerThread server;
private LoggingClass log = LoggingClass .getInstance();
private int heartbeat = 0;
public ServerMonitor(ServerThread server) {
this.server = server;
}
public boolean checkFile() {
File file = new File("cmd//in//shutdown.txt");
return file.exists();
}
public void run() {
log.logToFile("Server monitor running");
while (true) {
incHeartbeat();
if (checkFile()) {
log.logToFile("Shutting down server");
break;
}
writeStatus();
this.delay(5000);
}
log.logToFile("Server monitor stopped");
}
public void delay(long delay) {
try {
wait(delay);
} catch (InterruptedException e) {
log.logToFile("Monitor sleep error");
e.printStackTrace();
}
}
public void writeStatus() {
try {
BufferedWriter out = new BufferedWriter(new FileWriter(
"sys//status//status.txt"));
out.write("Start date:" + log.getStartDate());
out.newLine();
out.write("Current date:" + log.getTimestamp("yyyy-MMM-dd k:mm:ss"));
out.newLine();
out.write("Heartbeat:" + getHeartbeat());
out.newLine();
out.write("Cimd in:" + log.getCimdIn());
out.newLine();
out.write("Cimd out:" + log.getCimdOut());
out.newLine();
out.write("Keep alive:" + log.getCimdKeepAlive());
out.newLine();
out.write("HTTP in:" + log.getNumHTTPIn());
out.newLine();
out.write("HTTP out:" + log.getNumHTTPOut());
out.newLine();
out.close();
} catch (Exception e) {
log.logToFile("Write status error");
e.printStackTrace();
}
}
public int getHeartbeat() {
return heartbeat;
}
public synchronized void incHeartbeat() {
heartbeat++;
}
}
This is the rough skeleton of my app. I'm having trouble since sometimes it just stops without any errors. I suspect that it might be because of the sockets but I'm not quite sure so anyone of you guys got any ideas? thanks.
Added my class that monitors the server threads
>How do I know that it doesn't work
Heartbeat doesn't increment anymore
Upvotes: 0
Views: 622
Reputation: 5740
It's tough to tell from the pseudo-code you've posted, but if the application is hanging (and not crashing outright) my best guess is that in.readLine() in ServerThread is blocking. If the application is just shutting down, I'd look at socket closures and maybe check isReady() in your BufferedReader.
A general comment: you may be spawning more threads than you need here. It looks like the reading from the client socket and the writing to it happen serially. If this is the case, the ServerThread Runnable (I'm assuming it's a Runnable since you're passing it to the constructor of a Thread) doesn't necessarily need to spawn a RequestThread.
public class ServerRunnable implements Runnable {
...
public void run() {
BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
String input;
while(true)
{
input = in.readLine();
if(input == null)
{
//this means the underlying socket closed
//log something here or return or whatever
}
String output = new SomeProtocol(input);
if(output == null)
break;
//true for auto flush
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
out.println(output);
}
}
...
}
Upvotes: 0
Reputation: 3991
Could you better define 'stops working'? Does it stop accepting connection; does it crash; does it stop doing something in the middle of processing; etc?
One bit of speculation is that I'm not certain what will happen in the reader when a socket is closed. What I would expect to happen is that trying to call read line with a closed socket would cause an exception, which would halt that thread, and/or possibly kill the entire application. Which would generate a stack trace, and other issues.
Basically, in addition to what Oscar said about exception logging in main; you would want logging in any run() method. A try/catch in main would only catch exceptions thrown by the main thread; not those thrown in other threads.
Upvotes: 0
Reputation: 199215
You may be running out of memory/threads
Do you happen to have some high level exception handling like this:
public static void main( String ... args ) {
try {
// lots of code ...
} catch( Exception e ) {}
}
If so you have to at least log the errors that you're ignoring.
This is just what comes to my mind for I don't have your source code at hand.
Upvotes: 1