Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Need to stop the thread ,but not using stop()method after the server.accept() method

 
rosan samuel
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Friends,

my problem is a server socket is created and waiting to accept the client connections indefinite time of thread loop. Assume inside the while loop, the a server socket waits for connection. but in my case there is socket connection from client not happened.i need to stop the thread ,when the application server calls resource adapter destroy() method.This method intentionally close the Connection pool connections as well as The running thread and has to close the ServerSocket. but problem is how could we perform the ServerSocket.close and after the accept method. kindly share your thoughts.

public void run() {
acceptConnections();
}

protected void acceptConnections() {
try {
// Bind to a local port
if(server==null)
{
server = new ServerSocket(_port);
}
while(running) {

// Accept the next connection
Socket connection = server.accept();

// Check to see if maximum reached
if (getNumberOfConnections() >= MAX_CONNECTIONS) {
// Kill the connection
PrintStream pout = new PrintStream(
connection.getOutputStream());
pout.println("Too many users");
connection.close();
continue;
} else {
// Launch a new thread
new XXXClient(this, connection).start();
}
}


} catch (InterruptedIOException e) {
logger.log(Level.FINE, "This can be considered a normal exit", e);
} catch (IOException e) {
logger.log(Level.SEVERE, "Unable to accept connections", e);
}
}
 
Charles Burton
Ranch Hand
Posts: 39
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could try making a public method that sets running to false which will break that loop and let the thread exit cleanly. The thread stop() method is rarely a good idea, it's much better to let the thread finish processing. Inside the loop you could also add a conditional test for every iteration, that will allow you cleanly break all connections and close your streams so that you don't run into problems with hung resources.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic