Search in sources :

Example 21 with Connection

use of com.hazelcast.internal.nio.Connection in project hazelcast by hazelcast.

the class ClientListenerServiceImpl method registerListener.

@Nonnull
@Override
public UUID registerListener(final ListenerMessageCodec codec, final EventHandler handler) {
    // This method should not be called from registrationExecutor
    assert (!Thread.currentThread().getName().contains("eventRegistration"));
    Future<UUID> future = registrationExecutor.submit(() -> {
        UUID userRegistrationId = UuidUtil.newUnsecureUUID();
        ClientListenerRegistration registration = new ClientListenerRegistration(handler, codec);
        registrations.put(userRegistrationId, registration);
        Collection<Connection> connections = clientConnectionManager.getActiveConnections();
        for (Connection connection : connections) {
            try {
                invoke(registration, connection);
            } catch (Exception e) {
                if (connection.isAlive()) {
                    deregisterListenerInternal(userRegistrationId);
                    throw new HazelcastException("Listener can not be added ", e);
                }
            }
        }
        return userRegistrationId;
    });
    try {
        return future.get();
    } catch (Exception e) {
        throw ExceptionUtil.rethrow(e);
    }
}
Also used : HazelcastException(com.hazelcast.core.HazelcastException) ClientConnection(com.hazelcast.client.impl.connection.ClientConnection) Connection(com.hazelcast.internal.nio.Connection) UUID(java.util.UUID) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) HazelcastClientNotActiveException(com.hazelcast.client.HazelcastClientNotActiveException) HazelcastException(com.hazelcast.core.HazelcastException) IOException(java.io.IOException) TargetDisconnectedException(com.hazelcast.spi.exception.TargetDisconnectedException) Nonnull(javax.annotation.Nonnull)

Example 22 with Connection

use of com.hazelcast.internal.nio.Connection in project hazelcast by hazelcast.

the class TcpServerConnectionManager_AbstractConnectMemberTest method getOrConnect_whenAlreadyConnectedSameConnectionReturned.

@Test
public void getOrConnect_whenAlreadyConnectedSameConnectionReturned() {
    startAllTcpServers();
    Connection c1 = connect(tcpServerA, addressB);
    Connection c2 = tcpServerA.getConnectionManager(MEMBER).getOrConnect(addressB);
    assertSame(c1, c2);
}
Also used : Connection(com.hazelcast.internal.nio.Connection) Test(org.junit.Test)

Example 23 with Connection

use of com.hazelcast.internal.nio.Connection in project hazelcast by hazelcast.

the class TcpServerConnectionManager_ConnectionListenerTest method whenConnectionAdded.

@Test
public void whenConnectionAdded() {
    startAllTcpServers();
    ConnectionListener listener = mock(ConnectionListener.class);
    tcpServerA.getConnectionManager(MEMBER).addConnectionListener(listener);
    Connection c = connect(tcpServerA, addressB);
    assertTrueEventually(() -> listener.connectionAdded(c));
}
Also used : Connection(com.hazelcast.internal.nio.Connection) ConnectionListener(com.hazelcast.internal.nio.ConnectionListener) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 24 with Connection

use of com.hazelcast.internal.nio.Connection in project hazelcast by hazelcast.

the class TcpServerConnectionManager_ConnectionListenerTest method whenConnectionDestroyed.

@Test
public void whenConnectionDestroyed() {
    startAllTcpServers();
    ConnectionListener listener = mock(ConnectionListener.class);
    tcpServerA.getConnectionManager(MEMBER).addConnectionListener(listener);
    Connection c = connect(tcpServerA, addressB);
    c.close(null, null);
    assertTrueEventually(() -> listener.connectionRemoved(c));
}
Also used : Connection(com.hazelcast.internal.nio.Connection) ConnectionListener(com.hazelcast.internal.nio.ConnectionListener) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 25 with Connection

use of com.hazelcast.internal.nio.Connection in project hazelcast by hazelcast.

the class AbstractListenersOnReconnectTest method validateRegistrations.

private void validateRegistrations(final int clusterSize, final UUID registrationId, final HazelcastClientInstanceImpl clientInstanceImpl) {
    final boolean smartRouting = clientInstanceImpl.getClientConfig().getNetworkConfig().isSmartRouting();
    assertTrueEventually(() -> {
        int size = smartRouting ? clusterSize : 1;
        Map<Connection, ClientConnectionRegistration> registrations = getClientEventRegistrations(client, registrationId);
        assertEquals(size, registrations.size());
        if (smartRouting) {
            Collection<Member> members = clientInstanceImpl.getClientClusterService().getMemberList();
            for (Connection registeredSubscriber : registrations.keySet()) {
                boolean contains = false;
                for (Member member : members) {
                    contains |= registeredSubscriber.getRemoteAddress().equals(member.getAddress());
                }
                assertTrue("Registered member " + registeredSubscriber + " is not in the cluster member list " + members, contains);
            }
        } else {
            Iterator<Connection> expectedIterator = registrations.keySet().iterator();
            assertTrue(expectedIterator.hasNext());
            Iterator<Connection> iterator = clientInstanceImpl.getConnectionManager().getActiveConnections().iterator();
            assertTrue(iterator.hasNext());
            assertEquals(iterator.next(), expectedIterator.next());
        }
    });
}
Also used : Connection(com.hazelcast.internal.nio.Connection) Member(com.hazelcast.cluster.Member) ClientConnectionRegistration(com.hazelcast.client.impl.spi.impl.listener.ClientConnectionRegistration)

Aggregations

Connection (com.hazelcast.internal.nio.Connection)41 Test (org.junit.Test)14 QuickTest (com.hazelcast.test.annotation.QuickTest)11 ClientMessage (com.hazelcast.client.impl.protocol.ClientMessage)8 ServerConnection (com.hazelcast.internal.server.ServerConnection)7 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)7 ClientConnection (com.hazelcast.client.impl.connection.ClientConnection)6 Address (com.hazelcast.cluster.Address)6 HazelcastClientInstanceImpl (com.hazelcast.client.impl.clientside.HazelcastClientInstanceImpl)5 Member (com.hazelcast.cluster.Member)5 HazelcastInstance (com.hazelcast.core.HazelcastInstance)5 ConnectionListener (com.hazelcast.internal.nio.ConnectionListener)5 ClientConnectionManager (com.hazelcast.client.impl.connection.ClientConnectionManager)4 ILogger (com.hazelcast.logging.ILogger)4 QueryId (com.hazelcast.sql.impl.QueryId)4 Map (java.util.Map)4 UUID (java.util.UUID)4 ClientInvocation (com.hazelcast.client.impl.spi.impl.ClientInvocation)3 TargetDisconnectedException (com.hazelcast.spi.exception.TargetDisconnectedException)3 IOException (java.io.IOException)3