Search in sources :

Example 21 with Connection

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

the class ClientMembershipListener method listenMembershipEvents.

void listenMembershipEvents(Address ownerConnectionAddress) throws Exception {
    initialListFetchedLatch = new CountDownLatch(1);
    ClientMessage clientMessage = ClientAddMembershipListenerCodec.encodeRequest(false);
    Connection connection = connectionManager.getConnection(ownerConnectionAddress);
    if (connection == null) {
        throw new IllegalStateException("Can not load initial members list because owner connection is null. Address " + ownerConnectionAddress);
    }
    ClientInvocation invocation = new ClientInvocation(client, clientMessage, connection);
    invocation.setEventHandler(this);
    invocation.invokeUrgent().get();
    waitInitialMemberListFetched();
}
Also used : Connection(com.hazelcast.nio.Connection) ClientMessage(com.hazelcast.client.impl.protocol.ClientMessage) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 22 with Connection

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

the class ClientMembershipListener method memberRemoved.

private void memberRemoved(Member member) {
    members.remove(member);
    logger.info(membersString());
    final Connection connection = connectionManager.getConnection(member.getAddress());
    if (connection != null) {
        connection.close(null, newTargetDisconnectedExceptionCausedByMemberLeftEvent(connection));
    }
    MembershipEvent event = new MembershipEvent(client.getCluster(), member, MembershipEvent.MEMBER_REMOVED, unmodifiableSet(members));
    clusterService.handleMembershipEvent(event);
}
Also used : InitialMembershipEvent(com.hazelcast.core.InitialMembershipEvent) MembershipEvent(com.hazelcast.core.MembershipEvent) Connection(com.hazelcast.nio.Connection)

Example 23 with Connection

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

the class ClientConnectionTest method destroyConnection_whenDestroyedMultipleTimes_thenListenerRemoveCalledOnce.

@Test
public void destroyConnection_whenDestroyedMultipleTimes_thenListenerRemoveCalledOnce() {
    HazelcastInstance server = hazelcastFactory.newHazelcastInstance();
    HazelcastInstance client = hazelcastFactory.newHazelcastClient();
    HazelcastClientInstanceImpl clientImpl = ClientTestUtil.getHazelcastClientInstanceImpl(client);
    ClientConnectionManager connectionManager = clientImpl.getConnectionManager();
    final CountingConnectionRemoveListener listener = new CountingConnectionRemoveListener();
    connectionManager.addConnectionListener(listener);
    final Address serverAddress = new Address(server.getCluster().getLocalMember().getSocketAddress());
    final Connection connectionToServer = connectionManager.getConnection(serverAddress);
    final CountDownLatch isConnected = new CountDownLatch(1);
    clientImpl.getLifecycleService().addLifecycleListener(new LifecycleListener() {

        @Override
        public void stateChanged(LifecycleEvent event) {
            if (LifecycleEvent.LifecycleState.CLIENT_CONNECTED == event.getState()) {
                isConnected.countDown();
            }
        }
    });
    connectionToServer.close(null, null);
    assertTrueEventually(new AssertTask() {

        @Override
        public void run() throws Exception {
            assertTrue(isConnected.await(5, TimeUnit.SECONDS));
        }
    });
    connectionToServer.close(null, null);
    assertEquals("connection removed should be called only once", 1, listener.count.get());
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) Address(com.hazelcast.nio.Address) InetSocketAddress(java.net.InetSocketAddress) Connection(com.hazelcast.nio.Connection) HazelcastClientInstanceImpl(com.hazelcast.client.impl.HazelcastClientInstanceImpl) LifecycleEvent(com.hazelcast.core.LifecycleEvent) AssertTask(com.hazelcast.test.AssertTask) LifecycleListener(com.hazelcast.core.LifecycleListener) CountDownLatch(java.util.concurrent.CountDownLatch) ClientConnectionManager(com.hazelcast.client.connection.ClientConnectionManager) ExecutionException(java.util.concurrent.ExecutionException) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 24 with Connection

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

the class ClusterListenerSupport method connect.

private boolean connect(Set<InetSocketAddress> triedAddresses) throws Exception {
    final Collection<InetSocketAddress> socketAddresses = getSocketAddresses();
    for (InetSocketAddress inetSocketAddress : socketAddresses) {
        if (!client.getLifecycleService().isRunning()) {
            if (logger.isFinestEnabled()) {
                logger.finest("Giving up on retrying to connect to cluster since client is shutdown");
            }
            break;
        }
        Connection connection = null;
        try {
            triedAddresses.add(inetSocketAddress);
            Address address = new Address(inetSocketAddress);
            logger.info("Trying to connect to " + address + " as owner member");
            connection = connectionManager.getOrConnect(address, true);
            clientMembershipListener.listenMembershipEvents(ownerConnectionAddress);
            fireConnectionEvent(LifecycleEvent.LifecycleState.CLIENT_CONNECTED);
            return true;
        } catch (Exception e) {
            Level level = e instanceof AuthenticationException ? Level.WARNING : Level.FINEST;
            logger.log(level, "Exception during initial connection to " + inetSocketAddress, e);
            if (null != connection) {
                connection.close("Could not connect to " + inetSocketAddress + " as owner", e);
            }
        }
    }
    return false;
}
Also used : Address(com.hazelcast.nio.Address) InetSocketAddress(java.net.InetSocketAddress) AuthenticationException(com.hazelcast.client.AuthenticationException) InetSocketAddress(java.net.InetSocketAddress) Connection(com.hazelcast.nio.Connection) Level(java.util.logging.Level) AuthenticationException(com.hazelcast.client.AuthenticationException) TargetDisconnectedException(com.hazelcast.spi.exception.TargetDisconnectedException)

Example 25 with Connection

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

the class ClientSmartListenerService method deregisterListener.

@Override
public boolean deregisterListener(final String userRegistrationId) {
    Future<Boolean> future = registrationExecutor.submit(new Callable<Boolean>() {

        @Override
        public Boolean call() throws Exception {
            ClientRegistrationKey key = new ClientRegistrationKey(userRegistrationId);
            Map<Connection, ClientEventRegistration> registrationMap = registrations.get(key);
            if (registrationMap == null) {
                return false;
            }
            boolean successful = true;
            for (ClientEventRegistration registration : registrationMap.values()) {
                Connection subscriber = registration.getSubscriber();
                try {
                    ListenerMessageCodec listenerMessageCodec = registration.getCodec();
                    String serverRegistrationId = registration.getServerRegistrationId();
                    ClientMessage request = listenerMessageCodec.encodeRemoveRequest(serverRegistrationId);
                    new ClientInvocation(client, request, subscriber).invoke().get();
                    removeEventHandler(registration.getCallId());
                    registrationMap.remove(subscriber);
                } catch (Exception e) {
                    successful = false;
                    logger.warning("Deregistration of listener with id " + userRegistrationId + " has failed to address " + subscriber.getEndPoint(), e);
                }
            }
            if (successful) {
                registrations.remove(key);
            }
            return successful;
        }
    });
    try {
        return future.get();
    } catch (Exception e) {
        throw ExceptionUtil.rethrow(e);
    }
}
Also used : ClientConnection(com.hazelcast.client.connection.nio.ClientConnection) Connection(com.hazelcast.nio.Connection) ClientInvocation(com.hazelcast.client.spi.impl.ClientInvocation) ClientMessage(com.hazelcast.client.impl.protocol.ClientMessage) HazelcastException(com.hazelcast.core.HazelcastException) IOException(java.io.IOException) ListenerMessageCodec(com.hazelcast.client.spi.impl.ListenerMessageCodec) HashMap(java.util.HashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Aggregations

Connection (com.hazelcast.nio.Connection)59 Address (com.hazelcast.nio.Address)15 ClientConnection (com.hazelcast.client.connection.nio.ClientConnection)10 Test (org.junit.Test)9 QuickTest (com.hazelcast.test.annotation.QuickTest)7 IOException (java.io.IOException)7 ClientConnectionManager (com.hazelcast.client.connection.ClientConnectionManager)6 ClientMessage (com.hazelcast.client.impl.protocol.ClientMessage)6 AssertTask (com.hazelcast.test.AssertTask)6 HazelcastClientInstanceImpl (com.hazelcast.client.impl.HazelcastClientInstanceImpl)5 Packet (com.hazelcast.nio.Packet)5 ClientClusterService (com.hazelcast.client.spi.ClientClusterService)4 ClientInvocation (com.hazelcast.client.spi.impl.ClientInvocation)4 Member (com.hazelcast.core.Member)4 Node (com.hazelcast.instance.Node)4 CountDownLatch (java.util.concurrent.CountDownLatch)4 HazelcastInstance (com.hazelcast.core.HazelcastInstance)3 ParallelTest (com.hazelcast.test.annotation.ParallelTest)3 EventHandler (com.hazelcast.client.spi.EventHandler)2 ClientInvocationFuture (com.hazelcast.client.spi.impl.ClientInvocationFuture)2