Exemplo n.º 1
0
  @Override
  public FutureClusterResponses execute(ClusterRequest clusterRequest) {
    if (!isEnabled()) {
      return null;
    }

    List<Address> addresses = prepareAddresses(clusterRequest);

    Set<String> clusterNodeIds = new HashSet<>();

    for (Address address : addresses) {
      ClusterNode clusterNode = _liveInstances.get(address);

      if (clusterNode != null) {
        clusterNodeIds.add(clusterNode.getClusterNodeId());
      }
    }

    FutureClusterResponses futureClusterResponses = new FutureClusterResponses(clusterNodeIds);

    if (!clusterRequest.isFireAndForget()) {
      String uuid = clusterRequest.getUuid();

      _futureClusterResponses.put(uuid, futureClusterResponses);
    }

    if (addresses.remove(_localAddress)) {
      ClusterNodeResponse clusterNodeResponse = executeClusterRequest(clusterRequest);

      if (!clusterRequest.isFireAndForget()) {
        futureClusterResponses.addClusterNodeResponse(clusterNodeResponse);
      }
    }

    if (clusterRequest.isMulticast()) {
      try {
        _controlJChannel.send(null, clusterRequest);
      } catch (Exception e) {
        throw new SystemException("Unable to send multicast request", e);
      }
    } else {
      for (Address address : addresses) {
        org.jgroups.Address jGroupsAddress = (org.jgroups.Address) address.getRealAddress();

        try {
          _controlJChannel.send(jGroupsAddress, clusterRequest);
        } catch (Exception e) {
          throw new SystemException("Unable to send unicast request", e);
        }
      }
    }

    return futureClusterResponses;
  }
  @Override
  protected void doReceive(Object messagePayload, Address srcAddress) {
    ClusterChannel clusterChannel = _clusterExecutorImpl.getClusterChannel();

    if (srcAddress.equals(clusterChannel.getLocalAddress())) {
      return;
    }

    try {
      if (messagePayload instanceof ClusterRequest) {
        ClusterRequest clusterRequest = (ClusterRequest) messagePayload;

        Serializable responsePayload =
            _clusterExecutorImpl.handleReceivedClusterRequest(clusterRequest);

        if (clusterRequest.isFireAndForget()) {
          return;
        }

        try {
          clusterChannel.sendUnicastMessage(responsePayload, srcAddress);
        } catch (Throwable t) {
          _log.error("Unable to send message " + responsePayload, t);
        }
      } else if (messagePayload instanceof ClusterNodeResponse) {
        _clusterExecutorImpl.handleReceivedClusterNodeResponse(
            (ClusterNodeResponse) messagePayload);
      } else if (_log.isWarnEnabled()) {
        _log.warn("Unable to process message content of type " + messagePayload.getClass());
      }
    } finally {
      ThreadLocalCacheManager.clearAll(Lifecycle.REQUEST);

      CentralizedThreadLocal.clearShortLivedThreadLocals();
    }
  }