Ejemplo n.º 1
0
  @Override
  public void write(Message msg) throws Exception {
    if (shutdown || driverFailed) {
      return;
    }

    try {
      if (connection == null) {
        reconnect();
      }

      if (connection == null) {
        return;
      }

      synchronized (connection) {
        int index = 1;
        logInsert.setTimestamp(index++, new Timestamp(msg.getTimestamp().getMillis()));
        logInsert.setString(index++, msg.getId());
        logInsert.setString(index++, msg.getSource());
        String ms = msg.getMessage();
        if (ms != null && ms.length() > MAX_MESSAGE) {
          ms = ms.substring(0, MAX_MESSAGE);
        }
        logInsert.setString(index++, ms);

        if (fields != null) {
          for (String f : fields) {
            Object value = msg.getField(f);
            String s = value != null ? value.toString() : null;
            if (s == null) {
              logInsert.setNull(index++, Types.VARCHAR);
            } else {
              if (s.length() > MAX_VALUE) {
                s = s.substring(0, MAX_VALUE);
              }
              logInsert.setString(index++, s);
            }
          }
        }

        logInsert.executeUpdate();

        if (logInsertAttribute != null) {
          Object id = null;
          ResultSet ids = logInsert.getGeneratedKeys();
          while (ids != null && ids.next()) {
            id = ids.getObject(1);
          }
          if (id != null) {
            for (Entry<String, Object> e : msg.getFieldsEntries()) {
              String name = e.getKey();
              Object value = e.getValue();
              String s = value != null ? value.toString() : null;
              logInsertAttribute.setObject(1, id);
              logInsertAttribute.setString(2, name);
              if (s.length() > MAX_VALUE) {
                s = s.substring(0, MAX_VALUE);
              }
              logInsertAttribute.setString(3, s);
              logInsertAttribute.executeUpdate();
            }
          } else {
            throw new SQLException("Failed to generate ID for primary log record!");
          }
        }
      }
    } catch (SQLException e) {
      log.log(Level.WARNING, "JDBC output error: " + e.getMessage(), e);
      if (connection != null) {
        try {
          connection.rollback();
          connection.setAutoCommit(true);
        } catch (SQLException ee) {
          // Don`t care
        }
      }
      connection = null;
    } finally {
      if (connection != null) {
        connection.commit();
      }
    }
  }
Ejemplo n.º 2
0
 @Override
 public String apply(final Message input) {
   return input.getId();
 }