manojfdo

AWS Log Stream - Java

Apr 14th, 2018
780
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.99 KB | None | 0 0
  1. package au.com.itelasoft.iot;
  2.  
  3. import com.amazonaws.auth.AWSStaticCredentialsProvider;
  4. import com.amazonaws.auth.BasicAWSCredentials;
  5. import com.amazonaws.services.logs.AWSLogsClient;
  6. import com.amazonaws.services.logs.AWSLogsClientBuilder;
  7. import com.amazonaws.services.logs.model.DescribeLogStreamsRequest;
  8. import com.amazonaws.services.logs.model.DescribeLogStreamsResult;
  9. import com.amazonaws.services.logs.model.InputLogEvent;
  10. import com.amazonaws.services.logs.model.PutLogEventsRequest;
  11. import java.util.ArrayList;
  12. import java.util.List;
  13. import java.util.logging.Level;
  14. import java.util.logging.Logger;
  15.  
  16. public class AwsCloudWatch implements CloudLogger {
  17.    
  18.     private static volatile List<InputLogEvent> logEvents = new ArrayList();
  19.     private static AwsCloudWatch instance;
  20.     private static volatile Boolean inflight = false;
  21.     private Thread awsWriteThread;
  22.    
  23.     private final BasicAWSCredentials awsCreds;
  24.     private final AWSLogsClient cwClient;
  25.     private final String logStreamName = "access-log";
  26.     private final String logStreamGroup = "/test";
  27.  
  28.     public static AwsCloudWatch getInstance() {
  29.         synchronized (AwsCloudWatch.class) {
  30.             if (!(AwsCloudWatch.instance instanceof AwsCloudWatch)) {
  31.                 AwsCloudWatch.instance = new AwsCloudWatch();            
  32.             }
  33.         }
  34.         return AwsCloudWatch.instance;
  35.     }
  36.    
  37.     private AwsCloudWatch() {
  38.         this.awsCreds = new BasicAWSCredentials("xxxxxx", "xxxxxxxxxxxxxxxxxxxxxx");
  39.         this.cwClient = (AWSLogsClient) AWSLogsClientBuilder.standard()
  40.                         .withCredentials(new AWSStaticCredentialsProvider(this.awsCreds))
  41.                         .withRegion("ap-southeast-1").build();
  42.         this.initThread();
  43.     }
  44.    
  45.     private void initThread(){
  46.         this.awsWriteThread = new Thread(new Runnable() {
  47.             @Override
  48.             public synchronized void run() {
  49.                 try {
  50.                    
  51.                     while(true){
  52.                         writeToCloud();
  53.                     }
  54.  
  55.                 } catch (Exception e) {
  56.                     System.err.println("Error logging "+e.toString());
  57.                 }
  58.             }
  59.         });
  60.         this.awsWriteThread.setName("AWS Log writer thread");
  61.         this.awsWriteThread.start();
  62.     }
  63.  
  64.     @Override
  65.     public void write(String message) {
  66.         InputLogEvent logEvent = new InputLogEvent().withMessage(message).withTimestamp(System.currentTimeMillis());
  67.         AwsCloudWatch.logEvents.add(logEvent);
  68.         if(this.awsWriteThread.isAlive()){
  69.             synchronized(this.awsWriteThread){
  70.                 this.awsWriteThread.notify();
  71.             }
  72.         }else{
  73.             this.awsWriteThread.start();
  74.         }
  75.     }
  76.    
  77.     private synchronized void writeToCloud(){        
  78.         if(!AwsCloudWatch.logEvents.isEmpty() && AwsCloudWatch.inflight == false){
  79.             AwsCloudWatch.inflight = true;
  80.             PutLogEventsRequest putReq = new PutLogEventsRequest(this.logStreamGroup, this.logStreamName, logEvents)
  81.                 .withSequenceToken(this.getUploadSequenceToken());
  82.             AwsCloudWatch.logEvents.removeAll(putReq.getLogEvents());
  83.             this.cwClient.putLogEvents(putReq);
  84.             AwsCloudWatch.inflight = false;
  85.         }else{
  86.             if(AwsCloudWatch.inflight == false){
  87.                 try {
  88.                     synchronized(this.awsWriteThread){
  89.                         this.awsWriteThread.wait();
  90.                     }
  91.                 } catch (InterruptedException ex) {
  92.                     Logger.getLogger(AwsCloudWatch.class.getName()).log(Level.SEVERE, null, ex);
  93.                 }
  94.             }
  95.         }
  96.        
  97.     }
  98.  
  99.     private String getUploadSequenceToken() {
  100.         DescribeLogStreamsRequest req = new DescribeLogStreamsRequest().withLogGroupName(logStreamGroup).withLogStreamNamePrefix(logStreamName);
  101.         DescribeLogStreamsResult res = this.cwClient.describeLogStreams(req);
  102.         return res.getLogStreams().get(0).getUploadSequenceToken();
  103.     }
  104. }
Add Comment
Please, Sign In to add comment