public MessageGroup addMessageToGroup(Object groupId, Message<?> message) {
    final String groupKey = getKey(groupId);
    final String messageId = getKey(message.getHeaders().getId());
    boolean groupNotExist =
        jdbcTemplate.queryForInt(this.getQuery(Query.GROUP_EXISTS), groupKey, region) < 1;

    final Timestamp updatedDate = new Timestamp(System.currentTimeMillis());

    final Timestamp createdDate =
        groupNotExist
            ? updatedDate
            : jdbcTemplate.queryForObject(
                getQuery(Query.GET_GROUP_CREATED_DATE),
                new Object[] {groupKey, region},
                Timestamp.class);

    if (groupNotExist) {
      try {
        this.doCreateMessageGroup(groupKey, createdDate);
      } catch (DuplicateKeyException e) {
        logger.warn("Lost race to create group; attempting update instead", e);
        this.doUpdateMessageGroup(groupKey, updatedDate);
      }
    } else {
      this.doUpdateMessageGroup(groupKey, updatedDate);
    }

    this.addMessage(message);

    jdbcTemplate.update(
        getQuery(Query.CREATE_GROUP_TO_MESSAGE),
        new PreparedStatementSetter() {
          public void setValues(PreparedStatement ps) throws SQLException {
            if (logger.isDebugEnabled()) {
              logger.debug(
                  "Inserting message with id key="
                      + messageId
                      + " and created date="
                      + createdDate);
            }
            ps.setString(1, groupKey);
            ps.setString(2, messageId);
            ps.setString(3, region);
          }
        });
    return getMessageGroup(groupId);
  }
Esempio n. 2
0
 /**
  * Count the rows in the given table.
  *
  * @param jdbcTemplate the JdbcTemplate with which to perform JDBC operations
  * @param tableName table name to count rows in
  * @return the number of rows in the table
  */
 public static int countRowsInTable(JdbcOperations jdbcTemplate, String tableName) {
   return jdbcTemplate.queryForInt("SELECT COUNT(0) FROM " + tableName);
 }
 @ManagedAttribute
 public int messageGroupSize(Object groupId) {
   String key = getKey(groupId);
   return jdbcTemplate.queryForInt(getQuery(Query.COUNT_ALL_MESSAGES_IN_GROUP), key, region);
 }
 @Override
 @ManagedAttribute
 public int getMessageCountForAllMessageGroups() {
   return jdbcTemplate.queryForInt(getQuery(Query.COUNT_ALL_MESSAGES_IN_GROUPS), region);
 }
 @ManagedAttribute
 public long getMessageCount() {
   return jdbcTemplate.queryForInt(getQuery(Query.GET_MESSAGE_COUNT), region);
 }