• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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);
}
}
 
Ranch Hand
Posts: 39
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic