Search in sources :

Example 1 with ConnectionListener

use of com.hazelcast.nio.ConnectionListener in project hazelcast by hazelcast.

the class TcpIpConnectionManager_ConnectionListenerTest method whenConnectionAdded.

@Test
public void whenConnectionAdded() throws Exception {
    startAllConnectionManagers();
    final ConnectionListener listener = mock(ConnectionListener.class);
    connManagerA.addConnectionListener(listener);
    final Connection c = connect(connManagerA, addressB);
    assertTrueEventually(new AssertTask() {

        @Override
        public void run() throws Exception {
            listener.connectionAdded(c);
        }
    });
}
Also used : Connection(com.hazelcast.nio.Connection) AssertTask(com.hazelcast.test.AssertTask) ConnectionListener(com.hazelcast.nio.ConnectionListener) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 2 with ConnectionListener

use of com.hazelcast.nio.ConnectionListener in project hazelcast by hazelcast.

the class TcpIpConnectionManager_ConnectionListenerTest method whenConnectionManagerShutdown_thenListenersRemoved.

@Test
public void whenConnectionManagerShutdown_thenListenersRemoved() {
    startAllConnectionManagers();
    ConnectionListener listener = mock(ConnectionListener.class);
    connManagerA.addConnectionListener(listener);
    connManagerA.shutdown();
    assertEquals(0, connManagerA.connectionListeners.size());
}
Also used : ConnectionListener(com.hazelcast.nio.ConnectionListener) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 3 with ConnectionListener

use of com.hazelcast.nio.ConnectionListener in project hazelcast by hazelcast.

the class MockConnectionManager method onClose.

public void onClose(final Connection connection) {
    final Address endPoint = connection.getEndPoint();
    if (mapConnections.remove(endPoint, connection)) {
        logger.info("Removed connection to endpoint: " + endPoint + ", connection: " + connection);
        ioService.getEventService().executeEventCallback(new StripedRunnable() {

            @Override
            public void run() {
                for (ConnectionListener listener : connectionListeners) {
                    listener.connectionRemoved(connection);
                }
            }

            @Override
            public int getKey() {
                return endPoint.hashCode();
            }
        });
    }
}
Also used : StripedRunnable(com.hazelcast.util.executor.StripedRunnable) Address(com.hazelcast.nio.Address) ConnectionListener(com.hazelcast.nio.ConnectionListener)

Example 4 with ConnectionListener

use of com.hazelcast.nio.ConnectionListener in project hazelcast by hazelcast.

the class TcpIpConnectionManager method registerConnection.

@Override
public synchronized boolean registerConnection(final Address remoteEndPoint, final Connection connection) {
    try {
        if (remoteEndPoint.equals(ioService.getThisAddress())) {
            return false;
        }
        if (!connection.isAlive()) {
            if (logger.isFinestEnabled()) {
                logger.finest(connection + " to " + remoteEndPoint + " is not registered since connection is not active.");
            }
            return false;
        }
        if (connection instanceof TcpIpConnection) {
            TcpIpConnection tcpConnection = (TcpIpConnection) connection;
            Address currentEndPoint = tcpConnection.getEndPoint();
            if (currentEndPoint != null && !currentEndPoint.equals(remoteEndPoint)) {
                throw new IllegalArgumentException(connection + " has already a different endpoint than: " + remoteEndPoint);
            }
            tcpConnection.setEndPoint(remoteEndPoint);
            if (!connection.isClient()) {
                TcpIpConnectionMonitor connectionMonitor = getConnectionMonitor(remoteEndPoint, true);
                tcpConnection.setMonitor(connectionMonitor);
            }
        }
        connectionsMap.put(remoteEndPoint, connection);
        ioService.getEventService().executeEventCallback(new StripedRunnable() {

            @Override
            public void run() {
                for (ConnectionListener listener : connectionListeners) {
                    listener.connectionAdded(connection);
                }
            }

            @Override
            public int getKey() {
                return remoteEndPoint.hashCode();
            }
        });
        return true;
    } finally {
        connectionsInProgress.remove(remoteEndPoint);
    }
}
Also used : StripedRunnable(com.hazelcast.util.executor.StripedRunnable) Address(com.hazelcast.nio.Address) ConnectionListener(com.hazelcast.nio.ConnectionListener)

Example 5 with ConnectionListener

use of com.hazelcast.nio.ConnectionListener in project hazelcast by hazelcast.

the class TcpIpConnectionManager_ConnectionListenerTest method whenConnectionDestroyed.

@Test
public void whenConnectionDestroyed() throws Exception {
    startAllConnectionManagers();
    final ConnectionListener listener = mock(ConnectionListener.class);
    connManagerA.addConnectionListener(listener);
    final Connection c = connect(connManagerA, addressB);
    c.close(null, null);
    assertTrueEventually(new AssertTask() {

        @Override
        public void run() throws Exception {
            listener.connectionRemoved(c);
        }
    });
}
Also used : Connection(com.hazelcast.nio.Connection) AssertTask(com.hazelcast.test.AssertTask) ConnectionListener(com.hazelcast.nio.ConnectionListener) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Aggregations

ConnectionListener (com.hazelcast.nio.ConnectionListener)6 QuickTest (com.hazelcast.test.annotation.QuickTest)3 StripedRunnable (com.hazelcast.util.executor.StripedRunnable)3 Test (org.junit.Test)3 Address (com.hazelcast.nio.Address)2 Connection (com.hazelcast.nio.Connection)2 AssertTask (com.hazelcast.test.AssertTask)2