shutdown Thread pool and sockets
Bonjour,
Voilà mon souci, c'est que j'ai besoin de faire un shutdown propre de tout ce qui a été ouvert. je m'explique :
J'ai une appli server (socketServer : dbServer), à chaque nouvelle connexion d'un client je crée un Thread (via le thread pool) qui va traiter les requête de ce client, etc.
A un moment donné un threadclient recoit un requêt 'shutdown' donc, je dois arreter toute les socketService déjà acceptée/ouvertes, arreter tout les threads clients déjà ouverts, et enfin arreter la socketServer (dbServer) en sortant du while...voir code :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
|
//Code dbServer:
try
{
//Create a new thread pool
ExecutorService threadPool = Executors.newCachedThreadPool();
while(!listening)
{
//Wait for a client connection
Socket socketService=null;
socketService= serverSocket.accept();
// démarer le thread client qui va traiter les requêtes
threadPool.execute(new ClientDBThread(socketService,threadPool));
}
}
catch (IOException eIO)
{
System.err.println("DbServer issue : "+eIO.getMessage());
System.exit(-1);
} |
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
|
//thread client :
ClientDBThread(Socket socket,ExecutorService threadPool)
{
//Allocate a new thread with name "ClientConnection"
super("ClientConnection");
this.socketClient = socket;
this.threadPool=threadPool;
}
@Override
public void run()
{
while(listening)
{
System.out.println("start shutdown......");
//close all client socket
????socket.close() //but this only for the current socket not for all the others :(
//Close all the client threads
threadPool.shutdown();
//Return to dbServer and exit the wile??
???
} |
Je ne sais pas si c'est clair mais je suis vraiment bloqué :((((
Merci d'avance.......