Beispiel #1
0
 private void loadCache() throws SQLException, IOException {
   Connection connection = null;
   PreparedStatement query = null;
   PreparedStatement insert = null;
   ResultSet rs = null;
   try {
     connection = dataSource.getConnection();
     query = connection.prepareStatement(SQL_GET_SEQNUMS);
     setSessionIdParameters(query, 1);
     rs = query.executeQuery();
     if (rs.next()) {
       cache.setCreationTime(SystemTime.getUtcCalendar(rs.getTimestamp(1)));
       cache.setNextTargetMsgSeqNum(rs.getInt(2));
       cache.setNextSenderMsgSeqNum(rs.getInt(3));
     } else {
       insert = connection.prepareStatement(SQL_INSERT_SESSION);
       int offset = setSessionIdParameters(insert, 1);
       insert.setTimestamp(offset++, new Timestamp(cache.getCreationTime().getTime()));
       insert.setInt(offset++, cache.getNextTargetMsgSeqNum());
       insert.setInt(offset, cache.getNextSenderMsgSeqNum());
       insert.execute();
     }
   } finally {
     JdbcUtil.close(sessionID, rs);
     JdbcUtil.close(sessionID, query);
     JdbcUtil.close(sessionID, insert);
     JdbcUtil.close(sessionID, connection);
   }
 }
Beispiel #2
0
 public void reset() throws IOException {
   setNextSenderMsgSeqNum(1);
   setNextTargetMsgSeqNum(1);
   messages.clear();
   creationTime = SystemTime.getUtcCalendar();
 }
Beispiel #3
0
/**
 * In-memory message store implementation.
 *
 * @see quickfix.MemoryStoreFactory
 */
public class MemoryStore implements MessageStore {
  private final HashMap<Integer, String> messages = new HashMap<>();
  private int nextSenderMsgSeqNum;
  private int nextTargetMsgSeqNum;
  private SessionID sessionID;
  private Calendar creationTime = SystemTime.getUtcCalendar();

  public MemoryStore() throws IOException {
    reset();
  }

  public MemoryStore(SessionID sessionID) {
    this.sessionID = sessionID;
  }

  public void get(int startSequence, int endSequence, Collection<String> messages)
      throws IOException {
    for (int i = startSequence; i <= endSequence; i++) {
      String message = this.messages.get(i);
      if (message != null) {
        messages.add(message);
      }
    }
  }

  /**
   * This method is here for JNI API consistency but it's not implemented. Use get(int, int,
   * Collection) with the same start and end sequence.
   */
  public boolean get(int sequence, String message) throws IOException {
    throw new UnsupportedOperationException("not supported");
  }

  public Date getCreationTime() throws IOException {
    return creationTime.getTime();
  }

  /* package */ void setCreationTime(Calendar creationTime) {
    this.creationTime = creationTime;
  }

  public int getNextSenderMsgSeqNum() throws IOException {
    return nextSenderMsgSeqNum;
  }

  public int getNextTargetMsgSeqNum() throws IOException {
    return nextTargetMsgSeqNum;
  }

  public void incrNextSenderMsgSeqNum() throws IOException {
    setNextSenderMsgSeqNum(getNextSenderMsgSeqNum() + 1);
  }

  public void incrNextTargetMsgSeqNum() throws IOException {
    setNextTargetMsgSeqNum(getNextTargetMsgSeqNum() + 1);
  }

  public void reset() throws IOException {
    setNextSenderMsgSeqNum(1);
    setNextTargetMsgSeqNum(1);
    messages.clear();
    creationTime = SystemTime.getUtcCalendar();
  }

  public boolean set(int sequence, String message) throws IOException {
    return messages.put(sequence, message) == null;
  }

  public void setNextSenderMsgSeqNum(int next) throws IOException {
    nextSenderMsgSeqNum = next;
  }

  public void setNextTargetMsgSeqNum(int next) throws IOException {
    nextTargetMsgSeqNum = next;
  }

  public void refresh() throws IOException {
    // IOException is declared to maintain strict compatibility with QF JNI
    final String text = "memory store does not support refresh!";
    if (sessionID != null) {
      Session session = Session.lookupSession(sessionID);
      session.getLog().onErrorEvent("ERROR: " + text);
    } else {
      LoggerFactory.getLogger(MemoryStore.class).error(text);
    }
  }
}