|
|
|
@ -533,29 +533,44 @@ public class DatabaseImpl implements Database {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void readQueue(String table, String channel, Consumer<String> consumer) throws SQLException {
|
|
|
|
|
public long probeQueue(String table, String channel) throws SQLException {
|
|
|
|
|
try (ResultSet resultSet = queueLength(connection, table, channel)) {
|
|
|
|
|
if (resultSet.next()) {
|
|
|
|
|
return resultSet.getLong(0);
|
|
|
|
|
} else {
|
|
|
|
|
return -1L;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void consumeQueue(String table, String channel, int limit, Consumer<String> consumer) throws SQLException {
|
|
|
|
|
List<Long> consumedKeys = new ArrayList<>();
|
|
|
|
|
try {
|
|
|
|
|
connection.setAutoCommit(false);
|
|
|
|
|
while (true) {
|
|
|
|
|
try (ResultSet resultSet = readNextFromQueue(connection, table, channel)) {
|
|
|
|
|
if (resultSet.next()) {
|
|
|
|
|
Long key = resultSet.getLong("key");
|
|
|
|
|
try {
|
|
|
|
|
if (consumer != null) {
|
|
|
|
|
consumer.accept(resultSet.getString("data"));
|
|
|
|
|
}
|
|
|
|
|
succeedInQueue(connection, table, key);
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
failInQueue(connection, table, key);
|
|
|
|
|
throw e;
|
|
|
|
|
try (ResultSet resultSet = readNextFromQueue(connection, table, channel, limit)) {
|
|
|
|
|
while (resultSet.next()) {
|
|
|
|
|
Long key = resultSet.getLong("key");
|
|
|
|
|
consumedKeys.add(key);
|
|
|
|
|
try {
|
|
|
|
|
if (consumer != null) {
|
|
|
|
|
consumer.accept(resultSet.getString("data"));
|
|
|
|
|
}
|
|
|
|
|
succeedInQueue(connection, table, key);
|
|
|
|
|
} catch (QueueException e) {
|
|
|
|
|
connection.rollback();
|
|
|
|
|
failInQueue(connection, table, key);
|
|
|
|
|
throw e;
|
|
|
|
|
}
|
|
|
|
|
} finally {
|
|
|
|
|
connection.commit();
|
|
|
|
|
}
|
|
|
|
|
} finally {
|
|
|
|
|
connection.commit();
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
connection.rollback();
|
|
|
|
|
for (Long key : consumedKeys) {
|
|
|
|
|
failInQueue(connection, table, key);
|
|
|
|
|
}
|
|
|
|
|
throw e;
|
|
|
|
|
} finally {
|
|
|
|
|
connection.setAutoCommit(true);
|
|
|
|
@ -585,10 +600,17 @@ public class DatabaseImpl implements Database {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private ResultSet readNextFromQueue(Connection connection, String table, String channel) throws SQLException {
|
|
|
|
|
private ResultSet queueLength(Connection connection, String table, String channel) throws SQLException {
|
|
|
|
|
PreparedStatement preparedStatement = connection.prepareStatement(flavor().queueLength(table));
|
|
|
|
|
preparedStatement.setString(1, channel);
|
|
|
|
|
return preparedStatement.executeQuery();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private ResultSet readNextFromQueue(Connection connection, String table, String channel, int limit) throws SQLException {
|
|
|
|
|
PreparedStatement preparedStatement = connection.prepareStatement(flavor().readNextFromQueue(table));
|
|
|
|
|
preparedStatement.setString(1, channel);
|
|
|
|
|
preparedStatement.setTimestamp(2, Timestamp.valueOf(LocalDateTime.now()));
|
|
|
|
|
preparedStatement.setInt(3, limit);
|
|
|
|
|
return preparedStatement.executeQuery();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|