daily pastebin goal
23%
SHARE
TWEET

Untitled

brcuce132 Mar 18th, 2019 92 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.sql.Connection;
  2. import java.sql.PreparedStatement;
  3. import java.sql.SQLException;
  4. import java.util.List;
  5. import java.sql.Timestamp;
  6.  
  7. import javax.sql.DataSource;
  8.  
  9. import org.apache.logging.log4j.LogManager;
  10. import org.apache.logging.log4j.Logger;
  11.  
  12. public class PPUCloudServiceUsageSink implements CloudServiceUsageSink {
  13.  
  14.     private static final String STATEMENT = "INSERT INTO product_usage_summary(timestamp, reporting_entity_id, user_id, product_line_code, feature_id, "
  15.             + "tokens, version, minor_version, source, agree_nbr, created_ts, last_updated_ts) "
  16.             + "VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) ON DUPLICATE KEY UPDATE tokens = tokens + ? , latest_updated_ts = ? ";
  17.     private static final Logger LOGGER = LogManager.getLogger(PPUCloudServiceUsageSink.class);
  18.     private final DataSource dataSource;
  19.     private final Resolver resolver;
  20.     private final UsageCache cache;
  21.  
  22.     public PPUCloudServiceUsageSink(DataSource dataSource, Resolver resolver, UsageCache cache) {
  23.         this.dataSource = dataSource;
  24.         this.resolver = resolver;
  25.         this.cache = cache;
  26.     }
  27.  
  28.  
  29.     @Override
  30.     public void saveUsage(List<CloudServiceUsage> events) throws Exception {
  31.         int batchLimit = 1000;
  32.         try (final Connection connection = dataSource.getConnection()) {
  33.             PreparedStatement psInsert = connection.prepareStatement(STATEMENT);
  34.             for (CloudServiceUsage event : events) {
  35.                
  36.                 if (!cache.alreadyProcessed(event)) {
  37.  
  38.                
  39.                     final Timestamp timestamp = event.getTimestamp();
  40.                     final String reporting_entity_id = event.getReportingEntityId();
  41.          
  42.                     try {
  43.                         psInsert.setTimestamp(1, timestamp);
  44.                         psInsert.setString(2, reporting_entity_id);
  45.                         psInsert.addBatch();
  46.                         batchLimit--;
  47.                     } catch (SQLException e) {
  48.                         ;
  49.                     } catch (Exception e) {
  50.                         ;
  51.                     }
  52.                 } else {
  53.                     LOGGER.debug("Already processed event: " + event);
  54.                 }
  55.                 if (batchLimit == 0) {
  56.                     psInsert.executeBatch();
  57.                     psInsert.clearBatch();
  58.                     batchLimit = 1000;
  59.                 }
  60.                 psInsert.clearParameters();
  61.             }
  62.             psInsert.executeBatch();
  63.         }
  64.  
  65.     }
  66. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top