Пример #1
0
  void listenMembershipEvents(Address ownerConnectionAddress) {
    initialListFetchedLatch = new CountDownLatch(1);
    try {
      ClientMessage clientMessage = RegisterMembershipListenerParameters.encode();

      Connection connection = connectionManager.getConnection(ownerConnectionAddress);
      if (connection == null) {
        System.out.println("FATAL connection null " + ownerConnectionAddress);
        throw new IllegalStateException(
            "Can not load initial members list because owner connection is null. "
                + "Address "
                + ownerConnectionAddress);
      }
      ClientInvocation invocation = new ClientInvocation(client, this, clientMessage, connection);
      invocation.invoke().get();
      waitInitialMemberListFetched();

    } catch (Exception e) {
      if (client.getLifecycleService().isRunning()) {
        if (LOGGER.isFinestEnabled()) {
          LOGGER.warning(
              "Error while registering to cluster events! -> " + ownerConnectionAddress, e);
        } else {
          LOGGER.warning(
              "Error while registering to cluster events! -> "
                  + ownerConnectionAddress
                  + ", Error: "
                  + e.toString());
        }
      }
    }
  }
Пример #2
0
 protected void updateCacheListenerConfigOnOtherNodes(
     CacheEntryListenerConfiguration<K, V> cacheEntryListenerConfiguration, boolean isRegister) {
   final Collection<Member> members = clientContext.getClusterService().getMemberList();
   final HazelcastClientInstanceImpl client =
       (HazelcastClientInstanceImpl) clientContext.getHazelcastInstance();
   final Collection<Future> futures = new ArrayList<Future>();
   for (Member member : members) {
     try {
       final Address address = member.getAddress();
       Data configData = toData(cacheEntryListenerConfiguration);
       final ClientMessage request =
           CacheListenerRegistrationCodec.encodeRequest(
               nameWithPrefix, configData, isRegister, address.getHost(), address.getPort());
       final ClientInvocation invocation = new ClientInvocation(client, request, address);
       final Future future = invocation.invoke();
       futures.add(future);
     } catch (Exception e) {
       ExceptionUtil.sneakyThrow(e);
     }
   }
   // make sure all configs are created
   // TODO do we need this ???s
   //        try {
   //            FutureUtil.waitWithDeadline(futures,
   // CacheProxyUtil.AWAIT_COMPLETION_TIMEOUT_SECONDS, TimeUnit.SECONDS);
   //        } catch (TimeoutException e) {
   //            logger.warning(e);
   //        }
 }
Пример #3
0
 @Override
 public void run() {
   try {
     invoke();
   } catch (Throwable e) {
     clientInvocationFuture.complete(e);
   }
 }
Пример #4
0
 @Override
 public void run() {
   try {
     invoke();
   } catch (Throwable e) {
     if (handler != null) {
       listenerService.registerFailedListener(this);
     } else {
       clientInvocationFuture.setResponse(e);
     }
   }
 }
  protected Object getInternal(K key, ExpiryPolicy expiryPolicy, boolean async) {
    ensureOpen();
    validateNotNull(key);
    final Data keyData = toData(key);
    Object cached = getFromNearCache(keyData, async);
    if (cached != null) {
      return cached;
    }
    final Data expiryPolicyData = toData(expiryPolicy);
    ClientMessage request = CacheGetCodec.encodeRequest(nameWithPrefix, keyData, expiryPolicyData);
    ClientInvocationFuture future;
    try {
      final int partitionId = clientContext.getPartitionService().getPartitionId(key);
      final HazelcastClientInstanceImpl client =
          (HazelcastClientInstanceImpl) clientContext.getHazelcastInstance();
      final ClientInvocation clientInvocation = new ClientInvocation(client, request, partitionId);
      future = clientInvocation.invoke();
    } catch (Exception e) {
      throw ExceptionUtil.rethrow(e);
    }
    SerializationService serializationService = clientContext.getSerializationService();
    ClientDelegatingFuture<V> delegatingFuture =
        new ClientDelegatingFuture<V>(future, serializationService, cacheGetResponseDecoder);
    if (async) {
      if (nearCache != null) {
        delegatingFuture.andThenInternal(
            new ExecutionCallback<Data>() {
              public void onResponse(Data valueData) {
                storeInNearCache(keyData, valueData, null);
              }

              public void onFailure(Throwable t) {}
            });
      }
      return delegatingFuture;
    } else {
      try {
        Object value = delegatingFuture.get();
        if (nearCache != null) {
          storeInNearCache(keyData, (Data) delegatingFuture.getResponse(), null);
        }
        if (!(value instanceof Data)) {
          return value;
        } else {
          return serializationService.toObject(value);
        }
      } catch (Throwable e) {
        throw ExceptionUtil.rethrowAllowedTypeFirst(e, CacheException.class);
      }
    }
  }
  private void invoke(ClientRegistrationKey registrationKey, Address address) throws Exception {
    ListenerMessageCodec codec = registrationKey.getCodec();
    ClientMessage request = codec.encodeAddRequest(true);
    EventHandler handler = registrationKey.getHandler();
    handler.beforeListenerRegister();

    ClientInvocation invocation = new ClientInvocation(client, request, address);
    invocation.setEventHandler(handler);
    String serverRegistrationId = codec.decodeAddResponse(invocation.invoke().get());

    handler.onListenerRegister();
    int correlationId = request.getCorrelationId();
    ClientEventRegistration registration =
        new ClientEventRegistration(serverRegistrationId, correlationId, address, codec);

    Map<Address, ClientEventRegistration> registrationMap = registrations.get(registrationKey);
    registrationMap.put(address, registration);
  }
  @Override
  public void heartBeatStopped(Connection connection) {
    ClientMessage request = ClientRemoveAllListenersCodec.encodeRequest();
    ClientInvocation removeListenerInvocation = new ClientInvocation(client, request, connection);
    removeListenerInvocation.setBypassHeartbeatCheck(true);
    removeListenerInvocation.invoke();

    final Address remoteEndpoint = connection.getEndPoint();
    final Iterator<ClientListenerInvocation> iterator = eventHandlerMap.values().iterator();
    final TargetDisconnectedException response = new TargetDisconnectedException(remoteEndpoint);

    while (iterator.hasNext()) {
      final ClientInvocation clientInvocation = iterator.next();
      if (clientInvocation.getSendConnection().equals(connection)) {
        iterator.remove();
        clientInvocation.notifyException(response);
      }
    }
  }
  void listenMembershipEvents(Address ownerConnectionAddress) {
    initialListFetchedLatch = new CountDownLatch(1);
    try {
      ClientMessage clientMessage = ClientMembershipListenerCodec.encodeRequest();

      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 ClientListenerInvocation(
              client,
              this,
              clientMessage,
              connection,
              new ClientMessageDecoder() {
                @Override
                public <T> T decodeClientMessage(ClientMessage clientMessage) {
                  return (T) ClientMembershipListenerCodec.decodeResponse(clientMessage).response;
                }
              });
      invocation.invoke().get();
      waitInitialMemberListFetched();

    } catch (Exception e) {
      if (client.getLifecycleService().isRunning()) {
        if (LOGGER.isFinestEnabled()) {
          LOGGER.warning(
              "Error while registering to cluster events! -> " + ownerConnectionAddress, e);
        } else {
          LOGGER.warning(
              "Error while registering to cluster events! -> "
                  + ownerConnectionAddress
                  + ", Error: "
                  + e.toString());
        }
      }
    }
  }
 protected ClientInvocationFuture invoke(ClientMessage req, int partitionId, int completionId) {
   final boolean completionOperation = completionId != -1;
   if (completionOperation) {
     registerCompletionLatch(completionId, 1);
   }
   try {
     HazelcastClientInstanceImpl client =
         (HazelcastClientInstanceImpl) clientContext.getHazelcastInstance();
     final ClientInvocation clientInvocation = new ClientInvocation(client, req, partitionId);
     ClientInvocationFuture f = clientInvocation.invoke();
     if (completionOperation) {
       waitCompletionLatch(completionId, f);
     }
     return f;
   } catch (Throwable e) {
     if (e instanceof IllegalStateException) {
       close();
     }
     if (completionOperation) {
       deregisterCompletionLatch(completionId);
     }
     throw ExceptionUtil.rethrowAllowedTypeFirst(e, CacheException.class);
   }
 }