users@tyrus.java.net

Re: connectToServer leaving background threads when the connection fails

From: Pavel Bucek <pavel.bucek_at_oracle.com>
Date: Wed, 06 Nov 2013 00:01:15 +0100

Hi Kenneth,

Session.close() should close the connection and when called from client
side, it also stops the transport (it might take a moment to completely
stop grizzly container).

I created simple test:

             while (true) {
                 final CountDownLatch latch = new CountDownLatch(2);

                 final ClientManager client = ClientManager.createClient();

                 URI uri =
getURI(OnOpenCloseEndpoint.class.getAnnotation(ServerEndpoint.class).value());

                 client.connectToServer(new Endpoint() {
                     @Override
                     public void onOpen(Session session, EndpointConfig
EndpointConfig) {
                         latch.countDown();
                     }

                     @Override
                     public void onClose(Session session, CloseReason
closeReason) {
                         latch.countDown();
                     }
                 }, ClientEndpointConfig.Builder.create().build(), uri);

                 latch.await(3, TimeUnit.SECONDS);
                 assertEquals(0, latch.getCount());

                 System.out.println("$$$ " + counter++ + " " +
Thread.activeCount());
                 Thread.sleep(1);
                 System.out.println("$$$ " + counter++ + " " +
Thread.activeCount());
             }

which runs as expected, that test printed out following log:

$$ 1 $$ 354 31
$$ 2 $$ 355 28
$$ 1 $$ 356 31
$$ 2 $$ 357 28
$$ 1 $$ 358 31
$$ 2 $$ 359 28

which seems ok (client takes 3 threads, rest is server and my IDE..).
When I remove last two lines, it repeats "31".. So, if you still see
that issue, can you please try with current 1.4-SNAPSHOT? Or share
complete testcase.

Thanks and regards,
Pavel

On 05/11/13 22:52, Kenneth McCarthy wrote:
> tyrus websockets ClientManager connectToServer 'Handshake response not
> received'
>
> how do I retry the connection without more and more daemon and
> Grizzly-kernel and Grizzly-worker threads created. Is there a call to
> Session or client to kill/cleanup Thread-1 to 4 and Grizzly-kernel and
> Grizzly-worker threads?
>
> Example JAVA main line which attempts forever to make and maintain a
> connection with a server which may not be running or is periodically
> restart.
>
> ------------------------------------------------------------------------
> |public void onClose(Session session, CloseReason closeReason) {
> latch.countDown();
> }
>
>
> enter code here
> public static void main(String[] args) {
> while (true) {
> latch= new CountDownLatch(1);
> ClientManager client= ClientManager.createClient();
> try {
> client.connectToServer(wsListener.class, new URI("wss://<host>/ws"));
> latch.await();
> }
> catch (DeploymentException e) {
> try {
> Thread.sleep(1000);
> } catch (InterruptedException ie) {
> break;
> }
> }
> catch (Exception e) {
> throw new RuntimeException(e);
> }
>
> client= null;
> latch= null;
> // HERE... clean up
> }
> }|