示例#1
0
    public void prepare(String query, InetAddress toExclude) throws InterruptedException {
      for (Map.Entry<Host, HostConnectionPool> entry : pools.entrySet()) {
        if (entry.getKey().getAddress().equals(toExclude)) continue;

        // Let's not wait too long if we can't get a connection. Things
        // will fix themselves once the user tries a query anyway.
        Connection c = null;
        try {
          c = entry.getValue().borrowConnection(200, TimeUnit.MILLISECONDS);
          c.write(new PrepareMessage(query)).get();
        } catch (ConnectionException e) {
          // Again, not being able to prepare the query right now is no big deal, so just ignore
        } catch (BusyConnectionException e) {
          // Same as above
        } catch (TimeoutException e) {
          // Same as above
        } catch (ExecutionException e) {
          // We shouldn't really get exception while preparing a
          // query, so log this (but ignore otherwise as it's not a big deal)
          logger.error(
              String.format(
                  "Unexpected error while preparing query (%s) on %s", query, entry.getKey()),
              e);
        } finally {
          if (c != null) entry.getValue().returnConnection(c);
        }
      }
    }
示例#2
0
 public void sendResponse(ClientEndpoint endpoint, Object response) {
   if (response instanceof Throwable) {
     response =
         ClientExceptionConverters.get(endpoint.getClientType()).convert((Throwable) response);
   }
   final Data resultData = response != null ? serializationService.toData(response) : NULL;
   Connection conn = endpoint.getConnection();
   conn.write(new DataAdapter(resultData, serializationService.getSerializationContext()));
 }
示例#3
0
  /**
   * Send information to target host
   *
   * @param args
   * @param callbackContext
   */
  private void send(JSONArray args, CallbackContext callbackContext) {
    Connection socket;

    // validating parameters
    if (args.length() < 2) {
      callbackContext.error("Missing arguments when calling 'send' action.");
    } else {
      try {
        // retrieving parameters
        String key = args.getString(0);
        String data = args.getString(1);

        // getting socket
        socket = this.pool.get(key);

        // checking if socket was not found and his connectivity
        if (socket == null) {
          callbackContext.error("No connection found with host " + key);

        } else if (!socket.isConnected()) {
          callbackContext.error("Invalid connection with host " + key);

        } else if (data.length() == 0) {
          callbackContext.error("Cannot send empty data to " + key);

        } else {

          // write on output stream
          socket.write(data);

          // ending send process
          callbackContext.success();
        }

      } catch (JSONException e) {
        callbackContext.error("Unexpected error sending information: " + e.getMessage());
      }
    }
  }
 public boolean write(Data data) throws IOException {
   return connection.write(data);
 }