number of threads is increasing after each close-connection

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

number of threads is increasing after each close-connection

Taktak, Amine

Dear Sir or Madam,

I am working on a project, where I have developed an agent, which can connect clients to a cloud-platform. The clients will be controlled from the cloud-Platform. This agent has two sides: a client-side and a server-side.  The server-side is based on grizzly-framework and the connection is based on webSocket-Protocol. If a client connects to my agent, then the server-side will get the request und then starts a thread which connects to the cloud-Platform. The thread contains my business logic und has the same ID  (e.g. 00000002) as the client. It Means each client is associated with  a thread.  Now I have notified the following problem. I am always observing the number of threads working in my agent.  So I restarted the client from the cloud platform,  my client get the command that it was sent to him and it closes the connection by  sending my agent a close-frame (webSocket-Protocol).  When the server-side (grizzly-server) receives this frame, the onClose()-Method will be called, so that  the grizzly-server closes the connection. Here is my onClose-Method:

 

       /*

       * (non-Javadoc)

       * @see org.glassfish.grizzly.websockets.SimpleWebSocket#onClose(org.glassfish.grizzly.websockets.DataFrame)

       */

       @Override

           public void onClose(DataFrame frame) {

              System.out.println("ONCLOSE was called");

               super.onClose(frame);

           }

 

 

What I am assuming is that the grizzly server will shutdown all threads  responsible for that connection, since it  terminated. Unfortunately these threads are kept alive. The thread that contains my business logic will terminate after the onClose()-Method is called.

Since my client  executes a restart-Operation, it opened a new connection (after closing it) and I  get new threads ( 11 new Threads) which are managing the new connection. So if I restart my client 10 times, I will get 110 threads for the same client. Here is what I am getting on my console:

First connection from the client to grizzly-server:

 

grizzly-nio-kernel(1) SelectorRunner

grizzly-nio-kernel(2) SelectorRunner

grizzly-nio-kernel(3) SelectorRunner

grizzly-nio-kernel(4) SelectorRunner

grizzly-nio-kernel(5) SelectorRunner

grizzly-nio-kernel(6) SelectorRunner

grizzly-nio-kernel(7) SelectorRunner

grizzly-nio-kernel(8) SelectorRunner

grizzly-nio-kernel(9) SelectorRunner

Grizzly-worker(1)

Grizzly-worker(2)

Grizzly-worker(3)

Grizzly-worker(4)

Grizzly-worker(5)

Grizzly-worker(6)

Grizzly-worker(7)

Grizzly-worker(8)

Grizzly-worker(9)

Grizzly-worker(10)

Grizzly-worker(11)

Grizzly-worker(12)

Grizzly-worker(13)

Grizzly-worker(14)

Grizzly-worker(15)

Grizzly-worker(16)

grizzly-nio-kernel(1) SelectorRunner

grizzly-nio-kernel(2) SelectorRunner

grizzly-nio-kernel(3) SelectorRunner

grizzly-nio-kernel(4) SelectorRunner

grizzly-nio-kernel(5) SelectorRunner

grizzly-nio-kernel(6) SelectorRunner

grizzly-nio-kernel(7) SelectorRunner

grizzly-nio-kernel(8) SelectorRunner

grizzly-nio-kernel(9) SelectorRunner

Grizzly-worker(1)

Grizzly-worker(2)

Grizzly-worker(3)

Grizzly-worker(4)

Grizzly-worker(5)

Grizzly-worker(6)

Grizzly-worker(7)

Grizzly-worker(8)

Grizzly-worker(9)

Grizzly-worker(10)

Grizzly-worker(11)

Grizzly-worker(12)

Grizzly-worker(13)

Grizzly-worker(14)

Grizzly-worker(15)

Grizzly-worker(16)

Grizzly-HttpSession-Expirer

00000002

number of threads running on current server: 54

 

restart operation on my client to grizzly-server:

 

HttpServer-0

grizzly-nio-kernel(1) SelectorRunner

grizzly-nio-kernel(2) SelectorRunner

grizzly-nio-kernel(3) SelectorRunner

grizzly-nio-kernel(4) SelectorRunner

grizzly-nio-kernel(5) SelectorRunner

grizzly-nio-kernel(6) SelectorRunner

grizzly-nio-kernel(7) SelectorRunner

grizzly-nio-kernel(8) SelectorRunner

grizzly-nio-kernel(9) SelectorRunner

Grizzly-worker(1)

Grizzly-worker(2)

Grizzly-worker(3)

Grizzly-worker(4)

Grizzly-worker(5)

Grizzly-worker(6)

Grizzly-worker(7)

Grizzly-worker(8)

Grizzly-worker(9)

Grizzly-worker(10)

Grizzly-worker(11)

Grizzly-worker(12)

Grizzly-worker(13)

Grizzly-worker(14)

Grizzly-worker(15)

Grizzly-worker(16)

grizzly-nio-kernel(1) SelectorRunner

grizzly-nio-kernel(2) SelectorRunner

grizzly-nio-kernel(3) SelectorRunner

grizzly-nio-kernel(4) SelectorRunner

grizzly-nio-kernel(5) SelectorRunner

grizzly-nio-kernel(6) SelectorRunner

grizzly-nio-kernel(7) SelectorRunner

grizzly-nio-kernel(8) SelectorRunner

grizzly-nio-kernel(9) SelectorRunner

Grizzly-worker(1)

Grizzly-worker(2)

Grizzly-worker(3)

Grizzly-worker(4)

Grizzly-worker(5)

Grizzly-worker(6)

Grizzly-worker(7)

Grizzly-worker(8)

Grizzly-worker(9)

Grizzly-worker(10)

Grizzly-worker(11)

Grizzly-worker(12)

Grizzly-worker(13)

Grizzly-worker(14)

Grizzly-worker(15)

Grizzly-worker(16)

Grizzly-HttpSession-Expirer

pool-2-thread-1

MultiThreadedHttpConnectionManager cleanup

HttpClient-68 Selector0

HttpClient-69

HttpClient-70

HttpClient-71

HttpClient-72

HttpClient-73

HttpClient-74

HttpClient-75

pool-3-thread-1

00000002

number of threads running on current server: 65

 

Is it a normal behavior?

At the beginning, I have thought that the client has not sent a correct close-frame. But I verified this on Wireshark. It is correct. So is there  a Problem?

 

Mit freundlichen Grüßen,
With kind regards,

 

Amine Taktak

Master-Student

 


FEIG ELECTRONIC GmbH
Lange Straße 4
35781 Weilburg

Germany

 

Tel.:   +49 (0)6471/3109-0
Fax.:   +49 (0)6471/3109-99
E-Mail:
[hidden email]

 

-----------------------------------------------------------------------------------
Amtsgericht Limburg HRB 3178
Geschäftsführer: Dipl.-Ing. Wolfgang Feig, Eldor Walk
-----------------------------------------------------------------------------------

 

Wichtiger Hinweis:

Diese E-Mail enthält vertrauliche und nur für den angegebenen Empfänger bestimmte Informationen. Sofern Sie nicht der beabsichtigte Empfänger sind, benachrichtigen Sie bitte umgehend den Absender und löschen diese E-Mail. Das unbefugte Kopieren oder die unbefugte Weitergabe dieser E-Mail bzw. deren Inhalts ist nicht gestattet und kann eine kriminelle Handlung sein.

 

Important notice
This message contains confidential information which is only intended for the addressee. Unless you are the intended recipient, immediately inform the sender and delete this e-mail. It is not permitted neither to copy it nor pass the content on to someone who is not authorized to read it.
Acting against this notice could be considered unlawful.

 

Reply | Threaded
Open this post in threaded view
|

Re: number of threads is increasing after each close-connection

oleksiys
Administrator
Hi Amir,

it would definitely help if you could share your code.
From the first glance it looks like you create Grizzly Transport per client and then, when client closes the connection the transport is not getting shut down.

Thanks.

WBR,
Alexey.


On 12/21/15 7:12 AM, Taktak, Amine wrote:

Dear Sir or Madam,

I am working on a project, where I have developed an agent, which can connect clients to a cloud-platform. The clients will be controlled from the cloud-Platform. This agent has two sides: a client-side and a server-side.  The server-side is based on grizzly-framework and the connection is based on webSocket-Protocol. If a client connects to my agent, then the server-side will get the request und then starts a thread which connects to the cloud-Platform. The thread contains my business logic und has the same ID  (e.g. 00000002) as the client. It Means each client is associated with  a thread.  Now I have notified the following problem. I am always observing the number of threads working in my agent.  So I restarted the client from the cloud platform,  my client get the command that it was sent to him and it closes the connection by  sending my agent a close-frame (webSocket-Protocol).  When the server-side (grizzly-server) receives this frame, the onClose()-Method will be called, so that  the grizzly-server closes the connection. Here is my onClose-Method:

 

       /*

       * (non-Javadoc)

       * @see org.glassfish.grizzly.websockets.SimpleWebSocket#onClose(org.glassfish.grizzly.websockets.DataFrame)

       */

       @Override

           public void onClose(DataFrame frame) {

              System.out.println("ONCLOSE was called");

               super.onClose(frame);

           }

 

 

What I am assuming is that the grizzly server will shutdown all threads  responsible for that connection, since it  terminated. Unfortunately these threads are kept alive. The thread that contains my business logic will terminate after the onClose()-Method is called.

Since my client  executes a restart-Operation, it opened a new connection (after closing it) and I  get new threads ( 11 new Threads) which are managing the new connection. So if I restart my client 10 times, I will get 110 threads for the same client. Here is what I am getting on my console:

First connection from the client to grizzly-server:

 

grizzly-nio-kernel(1) SelectorRunner

grizzly-nio-kernel(2) SelectorRunner

grizzly-nio-kernel(3) SelectorRunner

grizzly-nio-kernel(4) SelectorRunner

grizzly-nio-kernel(5) SelectorRunner

grizzly-nio-kernel(6) SelectorRunner

grizzly-nio-kernel(7) SelectorRunner

grizzly-nio-kernel(8) SelectorRunner

grizzly-nio-kernel(9) SelectorRunner

Grizzly-worker(1)

Grizzly-worker(2)

Grizzly-worker(3)

Grizzly-worker(4)

Grizzly-worker(5)

Grizzly-worker(6)

Grizzly-worker(7)

Grizzly-worker(8)

Grizzly-worker(9)

Grizzly-worker(10)

Grizzly-worker(11)

Grizzly-worker(12)

Grizzly-worker(13)

Grizzly-worker(14)

Grizzly-worker(15)

Grizzly-worker(16)

grizzly-nio-kernel(1) SelectorRunner

grizzly-nio-kernel(2) SelectorRunner

grizzly-nio-kernel(3) SelectorRunner

grizzly-nio-kernel(4) SelectorRunner

grizzly-nio-kernel(5) SelectorRunner

grizzly-nio-kernel(6) SelectorRunner

grizzly-nio-kernel(7) SelectorRunner

grizzly-nio-kernel(8) SelectorRunner

grizzly-nio-kernel(9) SelectorRunner

Grizzly-worker(1)

Grizzly-worker(2)

Grizzly-worker(3)

Grizzly-worker(4)

Grizzly-worker(5)

Grizzly-worker(6)

Grizzly-worker(7)

Grizzly-worker(8)

Grizzly-worker(9)

Grizzly-worker(10)

Grizzly-worker(11)

Grizzly-worker(12)

Grizzly-worker(13)

Grizzly-worker(14)

Grizzly-worker(15)

Grizzly-worker(16)

Grizzly-HttpSession-Expirer

00000002

number of threads running on current server: 54

 

restart operation on my client to grizzly-server:

 

HttpServer-0

grizzly-nio-kernel(1) SelectorRunner

grizzly-nio-kernel(2) SelectorRunner

grizzly-nio-kernel(3) SelectorRunner

grizzly-nio-kernel(4) SelectorRunner

grizzly-nio-kernel(5) SelectorRunner

grizzly-nio-kernel(6) SelectorRunner

grizzly-nio-kernel(7) SelectorRunner

grizzly-nio-kernel(8) SelectorRunner

grizzly-nio-kernel(9) SelectorRunner

Grizzly-worker(1)

Grizzly-worker(2)

Grizzly-worker(3)

Grizzly-worker(4)

Grizzly-worker(5)

Grizzly-worker(6)

Grizzly-worker(7)

Grizzly-worker(8)

Grizzly-worker(9)

Grizzly-worker(10)

Grizzly-worker(11)

Grizzly-worker(12)

Grizzly-worker(13)

Grizzly-worker(14)

Grizzly-worker(15)

Grizzly-worker(16)

grizzly-nio-kernel(1) SelectorRunner

grizzly-nio-kernel(2) SelectorRunner

grizzly-nio-kernel(3) SelectorRunner

grizzly-nio-kernel(4) SelectorRunner

grizzly-nio-kernel(5) SelectorRunner

grizzly-nio-kernel(6) SelectorRunner

grizzly-nio-kernel(7) SelectorRunner

grizzly-nio-kernel(8) SelectorRunner

grizzly-nio-kernel(9) SelectorRunner

Grizzly-worker(1)

Grizzly-worker(2)

Grizzly-worker(3)

Grizzly-worker(4)

Grizzly-worker(5)

Grizzly-worker(6)

Grizzly-worker(7)

Grizzly-worker(8)

Grizzly-worker(9)

Grizzly-worker(10)

Grizzly-worker(11)

Grizzly-worker(12)

Grizzly-worker(13)

Grizzly-worker(14)

Grizzly-worker(15)

Grizzly-worker(16)

Grizzly-HttpSession-Expirer

pool-2-thread-1

MultiThreadedHttpConnectionManager cleanup

HttpClient-68 Selector0

HttpClient-69

HttpClient-70

HttpClient-71

HttpClient-72

HttpClient-73

HttpClient-74

HttpClient-75

pool-3-thread-1

00000002

number of threads running on current server: 65

 

Is it a normal behavior?

At the beginning, I have thought that the client has not sent a correct close-frame. But I verified this on Wireshark. It is correct. So is there  a Problem?

 

Mit freundlichen Grüßen,
With kind regards,

 

Amine Taktak

Master-Student

 


FEIG ELECTRONIC GmbH
Lange Straße 4
35781 Weilburg

Germany

 

Tel.:   +49 (0)6471/3109-0
Fax.:   +49 (0)6471/3109-99
E-Mail:
[hidden email]

 

-----------------------------------------------------------------------------------
Amtsgericht Limburg HRB 3178
Geschäftsführer: Dipl.-Ing. Wolfgang Feig, Eldor Walk
-----------------------------------------------------------------------------------

 

Wichtiger Hinweis:

Diese E-Mail enthält vertrauliche und nur für den angegebenen Empfänger bestimmte Informationen. Sofern Sie nicht der beabsichtigte Empfänger sind, benachrichtigen Sie bitte umgehend den Absender und löschen diese E-Mail. Das unbefugte Kopieren oder die unbefugte Weitergabe dieser E-Mail bzw. deren Inhalts ist nicht gestattet und kann eine kriminelle Handlung sein.

 

Important notice
This message contains confidential information which is only intended for the addressee. Unless you are the intended recipient, immediately inform the sender and delete this e-mail. It is not permitted neither to copy it nor pass the content on to someone who is not authorized to read it.
Acting against this notice could be considered unlawful.

 


Reply | Threaded
Open this post in threaded view
|

AW: number of threads is increasing after each close-connection

Taktak, Amine

HI Oleksiy,

 

Here is some Code:

 

public class Agent {

      

 

       public static void main(String args []){

 

             //run the server-side of the agent

             ServerSideAgent agent = new ServerSideAgent();

 

             try {

                    agent.runServerSideAgent();

            

              } catch (FileNotFoundException e) {

 

                    // TODO Auto-generated catch block

                    e.printStackTrace();

 

             } catch (IOException e) {

                    // TODO Auto-generated catch block

                    e.printStackTrace();

 

             } catch (InterruptedException e) {

 

                    // TODO Auto-generated catch block

                    e.printStackTrace();

             }

       }

}

 

 

public class ServerSideAgent {

      

       static String agent_ip_adresse="0.0.0.0";

       static int port=7777;

      

       public void runServerSideAgent() throws IOException, InterruptedException {

      

             HttpServer server = HttpServer.createSimpleServer();

             final WebSocketAddOn webSocketAddon = new WebSocketAddOn();

             NetworkListener nt=new NetworkListener("agent",agent_ip_adresse,port);

             server.addListener(nt);

 

             for (NetworkListener listener : server.getListeners()) {

                    listener.setChunkingEnabled(false);

                    listener.registerAddOn(webSocketAddon);

             }

            

             final C8yAccessPoint C8y_AP = new  C8yAccessPoint();

//           WebSocketEngine.getEngine().register("/echo", C8y_AP);

             WebSocketEngine.getEngine().register( C8y_AP);

            

             // start the server

             server.start();

             Thread.currentThread().join();   

       }

}

 

public class C8yAccessPoint extends WebSocketApplication {

      

       //the logger

       private static final Logger logger = Logger.getLogger(C8yAccessPoint.class.getName());

      

       /*

       * checks if the incoming request is for the registered application

       * Otherwise the application will be rejected

       *

        * @see org.glassfish.grizzly.websockets.WebSocketApplication#isApplicationRequest(org.glassfish.grizzly.http.HttpRequestPacket)

       */

       @Override

       public boolean isApplicationRequest(HttpRequestPacket request) {

            

             //get the uri

             final String uri = request.getRequestURI().toString();

 

             return uri.endsWith("/echo");

 

       }

      

       /*

       *  creates new WebSocket instance

       * 

        * @see org.glassfish.grizzly.websockets.WebSocketApplication#createSocket(org.glassfish.grizzly.websockets.ProtocolHandler, org.glassfish.grizzly.http.HttpRequestPacket, org.glassfish.grizzly.websockets.WebSocketListener[])

       */

       @Override

       public WebSocket createSocket(ProtocolHandler handler, HttpRequestPacket requestPacket, WebSocketListener ... listeners) {

            

             //instantiate a cumulocity-application

             return new C8yApp(handler, requestPacket, listeners);

            

       }

}

 

 

public class C8yApp extends DefaultWebSocket implements Runnable {

      

       ProtocolHandler handler = null;

       HttpRequestPacket requestPacket = null;

       WebSocketListener listeners[] = null;

 

       private Thread c8yThread;

       private boolean isConnected = false;

      

       // serial number of the device

       String SERIAL;

      

       //Objects for saving informations for an selected operation

       C8yData currentData = null;

       C8yData newData = null;

      

       String threadName = null;

       .

       .

       .

       .

       /*

       * constructor for the class C8yApp

       */

       C8yApp(ProtocolHandler handler, HttpRequestPacket requestPacket, WebSocketListener... listeners) {

             super(handler, requestPacket, listeners);

             c8yThread = new Thread(this);

//           try {

//                  System.setOut(new PrintStream(new BufferedOutputStream(new FileOutputStream("C:\\Users\\amesine.taktak\\Desktop\\console.out")), true));

//           } catch (FileNotFoundException e) {

//                  // TODO Auto-generated catch block

//                  e.printStackTrace();

//           }

 

       }

 

       /*

       * will be executed, when a device connects to the agent

       *

        * @see org.glassfish.grizzly.websockets.DefaultWebSocket#onConnect()

       */

       @Override

       public void onConnect() {

             isConnected = true;

             c8yThread.start();

             super.onConnect();

       }

      

       /*

       * (non-Javadoc)

       * @see org.glassfish.grizzly.websockets.SimpleWebSocket#onClose(org.glassfish.grizzly.websockets.DataFrame)

       */

       @Override

           public void onClose(DataFrame frame) {

                    System.out.println("ONCLOSE was called");

                    super.onClose(frame);

           }

 

       /*

       * will be executed , when the device sends a frame contains a json-parser

       * and the analyser for the websocket-frame

       *

        * @see

       * org.glassfish.grizzly.websockets.DefaultWebSocket#onMessage(java.lang.String)

       */

       @Override

       public void onMessage(String request_str) {

            .

     .

            .             

       }

      

      

 

       /*

       * contains the task that will be run by c8ythread

       *

        *

        * @see java.lang.Runnable#run()

       */

       public void run() {

              //my Thread

}

 

       /*

       * terminating the current thread running the task

       */

       public void stop() {

            

             System.out.println("############################");

             System.out.println("The actual thread terminated");

             System.out.println("############################");

             isConnected = false;

 

}

      

}

 

Hopefully, It can help you to identify the problem.  I tried to terminate the connection from the side of the agent, but it did not help to solve the problem.

 

 

Mit freundlichen Grüßen,
With kind regards,

 

Amine Taktak

Master-Student

 


FEIG ELECTRONIC GmbH
Lange Straße 4
35781 Weilburg

Germany

 

Tel.:   +49 (0)6471/3109-0
Fax.:   +49 (0)6471/3109-99
E-Mail:
[hidden email]

 

-----------------------------------------------------------------------------------
Amtsgericht Limburg HRB 3178
Geschäftsführer: Dipl.-Ing. Wolfgang Feig, Eldor Walk
-----------------------------------------------------------------------------------

 

Wichtiger Hinweis:

Diese E-Mail enthält vertrauliche und nur für den angegebenen Empfänger bestimmte Informationen. Sofern Sie nicht der beabsichtigte Empfänger sind, benachrichtigen Sie bitte umgehend den Absender und löschen diese E-Mail. Das unbefugte Kopieren oder die unbefugte Weitergabe dieser E-Mail bzw. deren Inhalts ist nicht gestattet und kann eine kriminelle Handlung sein.

 

Important notice
This message contains confidential information which is only intended for the addressee. Unless you are the intended recipient, immediately inform the sender and delete this e-mail. It is not permitted neither to copy it nor pass the content on to someone who is not authorized to read it.
Acting against this notice could be considered unlawful.

 

Von: Oleksiy Stashok [mailto:[hidden email]]
Gesendet: Montag, 21. Dezember 2015 19:58
An: [hidden email]
Betreff: Re: number of threads is increasing after each close-connection

 

Hi Amir,

it would definitely help if you could share your code.
From the first glance it looks like you create Grizzly Transport per client and then, when client closes the connection the transport is not getting shut down.

Thanks.

WBR,
Alexey.

On 12/21/15 7:12 AM, Taktak, Amine wrote:

Dear Sir or Madam,

I am working on a project, where I have developed an agent, which can connect clients to a cloud-platform. The clients will be controlled from the cloud-Platform. This agent has two sides: a client-side and a server-side.  The server-side is based on grizzly-framework and the connection is based on webSocket-Protocol. If a client connects to my agent, then the server-side will get the request und then starts a thread which connects to the cloud-Platform. The thread contains my business logic und has the same ID  (e.g. 00000002) as the client. It Means each client is associated with  a thread.  Now I have notified the following problem. I am always observing the number of threads working in my agent.  So I restarted the client from the cloud platform,  my client get the command that it was sent to him and it closes the connection by  sending my agent a close-frame (webSocket-Protocol).  When the server-side (grizzly-server) receives this frame, the onClose()-Method will be called, so that  the grizzly-server closes the connection. Here is my onClose-Method:

 

       /*

       * (non-Javadoc)

       * @see org.glassfish.grizzly.websockets.SimpleWebSocket#onClose(org.glassfish.grizzly.websockets.DataFrame)

       */

       @Override

           public void onClose(DataFrame frame) {

              System.out.println("ONCLOSE was called");

               super.onClose(frame);

           }

 

 

What I am assuming is that the grizzly server will shutdown all threads  responsible for that connection, since it  terminated. Unfortunately these threads are kept alive. The thread that contains my business logic will terminate after the onClose()-Method is called.

Since my client  executes a restart-Operation, it opened a new connection (after closing it) and I  get new threads ( 11 new Threads) which are managing the new connection. So if I restart my client 10 times, I will get 110 threads for the same client. Here is what I am getting on my console:

First connection from the client to grizzly-server:

 

grizzly-nio-kernel(1) SelectorRunner

grizzly-nio-kernel(2) SelectorRunner

grizzly-nio-kernel(3) SelectorRunner

grizzly-nio-kernel(4) SelectorRunner

grizzly-nio-kernel(5) SelectorRunner

grizzly-nio-kernel(6) SelectorRunner

grizzly-nio-kernel(7) SelectorRunner

grizzly-nio-kernel(8) SelectorRunner

grizzly-nio-kernel(9) SelectorRunner

Grizzly-worker(1)

Grizzly-worker(2)

Grizzly-worker(3)

Grizzly-worker(4)

Grizzly-worker(5)

Grizzly-worker(6)

Grizzly-worker(7)

Grizzly-worker(8)

Grizzly-worker(9)

Grizzly-worker(10)

Grizzly-worker(11)

Grizzly-worker(12)

Grizzly-worker(13)

Grizzly-worker(14)

Grizzly-worker(15)

Grizzly-worker(16)

grizzly-nio-kernel(1) SelectorRunner

grizzly-nio-kernel(2) SelectorRunner

grizzly-nio-kernel(3) SelectorRunner

grizzly-nio-kernel(4) SelectorRunner

grizzly-nio-kernel(5) SelectorRunner

grizzly-nio-kernel(6) SelectorRunner

grizzly-nio-kernel(7) SelectorRunner

grizzly-nio-kernel(8) SelectorRunner

grizzly-nio-kernel(9) SelectorRunner

Grizzly-worker(1)

Grizzly-worker(2)

Grizzly-worker(3)

Grizzly-worker(4)

Grizzly-worker(5)

Grizzly-worker(6)

Grizzly-worker(7)

Grizzly-worker(8)

Grizzly-worker(9)

Grizzly-worker(10)

Grizzly-worker(11)

Grizzly-worker(12)

Grizzly-worker(13)

Grizzly-worker(14)

Grizzly-worker(15)

Grizzly-worker(16)

Grizzly-HttpSession-Expirer

00000002

number of threads running on current server: 54

 

restart operation on my client to grizzly-server:

 

HttpServer-0

grizzly-nio-kernel(1) SelectorRunner

grizzly-nio-kernel(2) SelectorRunner

grizzly-nio-kernel(3) SelectorRunner

grizzly-nio-kernel(4) SelectorRunner

grizzly-nio-kernel(5) SelectorRunner

grizzly-nio-kernel(6) SelectorRunner

grizzly-nio-kernel(7) SelectorRunner

grizzly-nio-kernel(8) SelectorRunner

grizzly-nio-kernel(9) SelectorRunner

Grizzly-worker(1)

Grizzly-worker(2)

Grizzly-worker(3)

Grizzly-worker(4)

Grizzly-worker(5)

Grizzly-worker(6)

Grizzly-worker(7)

Grizzly-worker(8)

Grizzly-worker(9)

Grizzly-worker(10)

Grizzly-worker(11)

Grizzly-worker(12)

Grizzly-worker(13)

Grizzly-worker(14)

Grizzly-worker(15)

Grizzly-worker(16)

grizzly-nio-kernel(1) SelectorRunner

grizzly-nio-kernel(2) SelectorRunner

grizzly-nio-kernel(3) SelectorRunner

grizzly-nio-kernel(4) SelectorRunner

grizzly-nio-kernel(5) SelectorRunner

grizzly-nio-kernel(6) SelectorRunner

grizzly-nio-kernel(7) SelectorRunner

grizzly-nio-kernel(8) SelectorRunner

grizzly-nio-kernel(9) SelectorRunner

Grizzly-worker(1)

Grizzly-worker(2)

Grizzly-worker(3)

Grizzly-worker(4)

Grizzly-worker(5)

Grizzly-worker(6)

Grizzly-worker(7)

Grizzly-worker(8)

Grizzly-worker(9)

Grizzly-worker(10)

Grizzly-worker(11)

Grizzly-worker(12)

Grizzly-worker(13)

Grizzly-worker(14)

Grizzly-worker(15)

Grizzly-worker(16)

Grizzly-HttpSession-Expirer

pool-2-thread-1

MultiThreadedHttpConnectionManager cleanup

HttpClient-68 Selector0

HttpClient-69

HttpClient-70

HttpClient-71

HttpClient-72

HttpClient-73

HttpClient-74

HttpClient-75

pool-3-thread-1

00000002

number of threads running on current server: 65

 

Is it a normal behavior?

At the beginning, I have thought that the client has not sent a correct close-frame. But I verified this on Wireshark. It is correct. So is there  a Problem?

 

Mit freundlichen Grüßen,
With kind regards,

 

Amine Taktak

Master-Student

 


FEIG ELECTRONIC GmbH
Lange Straße 4
35781 Weilburg

Germany

 

Tel.:   +49 (0)6471/3109-0
Fax.:   +49 (0)6471/3109-99
E-Mail:
[hidden email]

 

-----------------------------------------------------------------------------------
Amtsgericht Limburg HRB 3178
Geschäftsführer: Dipl.-Ing. Wolfgang Feig, Eldor Walk
-----------------------------------------------------------------------------------

 

Wichtiger Hinweis:

Diese E-Mail enthält vertrauliche und nur für den angegebenen Empfänger bestimmte Informationen. Sofern Sie nicht der beabsichtigte Empfänger sind, benachrichtigen Sie bitte umgehend den Absender und löschen diese E-Mail. Das unbefugte Kopieren oder die unbefugte Weitergabe dieser E-Mail bzw. deren Inhalts ist nicht gestattet und kann eine kriminelle Handlung sein.

 

Important notice
This message contains confidential information which is only intended for the addressee. Unless you are the intended recipient, immediately inform the sender and delete this e-mail. It is not permitted neither to copy it nor pass the content on to someone who is not authorized to read it.
Acting against this notice could be considered unlawful.

 

 

Reply | Threaded
Open this post in threaded view
|

Re: AW: number of threads is increasing after each close-connection

oleksiys
Administrator
Hi,

I suspect the problem is caused by the code inside the CByApp.run(), where you probably create one more Grizzly Transport.
Could you pls. share that code?

In general IMO it's not the best idea to create Thread per Client, it might be better to find a way to avoid that.

Thanks.

WBR,
Alexey.

On 12/21/15 11:48 PM, Taktak, Amine wrote:

HI Oleksiy,

 

Here is some Code:

 

public class Agent {

      

 

       public static void main(String args []){

 

             //run the server-side of the agent

             ServerSideAgent agent = new ServerSideAgent();

 

             try {

                    agent.runServerSideAgent();

            

              } catch (FileNotFoundException e) {

 

                    // TODO Auto-generated catch block

                    e.printStackTrace();

 

             } catch (IOException e) {

                    // TODO Auto-generated catch block

                    e.printStackTrace();

 

             } catch (InterruptedException e) {

 

                    // TODO Auto-generated catch block

                    e.printStackTrace();

             }

       }

}

 

 

public class ServerSideAgent {

      

       static String agent_ip_adresse="0.0.0.0";

       static int port=7777;

      

       public void runServerSideAgent() throws IOException, InterruptedException {

      

             HttpServer server = HttpServer.createSimpleServer();

             final WebSocketAddOn webSocketAddon = new WebSocketAddOn();

             NetworkListener nt=new NetworkListener("agent",agent_ip_adresse,port);

             server.addListener(nt);

 

             for (NetworkListener listener : server.getListeners()) {

                    listener.setChunkingEnabled(false);

                    listener.registerAddOn(webSocketAddon);

             }

            

             final C8yAccessPoint C8y_AP = new  C8yAccessPoint();

//           WebSocketEngine.getEngine().register("/echo", C8y_AP);

             WebSocketEngine.getEngine().register( C8y_AP);

            

             // start the server

             server.start();

             Thread.currentThread().join();   

       }

}

 

public class C8yAccessPoint extends WebSocketApplication {

      

       //the logger

       private static final Logger logger = Logger.getLogger(C8yAccessPoint.class.getName());

      

       /*

       * checks if the incoming request is for the registered application

       * Otherwise the application will be rejected

       *

        * @see org.glassfish.grizzly.websockets.WebSocketApplication#isApplicationRequest(org.glassfish.grizzly.http.HttpRequestPacket)

       */

       @Override

       public boolean isApplicationRequest(HttpRequestPacket request) {

            

             //get the uri

             final String uri = request.getRequestURI().toString();

 

             return uri.endsWith("/echo");

 

       }

      

       /*

       *  creates new WebSocket instance

       * 

        * @see org.glassfish.grizzly.websockets.WebSocketApplication#createSocket(org.glassfish.grizzly.websockets.ProtocolHandler, org.glassfish.grizzly.http.HttpRequestPacket, org.glassfish.grizzly.websockets.WebSocketListener[])

       */

       @Override

       public WebSocket createSocket(ProtocolHandler handler, HttpRequestPacket requestPacket, WebSocketListener ... listeners) {

            

             //instantiate a cumulocity-application

             return new C8yApp(handler, requestPacket, listeners);

            

       }

}

 

 

public class C8yApp extends DefaultWebSocket implements Runnable {

      

       ProtocolHandler handler = null;

       HttpRequestPacket requestPacket = null;

       WebSocketListener listeners[] = null;

 

       private Thread c8yThread;

       private boolean isConnected = false;

      

       // serial number of the device

       String SERIAL;

      

       //Objects for saving informations for an selected operation

       C8yData currentData = null;

       C8yData newData = null;

      

       String threadName = null;

       .

       .

       .

       .

       /*

       * constructor for the class C8yApp

       */

       C8yApp(ProtocolHandler handler, HttpRequestPacket requestPacket, WebSocketListener... listeners) {

             super(handler, requestPacket, listeners);

             c8yThread = new Thread(this);

//           try {

//                  System.setOut(new PrintStream(new BufferedOutputStream(new FileOutputStream("C:\\Users\\amesine.taktak\\Desktop\\console.out")), true));

//           } catch (FileNotFoundException e) {

//                  // TODO Auto-generated catch block

//                  e.printStackTrace();

//           }

 

       }

 

       /*

       * will be executed, when a device connects to the agent

       *

        * @see org.glassfish.grizzly.websockets.DefaultWebSocket#onConnect()

       */

       @Override

       public void onConnect() {

             isConnected = true;

             c8yThread.start();

             super.onConnect();

       }

      

       /*

       * (non-Javadoc)

       * @see org.glassfish.grizzly.websockets.SimpleWebSocket#onClose(org.glassfish.grizzly.websockets.DataFrame)

       */

       @Override

           public void onClose(DataFrame frame) {

                    System.out.println("ONCLOSE was called");

                    super.onClose(frame);

           }

 

       /*

       * will be executed , when the device sends a frame contains a json-parser

       * and the analyser for the websocket-frame

       *

        * @see

       * org.glassfish.grizzly.websockets.DefaultWebSocket#onMessage(java.lang.String)

       */

       @Override

       public void onMessage(String request_str) {

            .

     .

            .             

       }

      

      

 

       /*

       * contains the task that will be run by c8ythread

       *

        *

        * @see java.lang.Runnable#run()

       */

       public void run() {

              //my Thread

}

 

       /*

       * terminating the current thread running the task

       */

       public void stop() {

            

             System.out.println("############################");

             System.out.println("The actual thread terminated");

             System.out.println("############################");

             isConnected = false;

 

}

      

}

 

Hopefully, It can help you to identify the problem.  I tried to terminate the connection from the side of the agent, but it did not help to solve the problem.

 

 

Mit freundlichen Grüßen,
With kind regards,

 

Amine Taktak

Master-Student

 


FEIG ELECTRONIC GmbH
Lange Straße 4
35781 Weilburg

Germany

 

Tel.:   +49 (0)6471/3109-0
Fax.:   +49 (0)6471/3109-99
E-Mail:
[hidden email]

 

-----------------------------------------------------------------------------------
Amtsgericht Limburg HRB 3178
Geschäftsführer: Dipl.-Ing. Wolfgang Feig, Eldor Walk
-----------------------------------------------------------------------------------

 

Wichtiger Hinweis:

Diese E-Mail enthält vertrauliche und nur für den angegebenen Empfänger bestimmte Informationen. Sofern Sie nicht der beabsichtigte Empfänger sind, benachrichtigen Sie bitte umgehend den Absender und löschen diese E-Mail. Das unbefugte Kopieren oder die unbefugte Weitergabe dieser E-Mail bzw. deren Inhalts ist nicht gestattet und kann eine kriminelle Handlung sein.

 

Important notice
This message contains confidential information which is only intended for the addressee. Unless you are the intended recipient, immediately inform the sender and delete this e-mail. It is not permitted neither to copy it nor pass the content on to someone who is not authorized to read it.
Acting against this notice could be considered unlawful.

 

Von: Oleksiy Stashok [[hidden email]]
Gesendet: Montag, 21. Dezember 2015 19:58
An: [hidden email]
Betreff: Re: number of threads is increasing after each close-connection

 

Hi Amir,

it would definitely help if you could share your code.
From the first glance it looks like you create Grizzly Transport per client and then, when client closes the connection the transport is not getting shut down.

Thanks.

WBR,
Alexey.

On 12/21/15 7:12 AM, Taktak, Amine wrote:

Dear Sir or Madam,

I am working on a project, where I have developed an agent, which can connect clients to a cloud-platform. The clients will be controlled from the cloud-Platform. This agent has two sides: a client-side and a server-side.  The server-side is based on grizzly-framework and the connection is based on webSocket-Protocol. If a client connects to my agent, then the server-side will get the request und then starts a thread which connects to the cloud-Platform. The thread contains my business logic und has the same ID  (e.g. 00000002) as the client. It Means each client is associated with  a thread.  Now I have notified the following problem. I am always observing the number of threads working in my agent.  So I restarted the client from the cloud platform,  my client get the command that it was sent to him and it closes the connection by  sending my agent a close-frame (webSocket-Protocol).  When the server-side (grizzly-server) receives this frame, the onClose()-Method will be called, so that  the grizzly-server closes the connection. Here is my onClose-Method:

 

       /*

       * (non-Javadoc)

       * @see org.glassfish.grizzly.websockets.SimpleWebSocket#onClose(org.glassfish.grizzly.websockets.DataFrame)

       */

       @Override

           public void onClose(DataFrame frame) {

              System.out.println("ONCLOSE was called");

               super.onClose(frame);

           }

 

 

What I am assuming is that the grizzly server will shutdown all threads  responsible for that connection, since it  terminated. Unfortunately these threads are kept alive. The thread that contains my business logic will terminate after the onClose()-Method is called.

Since my client  executes a restart-Operation, it opened a new connection (after closing it) and I  get new threads ( 11 new Threads) which are managing the new connection. So if I restart my client 10 times, I will get 110 threads for the same client. Here is what I am getting on my console:

First connection from the client to grizzly-server:

 

grizzly-nio-kernel(1) SelectorRunner

grizzly-nio-kernel(2) SelectorRunner

grizzly-nio-kernel(3) SelectorRunner

grizzly-nio-kernel(4) SelectorRunner

grizzly-nio-kernel(5) SelectorRunner

grizzly-nio-kernel(6) SelectorRunner

grizzly-nio-kernel(7) SelectorRunner

grizzly-nio-kernel(8) SelectorRunner

grizzly-nio-kernel(9) SelectorRunner

Grizzly-worker(1)

Grizzly-worker(2)

Grizzly-worker(3)

Grizzly-worker(4)

Grizzly-worker(5)

Grizzly-worker(6)

Grizzly-worker(7)

Grizzly-worker(8)

Grizzly-worker(9)

Grizzly-worker(10)

Grizzly-worker(11)

Grizzly-worker(12)

Grizzly-worker(13)

Grizzly-worker(14)

Grizzly-worker(15)

Grizzly-worker(16)

grizzly-nio-kernel(1) SelectorRunner

grizzly-nio-kernel(2) SelectorRunner

grizzly-nio-kernel(3) SelectorRunner

grizzly-nio-kernel(4) SelectorRunner

grizzly-nio-kernel(5) SelectorRunner

grizzly-nio-kernel(6) SelectorRunner

grizzly-nio-kernel(7) SelectorRunner

grizzly-nio-kernel(8) SelectorRunner

grizzly-nio-kernel(9) SelectorRunner

Grizzly-worker(1)

Grizzly-worker(2)

Grizzly-worker(3)

Grizzly-worker(4)

Grizzly-worker(5)

Grizzly-worker(6)

Grizzly-worker(7)

Grizzly-worker(8)

Grizzly-worker(9)

Grizzly-worker(10)

Grizzly-worker(11)

Grizzly-worker(12)

Grizzly-worker(13)

Grizzly-worker(14)

Grizzly-worker(15)

Grizzly-worker(16)

Grizzly-HttpSession-Expirer

00000002

number of threads running on current server: 54

 

restart operation on my client to grizzly-server:

 

HttpServer-0

grizzly-nio-kernel(1) SelectorRunner

grizzly-nio-kernel(2) SelectorRunner

grizzly-nio-kernel(3) SelectorRunner

grizzly-nio-kernel(4) SelectorRunner

grizzly-nio-kernel(5) SelectorRunner

grizzly-nio-kernel(6) SelectorRunner

grizzly-nio-kernel(7) SelectorRunner

grizzly-nio-kernel(8) SelectorRunner

grizzly-nio-kernel(9) SelectorRunner

Grizzly-worker(1)

Grizzly-worker(2)

Grizzly-worker(3)

Grizzly-worker(4)

Grizzly-worker(5)

Grizzly-worker(6)

Grizzly-worker(7)

Grizzly-worker(8)

Grizzly-worker(9)

Grizzly-worker(10)

Grizzly-worker(11)

Grizzly-worker(12)

Grizzly-worker(13)

Grizzly-worker(14)

Grizzly-worker(15)

Grizzly-worker(16)

grizzly-nio-kernel(1) SelectorRunner

grizzly-nio-kernel(2) SelectorRunner

grizzly-nio-kernel(3) SelectorRunner

grizzly-nio-kernel(4) SelectorRunner

grizzly-nio-kernel(5) SelectorRunner

grizzly-nio-kernel(6) SelectorRunner

grizzly-nio-kernel(7) SelectorRunner

grizzly-nio-kernel(8) SelectorRunner

grizzly-nio-kernel(9) SelectorRunner

Grizzly-worker(1)

Grizzly-worker(2)

Grizzly-worker(3)

Grizzly-worker(4)

Grizzly-worker(5)

Grizzly-worker(6)

Grizzly-worker(7)

Grizzly-worker(8)

Grizzly-worker(9)

Grizzly-worker(10)

Grizzly-worker(11)

Grizzly-worker(12)

Grizzly-worker(13)

Grizzly-worker(14)

Grizzly-worker(15)

Grizzly-worker(16)

Grizzly-HttpSession-Expirer

pool-2-thread-1

MultiThreadedHttpConnectionManager cleanup

HttpClient-68 Selector0

HttpClient-69

HttpClient-70

HttpClient-71

HttpClient-72

HttpClient-73

HttpClient-74

HttpClient-75

pool-3-thread-1

00000002

number of threads running on current server: 65

 

Is it a normal behavior?

At the beginning, I have thought that the client has not sent a correct close-frame. But I verified this on Wireshark. It is correct. So is there  a Problem?

 

Mit freundlichen Grüßen,
With kind regards,

 

Amine Taktak

Master-Student

 


FEIG ELECTRONIC GmbH
Lange Straße 4
35781 Weilburg

Germany

 

Tel.:   +49 (0)6471/3109-0
Fax.:   +49 (0)6471/3109-99
E-Mail:
[hidden email]

 

-----------------------------------------------------------------------------------
Amtsgericht Limburg HRB 3178
Geschäftsführer: Dipl.-Ing. Wolfgang Feig, Eldor Walk
-----------------------------------------------------------------------------------

 

Wichtiger Hinweis:

Diese E-Mail enthält vertrauliche und nur für den angegebenen Empfänger bestimmte Informationen. Sofern Sie nicht der beabsichtigte Empfänger sind, benachrichtigen Sie bitte umgehend den Absender und löschen diese E-Mail. Das unbefugte Kopieren oder die unbefugte Weitergabe dieser E-Mail bzw. deren Inhalts ist nicht gestattet und kann eine kriminelle Handlung sein.

 

Important notice
This message contains confidential information which is only intended for the addressee. Unless you are the intended recipient, immediately inform the sender and delete this e-mail. It is not permitted neither to copy it nor pass the content on to someone who is not authorized to read it.
Acting against this notice could be considered unlawful.