Example usage for javax.websocket Session removeMessageHandler

List of usage examples for javax.websocket Session removeMessageHandler

Introduction

In this page you can find the example usage for javax.websocket Session removeMessageHandler.

Prototype

void removeMessageHandler(MessageHandler listener);

Source Link

Usage

From source file:org.ocelotds.integration.AbstractOcelotTest.java

/**
 * Test reception of 0 msg triggered by call Runnable
 *
 * @param wssession//from w  ww .  jav  a  2  s .  co  m
 * @param topic
 * @param trigger
 */
protected void testWait0MessageToTopic(Session wssession, String topic, Runnable trigger) {
    try {
        long t0 = System.currentTimeMillis();
        CountDownLatch lock = new CountDownLatch(1);
        CountDownMessageHandler messageHandler = new CountDownMessageHandler(topic, lock);
        wssession.addMessageHandler(messageHandler);
        trigger.run();
        boolean await = lock.await(TIMEOUT, TimeUnit.MILLISECONDS);
        long t1 = System.currentTimeMillis();
        assertThat(await).as("Timeout. waiting %d ms. Remain %d/%d msgs", t1 - t0, lock.getCount(), 1)
                .isFalse();
        wssession.removeMessageHandler(messageHandler);
    } catch (IllegalStateException | InterruptedException ex) {
        fail(ex.getMessage());
    }
}

From source file:org.ocelotds.integration.AbstractOcelotTest.java

/**
 * Test reception of X msg triggered by call Runnable
 *
 * @param wssession/*from  w  ww .  ja v  a  2s  .  c om*/
 * @param nbMsg
 * @param topic
 * @param trigger
 */
protected void testWaitXMessageToTopic(Session wssession, int nbMsg, String topic, Runnable trigger) {
    try {
        long t0 = System.currentTimeMillis();
        CountDownLatch lock = new CountDownLatch(nbMsg);
        CountDownMessageHandler messageHandler = new CountDownMessageHandler(topic, lock);
        wssession.addMessageHandler(messageHandler);
        trigger.run();
        boolean await = lock.await(TIMEOUT * nbMsg, TimeUnit.MILLISECONDS);
        long t1 = System.currentTimeMillis();
        assertThat(await).as("Timeout. waiting %d ms. Remain %d/%d msgs", t1 - t0, lock.getCount(), nbMsg)
                .isTrue();
        wssession.removeMessageHandler(messageHandler);
    } catch (IllegalStateException | InterruptedException ex) {
        fail(ex.getMessage());
    }
}