public void initializeHeartbeatStageTable(Database database) throws SQLException {
    if (logger.isDebugEnabled()) logger.debug("Initializing heartbeat staging table");

    // Create the table if it does not exist.
    if (database.findTable(hbStageTable.getSchema(), hbStageTable.getName()) == null) {
      database.createTable(
          this.hbStageTable, false, this.hbStageTable.getSchema(), tableType, serviceName);
    }
  }
  /** Set up the heartbeat table on the master. */
  public void initializeHeartbeatTable(Database database) throws SQLException {
    if (logger.isDebugEnabled()) logger.debug("Initializing heartbeat table");

    // Create the table if it does not exist.
    if (database.findTable(hbTable.getSchema(), hbTable.getName()) == null) {
      database.createTable(this.hbTable, false, this.hbTable.getSchema(), tableType, serviceName);
    }

    // Add an initial heartbeat value if needed
    ResultSet res = null;
    PreparedStatement hbRowCount = null;
    int rows = 0;

    try {
      hbRowCount =
          database.prepareStatement(
              "SELECT count(*) from " + this.hbTable.getSchema() + "." + this.hbTable.getName());
      res = hbRowCount.executeQuery();
      if (res.next()) {
        rows = res.getInt(1);
      }
    } finally {
      if (res != null) {
        try {
          res.close();
        } catch (SQLException e) {
        }
      }
      if (hbRowCount != null) {
        try {
          hbRowCount.close();
        } catch (Exception e) {
        }
      }
    }

    if (rows == 0) {

      hbId.setValue(KEY);
      hbSourceTstamp.setValue(new Timestamp(System.currentTimeMillis()));
      hbSalt.setValue(saltValue.getAndIncrement());
      database.insert(hbTable);
    }
  }
  /** TODO: runTask definition. */
  private void runTask() {
    connection = null;
    try {
      connection = DatabaseFactory.createDatabase(url, user, password);
    } catch (SQLException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }

    try {
      connection.connect();
    } catch (SQLException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }

    if (ignoreTablesFile != null) {
      ignoreTablesDefinition = new ChunkDefinitions(ignoreTablesFile);
      try {
        ignoreTablesDefinition.parseFile();
      } catch (Exception e) {
        e.printStackTrace();
      }
    }

    // Check whether we have to use a chunk definition file
    if (chunkDefFile != null) {
      logger.info("Using definition from file " + chunkDefFile);
      chunkDefinition = new ChunkDefinitions(chunkDefFile);
      try {
        chunkDefinition.parseFile();
      } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      } catch (ReplicatorException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }

      LinkedList<ChunkRequest> chunksDefinitions = chunkDefinition.getChunksDefinitions();
      for (ChunkRequest chunkRequest : chunksDefinitions) {
        if (chunkRequest.getTable() != null) {
          try {
            Table table =
                connection.findTable(chunkRequest.getSchema(), chunkRequest.getTable(), true);

            if (table != null)
              generateChunksForTable(table, chunkRequest.getChunkSize(), chunkRequest.getColumns());
            else
              logger.warn(
                  "Failed while processing table "
                      + chunkRequest.getSchema()
                      + "."
                      + chunkRequest.getTable()
                      + " : table not found.");
          } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
          } catch (ReplicatorException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
          } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
          }
        } else if (chunkRequest.getSchema() != null) {
          generateChunksForSchema(chunkRequest.getSchema());
        }
      }
    } else {
      try {
        DatabaseMetaData databaseMetaData = connection.getDatabaseMetaData();
        ResultSet schemasRs = databaseMetaData.getSchemas();
        while (schemasRs.next()) {
          String schemaName = schemasRs.getString("TABLE_SCHEM");
          // TODO: System schemas could be needed -> this needs a
          // setting
          if (!connection.isSystemSchema(schemaName)) {
            generateChunksForSchema(schemaName);
          }
        }
        schemasRs.close();
      } catch (SQLException e) {
        logger.error(e);
      } catch (Exception e) {
        logger.error(e);
      }
    }

    // Stop threads
    for (int i = 0; i < extractChannels; i++) {
      logger.info("Posting job complete request " + i);
      try {
        chunks.put(new NumericChunk());
      } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    }

    if (logger.isDebugEnabled()) logger.debug(this.getName() + " done.");
  }