public org.apache.catalina.tribes.transport.nio.NioSenderRemoteInterface[] setupForSend(
      Member[] destination) throws ChannelException, RemoteException {
    try {
      ChannelException cx = null;
      org.apache.catalina.tribes.transport.nio.NioSenderRemoteInterface[] result =
          new org.apache.catalina.tribes.transport.nio.NioSenderRemoteInterface[destination.length];
      for (int i = 0; i < destination.length; i++) {
        org.apache.catalina.tribes.transport.nio.NioSenderRemoteInterface sender =
            nioSenders.get(destination[i]);
        try {

          if (sender == null) {
            sender = gerenciadornuvem1.getNioSender();
            gerenciadornuvem1.AbstractSendertransferProperties(this, sender);
            nioSenders.put(destination[i], sender);
          }
          sender.reset();
          sender.setDestination(destination[i]);
          sender.setSelector(selector);
          sender.setUdpBased(isUdpBased());
          result[i] = sender;
        } catch (UnknownHostException x) {
          if (cx == null)
            cx = gerenciadornuvem1.getChannelException("Unable to setup NioSender.", x);
          cx.addFaultyMember(destination[i], x);
        }
      }
      if (cx != null) throw cx;
      else return result;
    } catch (Exception excp) {
      excp.printStackTrace();
    }
    return null;
  }
Exemplo n.º 2
0
 @Override
 public void sendMessage(Member[] destination, ChannelMessage msg, InterceptorPayload payload)
     throws ChannelException {
   if (!okToProcess(msg.getOptions())) {
     super.sendMessage(destination, msg, payload);
     return;
   }
   ChannelException cx = null;
   for (int i = 0; i < destination.length; i++) {
     try {
       int nr = 0;
       try {
         outLock.writeLock().lock();
         nr = incCounter(destination[i]);
       } finally {
         outLock.writeLock().unlock();
       }
       // reduce byte copy
       msg.getMessage().append(nr);
       try {
         getNext().sendMessage(new Member[] {destination[i]}, msg, payload);
       } finally {
         msg.getMessage().trim(4);
       }
     } catch (ChannelException x) {
       if (cx == null) cx = x;
       cx.addFaultyMember(x.getFaultyMembers());
     }
   } // for
   if (cx != null) throw cx;
 }
 public void connect(org.apache.catalina.tribes.transport.nio.NioSenderRemoteInterface[] senders)
     throws ChannelException, RemoteException {
   try {
     ChannelException x = null;
     for (int i = 0; i < senders.length; i++) {
       try {
         senders[i].connect();
       } catch (IOException io) {
         if (x == null) x = gerenciadornuvem1.getChannelException(io);
         x.addFaultyMember(senders[i].getDestination(), io);
       }
     }
     if (x != null) throw x;
   } catch (Exception excp) {
     excp.printStackTrace();
   }
 }
 public synchronized void close() throws ChannelException, RemoteException {
   try {
     ChannelException x = null;
     Object[] members = nioSenders.keySet().toArray();
     for (int i = 0; i < members.length; i++) {
       Member mbr = (Member) members[i];
       try {
         org.apache.catalina.tribes.transport.nio.NioSenderRemoteInterface sender =
             nioSenders.get(mbr);
         sender.disconnect();
       } catch (Exception e) {
         if (x == null) x = gerenciadornuvem1.getChannelException(e);
         x.addFaultyMember(mbr, e);
       }
       nioSenders.remove(mbr);
     }
     if (x != null) throw x;
   } catch (Exception excp) {
     excp.printStackTrace();
   }
 }
 public void sendMessage(Member[] destination, ChannelMessage message) throws ChannelException {
   if (!connected) throw new ChannelException("Sender not connected.");
   ParallelNioSender sender = (ParallelNioSender) getSender();
   if (sender == null) {
     ChannelException cx =
         new ChannelException("Unable to retrieve a data sender, time out error.");
     for (int i = 0; i < destination.length; i++)
       cx.addFaultyMember(
           destination[i],
           new NullPointerException("Unable to retrieve a sender from the sender pool"));
     throw cx;
   } else {
     try {
       sender.sendMessage(destination, message);
       sender.keepalive();
     } catch (ChannelException x) {
       sender.disconnect();
       throw x;
     } finally {
       returnSender(sender);
       if (!connected) disconnect();
     }
   }
 }
  @Override
  public synchronized void sendMessage(Member[] destination, ChannelMessage msg)
      throws ChannelException, RemoteException {
    try {
      long start = System.currentTimeMillis();
      this.setUdpBased((msg.getOptions() & Channel.SEND_OPTIONS_UDP) == Channel.SEND_OPTIONS_UDP);
      byte[] data =
          gerenciadornuvem1.XByteBuffercreateDataPackage(
              (org.apache.catalina.tribes.io.ChannelDataRemoteInterface) msg);
      org.apache.catalina.tribes.transport.nio.NioSenderRemoteInterface[] senders =
          setupForSend(destination);
      connect(senders);
      setData(senders, data);

      int remaining = senders.length;
      ChannelException cx = null;
      try {
        // loop until complete, an error happens, or we timeout
        long delta = System.currentTimeMillis() - start;
        boolean waitForAck =
            (Channel.SEND_OPTIONS_USE_ACK & msg.getOptions()) == Channel.SEND_OPTIONS_USE_ACK;
        while ((remaining > 0) && (delta < getTimeout())) {
          try {
            remaining -= doLoop(selectTimeout, getMaxRetryAttempts(), waitForAck, msg);
          } catch (Exception x) {
            if (log.isTraceEnabled()) log.trace("Error sending message", x);
            int faulty = (cx == null) ? 0 : cx.getFaultyMembers().length;
            if (cx == null) {
              if (x instanceof ChannelException) cx = (ChannelException) x;
              else cx = gerenciadornuvem1.getChannelException("Parallel NIO send failed.", x);
            } else {
              if (x instanceof ChannelException)
                cx.addFaultyMember(((ChannelException) x).getFaultyMembers());
            }
            // count down the remaining on an error
            if (faulty < cx.getFaultyMembers().length)
              remaining -= (cx.getFaultyMembers().length - faulty);
          }
          // bail out if all remaining senders are failing
          if (cx != null && cx.getFaultyMembers().length == remaining) throw cx;
          delta = System.currentTimeMillis() - start;
        }
        if (remaining > 0) {
          // timeout has occurred
          ChannelException cxtimeout =
              gerenciadornuvem1.getChannelException(
                  "Operation has timed out(" + getTimeout() + " ms.).");
          if (cx == null)
            cx =
                gerenciadornuvem1.getChannelException(
                    "Operation has timed out(" + getTimeout() + " ms.).");
          for (int i = 0; i < senders.length; i++) {
            if (!senders[i].isComplete())
              cx.addFaultyMember(senders[i].getDestination(), cxtimeout);
          }
          throw cx;
        } else if (cx != null) {
          // there was an error
          throw cx;
        }
      } catch (Exception x) {
        try {
          this.disconnect();
        } catch (Exception e) {
          /* Ignore */
        }
        if (x instanceof ChannelException) throw (ChannelException) x;
        else throw new ChannelException(x);
      }

    } catch (Exception excp) {
      excp.printStackTrace();
    }
  }
  public int doLoop(long selectTimeOut, int maxAttempts, boolean waitForAck, ChannelMessage msg)
      throws IOException, ChannelException, RemoteException {
    try {
      int completed = 0;
      int selectedKeys = selector.select(selectTimeOut);

      if (selectedKeys == 0) {
        return 0;
      }

      Iterator<SelectionKey> it = selector.selectedKeys().iterator();
      while (it.hasNext()) {
        SelectionKey sk = it.next();
        it.remove();
        int readyOps = sk.readyOps();
        sk.interestOps(sk.interestOps() & ~readyOps);
        org.apache.catalina.tribes.transport.nio.NioSenderRemoteInterface sender =
            (org.apache.catalina.tribes.transport.nio.NioSenderRemoteInterface) sk.attachment();
        try {
          if (sender.process(sk, waitForAck)) {
            completed++;
            sender.setComplete(true);
            if (Logs.getMessages().isTraceEnabled()) {
              Logs.getMessages()
                  .trace(
                      "ParallelNioSender - Sent msg:"
                          + new UniqueId(msg.getUniqueId())
                          + " at "
                          + new java.sql.Timestamp(System.currentTimeMillis())
                          + " to "
                          + sender.getDestination().getName());
            }
            SenderState.getSenderState(sender.getDestination()).setReady();
          } // end if
        } catch (Exception x) {
          if (log.isTraceEnabled()) {
            log.trace("Error while processing send to " + sender.getDestination().getName(), x);
          }
          org.apache.catalina.tribes.transport.SenderStateRemoteInterface state =
              gerenciadornuvem0.SenderStategetSenderState(sender.getDestination());
          int attempt = sender.getAttempt() + 1;
          boolean retry = (sender.getAttempt() <= maxAttempts && maxAttempts > 0);
          synchronized (state) {

            // sk.cancel();
            if (state.isSuspect()) state.setFailing();
            if (state.isReady()) {
              state.setSuspect();
              if (retry)
                log.warn(
                    "Member send is failing for:"
                        + sender.getDestination().getName()
                        + " ; Setting to suspect and retrying.");
              else
                log.warn(
                    "Member send is failing for:"
                        + sender.getDestination().getName()
                        + " ; Setting to suspect.",
                    x);
            }
          }
          if (!isConnected()) {
            log.warn(
                "Not retrying send for:"
                    + sender.getDestination().getName()
                    + "; Sender is disconnected.");
            ChannelException cx =
                gerenciadornuvem1.getChannelException(
                    "Send failed, and sender is disconnected. Not retrying.", x);
            cx.addFaultyMember(sender.getDestination(), x);
            throw cx;
          }

          byte[] data = sender.getMessage();
          if (retry) {
            try {
              sender.disconnect();
              sender.connect();
              sender.setAttempt(attempt);
              sender.setMessage(data);
            } catch (Exception ignore) {
              state.setFailing();
            }
          } else {
            ChannelException cx =
                gerenciadornuvem1.getChannelException(
                    "Send failed, attempt:" + sender.getAttempt() + " max:" + maxAttempts, x);
            cx.addFaultyMember(sender.getDestination(), x);
            throw cx;
          } // end if
        }
      }
      return completed;

    } catch (Exception excp) {
      excp.printStackTrace();
    }
    return 0;
  }