Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.sql.SQLException;
- import java.util.List;
- import java.sql.Timestamp;
- import javax.sql.DataSource;
- import org.apache.logging.log4j.LogManager;
- import org.apache.logging.log4j.Logger;
- public class PPUCloudServiceUsageSink implements CloudServiceUsageSink {
- private static final String STATEMENT = "INSERT INTO product_usage_summary(timestamp, reporting_entity_id, user_id, product_line_code, feature_id, "
- + "tokens, version, minor_version, source, agree_nbr, created_ts, last_updated_ts) "
- + "VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) ON DUPLICATE KEY UPDATE tokens = tokens + ? , latest_updated_ts = ? ";
- private static final Logger LOGGER = LogManager.getLogger(PPUCloudServiceUsageSink.class);
- private final DataSource dataSource;
- private final Resolver resolver;
- private final UsageCache cache;
- public PPUCloudServiceUsageSink(DataSource dataSource, Resolver resolver, UsageCache cache) {
- this.dataSource = dataSource;
- this.resolver = resolver;
- this.cache = cache;
- }
- @Override
- public void saveUsage(List<CloudServiceUsage> events) throws Exception {
- int batchLimit = 1000;
- try (final Connection connection = dataSource.getConnection()) {
- PreparedStatement psInsert = connection.prepareStatement(STATEMENT);
- for (CloudServiceUsage event : events) {
- if (!cache.alreadyProcessed(event)) {
- final Timestamp timestamp = event.getTimestamp();
- final String reporting_entity_id = event.getReportingEntityId();
- try {
- psInsert.setTimestamp(1, timestamp);
- psInsert.setString(2, reporting_entity_id);
- psInsert.addBatch();
- batchLimit--;
- } catch (SQLException e) {
- ;
- } catch (Exception e) {
- ;
- }
- } else {
- LOGGER.debug("Already processed event: " + event);
- }
- if (batchLimit == 0) {
- psInsert.executeBatch();
- psInsert.clearBatch();
- batchLimit = 1000;
- }
- psInsert.clearParameters();
- }
- psInsert.executeBatch();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement