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();
}
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);
}
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());
}
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;
}
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);
}
}
Aggregations