Ejemplo n.º 1
0
 private void readDataBase() {
   try {
     logger.traceINFO("Initializing database...");
     Class.forName("com.mysql.jdbc.Driver");
     connect =
         DriverManager.getConnection("jdbc:mysql://localhost/marketdata?" + "user=root&password=");
   } catch (Exception e) {
     logger.traceERROR(e);
     System.exit(0);
   }
 }
Ejemplo n.º 2
0
  public RealtimeTimer(int timerInterval, int hourStart, int minStart, int hourStop, int minStop) {
    this.timerInterval = timerInterval;
    Calendar startDateCal = Calendar.getInstance();
    startDateCal.set(Calendar.HOUR_OF_DAY, hourStart);
    startDateCal.set(Calendar.MINUTE, minStart);
    startDateCal.set(Calendar.SECOND, 0);

    Calendar endDateCal = Calendar.getInstance();
    endDateCal.set(Calendar.HOUR_OF_DAY, hourStop);
    endDateCal.set(Calendar.MINUTE, minStop);
    endDateCal.set(Calendar.SECOND, 0);

    this.startDate = startDateCal;
    this.endDate = endDateCal;
    logger.traceINFO("start time = " + startDate.getTime().toString());
    logger.traceINFO("end time = " + endDate.getTime().toString());
  }
Ejemplo n.º 3
0
 public void insertMarketData(String symbol, String price) {
   String query =
       "INSERT INTO `marketdata`.`market_data` (`ID`, `SYMBOL`, `PRICE`, `TIMESTAMP`) VALUES (NULL, '"
           + symbol
           + "', '"
           + price
           + "', CURRENT_TIMESTAMP);";
   logger.traceINFO("Inserting ( " + symbol + ", " + price + " )");
   try {
     connect.createStatement().execute(query);
   } catch (CommunicationsException e) {
     readDataBase();
     insertMarketData(symbol, price);
   } catch (Exception e) {
     logger.traceERROR(e);
   }
 }
Ejemplo n.º 4
0
public class DatabaseAccess {
  private Logger logger = Logger.getInstance();
  private Connection connect = null;
  private Statement statement = null;
  private ResultSet resultSet = null;

  public DatabaseAccess() {
    readDataBase();
  }

  public void insertMarketData(String symbol, String price) {
    String query =
        "INSERT INTO `marketdata`.`market_data` (`ID`, `SYMBOL`, `PRICE`, `TIMESTAMP`) VALUES (NULL, '"
            + symbol
            + "', '"
            + price
            + "', CURRENT_TIMESTAMP);";
    logger.traceINFO("Inserting ( " + symbol + ", " + price + " )");
    try {
      connect.createStatement().execute(query);
    } catch (CommunicationsException e) {
      readDataBase();
      insertMarketData(symbol, price);
    } catch (Exception e) {
      logger.traceERROR(e);
    }
  }

  private void readDataBase() {
    try {
      logger.traceINFO("Initializing database...");
      Class.forName("com.mysql.jdbc.Driver");
      connect =
          DriverManager.getConnection("jdbc:mysql://localhost/marketdata?" + "user=root&password=");
    } catch (Exception e) {
      logger.traceERROR(e);
      System.exit(0);
    }
  }

  public void close() {
    try {
      if (resultSet != null) {
        resultSet.close();
      }

      if (statement != null) {
        statement.close();
      }

      if (connect != null) {
        connect.close();
      }
    } catch (Exception e) {
      logger.traceERROR(e);
    }
  }
}
  public void flush() {
    String formatString = "| %55s | %9s | %18s |";
    // Mind that all rows have the same sizes.
    for (int i = 0; i < rows.size(); i++) {
      Row row = rows.get(i);
      String strOut = String.format(formatString, row.adTitle, row.adPrice, row.status);

      // First or last row
      if (i == 0) {
        Logger.traceINFO(org.apache.commons.lang3.StringUtils.repeat("_", strOut.length()));
      }

      Logger.traceINFO(strOut);

      if (i == rows.size() - 1) {
        Logger.traceINFO(org.apache.commons.lang3.StringUtils.repeat("_", strOut.length()));
      }
    }
  }
Ejemplo n.º 6
0
  private boolean checkBounds() {
    Calendar nowDate = Calendar.getInstance();

    if (startDate.get(Calendar.DATE) < nowDate.get(Calendar.DATE)) {
      // Increment day by one.
      startDate.add(Calendar.DATE, 1);
      endDate.add(Calendar.DATE, 1);
      logger.traceINFO("start time = " + startDate.getTime().toString());
      logger.traceINFO("end time = " + endDate.getTime().toString());
    }

    if (nowDate.getTime().after(startDate.getTime())
        && nowDate.getTime().before(endDate.getTime())) {
      return true;
    } else {
      long currentMs = nowDate.getTime().getTime();
      if (currentMs - lastCheckBoundsMessageMs > 5000) {
        lastCheckBoundsMessageMs = currentMs;
        logger.traceINFO("Out of dates bounds");
      }
      return false;
    }
  }
Ejemplo n.º 7
0
  public void close() {
    try {
      if (resultSet != null) {
        resultSet.close();
      }

      if (statement != null) {
        statement.close();
      }

      if (connect != null) {
        connect.close();
      }
    } catch (Exception e) {
      logger.traceERROR(e);
    }
  }
Ejemplo n.º 8
0
public class RealtimeTimer {
  private Logger logger = Logger.getInstance();
  private int timerInterval = 1000;
  private TimerListener listener = null;
  private boolean condition = true;
  private Thread timerThread = new Thread(new TimerTask(this));
  private Calendar startDate;
  private Calendar endDate;

  private class TimerTask implements Runnable {
    private RealtimeTimer timer;

    public TimerTask(RealtimeTimer timer) {
      this.timer = timer;
    }

    @Override
    public void run() {
      while (condition) {
        if (checkBounds() && listener != null) {
          listener.onTimerTick(timer);
        }

        if (timerInterval > 0) {
          try {
            Thread.sleep(timerInterval);
          } catch (InterruptedException e) {
            logger.traceERROR(e);
            condition = false;
          }
        }
      }
    }
  }

  public void start() {
    timerThread.start();
  }

  public void stop() {
    condition = false;
  }

  public RealtimeTimer(int timerInterval, int hourStart, int minStart, int hourStop, int minStop) {
    this.timerInterval = timerInterval;
    Calendar startDateCal = Calendar.getInstance();
    startDateCal.set(Calendar.HOUR_OF_DAY, hourStart);
    startDateCal.set(Calendar.MINUTE, minStart);
    startDateCal.set(Calendar.SECOND, 0);

    Calendar endDateCal = Calendar.getInstance();
    endDateCal.set(Calendar.HOUR_OF_DAY, hourStop);
    endDateCal.set(Calendar.MINUTE, minStop);
    endDateCal.set(Calendar.SECOND, 0);

    this.startDate = startDateCal;
    this.endDate = endDateCal;
    logger.traceINFO("start time = " + startDate.getTime().toString());
    logger.traceINFO("end time = " + endDate.getTime().toString());
  }

  public void setListener(TimerListener listener) {
    this.listener = listener;
  }

  long lastCheckBoundsMessageMs = System.currentTimeMillis();

  private boolean checkBounds() {
    Calendar nowDate = Calendar.getInstance();

    if (startDate.get(Calendar.DATE) < nowDate.get(Calendar.DATE)) {
      // Increment day by one.
      startDate.add(Calendar.DATE, 1);
      endDate.add(Calendar.DATE, 1);
      logger.traceINFO("start time = " + startDate.getTime().toString());
      logger.traceINFO("end time = " + endDate.getTime().toString());
    }

    if (nowDate.getTime().after(startDate.getTime())
        && nowDate.getTime().before(endDate.getTime())) {
      return true;
    } else {
      long currentMs = nowDate.getTime().getTime();
      if (currentMs - lastCheckBoundsMessageMs > 5000) {
        lastCheckBoundsMessageMs = currentMs;
        logger.traceINFO("Out of dates bounds");
      }
      return false;
    }
  }
}