use of com.hazelcast.client.impl.connection.ClientConnection in project hazelcast by hazelcast.
the class ClientTransactionManagerServiceImpl method connect.
public ClientConnection connect() throws Exception {
ClientInvocationServiceImpl invocationService = (ClientInvocationServiceImpl) client.getInvocationService();
long startTimeMillis = System.currentTimeMillis();
long invocationTimeoutMillis = invocationService.getInvocationTimeoutMillis();
ClientConfig clientConfig = client.getClientConfig();
boolean smartRouting = clientConfig.getNetworkConfig().isSmartRouting();
while (client.getLifecycleService().isRunning()) {
try {
ClientConnection connection = client.getConnectionManager().getRandomConnection();
if (connection == null) {
throw throwException(smartRouting);
}
return connection;
} catch (Exception e) {
if (e instanceof HazelcastClientOfflineException) {
throw e;
}
if (System.currentTimeMillis() - startTimeMillis > invocationTimeoutMillis) {
throw newOperationTimeoutException(e, invocationTimeoutMillis, startTimeMillis);
}
}
Thread.sleep(invocationService.getInvocationRetryPauseMillis());
}
throw new HazelcastClientNotActiveException();
}
use of com.hazelcast.client.impl.connection.ClientConnection in project hazelcast by hazelcast.
the class ClientListenerServiceImpl method handleEventMessageOnCallingThread.
public void handleEventMessageOnCallingThread(ClientMessage clientMessage) {
long correlationId = clientMessage.getCorrelationId();
ClientConnection connection = (ClientConnection) clientMessage.getConnection();
EventHandler eventHandler = connection.getEventHandler(correlationId);
if (eventHandler == null) {
if (logger.isFineEnabled()) {
logger.fine("No eventHandler for callId: " + correlationId + ", event: " + clientMessage);
}
return;
}
eventHandler.handle(clientMessage);
}
use of com.hazelcast.client.impl.connection.ClientConnection in project hazelcast by hazelcast.
the class TcpClientConnectionManager method getRandomConnection.
@Override
public ClientConnection getRandomConnection() {
// Try getting the connection from the load balancer, if smart routing is enabled
if (isSmartRoutingEnabled) {
Member member = loadBalancer.next();
// Failed to get a member
ClientConnection connection = member != null ? activeConnections.get(member.getUuid()) : null;
if (connection != null) {
return connection;
}
}
// Otherwise iterate over connections and return the first one
for (Map.Entry<UUID, TcpClientConnection> connectionEntry : activeConnections.entrySet()) {
return connectionEntry.getValue();
}
// Failed to get a connection
return null;
}
use of com.hazelcast.client.impl.connection.ClientConnection in project hazelcast by hazelcast.
the class ClientListenerServiceImpl method deregisterListenerInternal.
private Boolean deregisterListenerInternal(@Nullable UUID userRegistrationId) {
// This method should only be called from registrationExecutor
assert (Thread.currentThread().getName().contains("eventRegistration"));
ClientListenerRegistration listenerRegistration = registrations.remove(userRegistrationId);
if (listenerRegistration == null) {
return false;
}
Map<Connection, ClientConnectionRegistration> registrations = listenerRegistration.getConnectionRegistrations();
CompletableFuture[] futures = new CompletableFuture[registrations.size()];
int i = 0;
for (Map.Entry<Connection, ClientConnectionRegistration> entry : registrations.entrySet()) {
ClientConnectionRegistration registration = entry.getValue();
ClientConnection subscriber = (ClientConnection) entry.getKey();
// remove local handler
subscriber.removeEventHandler(registration.getCallId());
// the rest is for deleting remote registration
ListenerMessageCodec listenerMessageCodec = listenerRegistration.getCodec();
UUID serverRegistrationId = registration.getServerRegistrationId();
ClientMessage request = listenerMessageCodec.encodeRemoveRequest(serverRegistrationId);
if (request == null) {
futures[i++] = CompletableFuture.completedFuture(null);
continue;
}
ClientInvocation clientInvocation = new ClientInvocation(client, request, null, subscriber);
clientInvocation.setInvocationTimeoutMillis(Long.MAX_VALUE);
futures[i++] = clientInvocation.invokeUrgent().exceptionally(throwable -> {
if (!(throwable instanceof HazelcastClientNotActiveException || throwable instanceof IOException || throwable instanceof TargetDisconnectedException)) {
logger.warning("Deregistration of listener with ID " + userRegistrationId + " has failed for address " + subscriber.getRemoteAddress(), throwable);
}
return null;
});
}
CompletableFuture.allOf(futures).join();
return true;
}
use of com.hazelcast.client.impl.connection.ClientConnection in project hazelcast by hazelcast.
the class ClientInvocation method getPermissionToNotify.
boolean getPermissionToNotify(long responseCorrelationId) {
ClientConnection conn = this.sentConnection;
if (conn == null) {
// we don't need to take action
return false;
}
long requestCorrelationId = clientMessage.getCorrelationId();
if (responseCorrelationId != requestCorrelationId) {
// we should not notify
return false;
}
// otherwise another thread is handling it, we don't need to notify anymore
return SENT_CONNECTION.compareAndSet(this, conn, null);
}
Aggregations