- 浏览: 2539591 次
- 性别:
- 来自: 成都
文章分类
最新评论
-
nation:
你好,在部署Mesos+Spark的运行环境时,出现一个现象, ...
Spark(4)Deal with Mesos -
sillycat:
AMAZON Relatedhttps://www.godad ...
AMAZON API Gateway(2)Client Side SSL with NGINX -
sillycat:
sudo usermod -aG docker ec2-use ...
Docker and VirtualBox(1)Set up Shared Disk for Virtual Box -
sillycat:
Every Half an Hour30 * * * * /u ...
Build Home NAS(3)Data Redundancy -
sillycat:
3 List the Cron Job I Have>c ...
Build Home NAS(3)Data Redundancy
Java Monitor S3 File Update Time
I have a JAVA Spring Boot Project. The requirement is to monitor the file update time in S3.
Here is the pom.xml requirement pom.xml
<!-- AWS -->
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-s3</artifactId>
<version>1.11.251</version>
</dependency>
Here is the implementation Class S3ServiceImpl.java
package com.sillycat.jobsmonitorapi.service;
import java.util.Date;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.amazonaws.services.s3.model.ObjectListing;
import com.amazonaws.services.s3.model.S3ObjectSummary;
import com.sillycat.jobsmonitorapi.config.AWSConfig;
@Service
public class S3ServiceImpl implements S3Service {
private static final Logger logger = LoggerFactory.getLogger(S3ServiceImpl.class);
@Autowired
private AWSConfig awsConfig;
private AmazonS3 s3Client;
private void init() {
logger.info("start to init the s3Client--------");
this.s3Client = AmazonS3ClientBuilder.standard()
.withCredentials(new AWSStaticCredentialsProvider(
new BasicAWSCredentials(awsConfig.getKey(), this.awsConfig.getSecret())))
.withRegion(Regions.US_EAST_1).build();
logger.info("start to init the s3Client end--------");
}
public Date getFileLastUpdate(String bucketName, String path) {
if (this.s3Client == null) {
this.init();
}
Date result = null;
ObjectListing objectList = this.s3Client.listObjects(bucketName, path);
if (objectList != null) {
for (S3ObjectSummary fileSummary : objectList.getObjectSummaries()) {
String tmpKey = fileSummary.getKey();
logger.info("find files {}", tmpKey);
logger.info("file the file with last update {}", fileSummary.getLastModified());
result = fileSummary.getLastModified();
break;
}
}
return result;
}
}
Here is the config class under the config directory AWSConfig.java
package com.sillycat.jobsmonitorapi.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableConfigurationProperties
@ConfigurationProperties(prefix = "aws")
public class AWSConfig {
private String key;
private String secret;
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public String getSecret() {
return secret;
}
public void setSecret(String secret) {
this.secret = secret;
}
}
In side the application.yaml, we will have our configuration as follow:
aws:
key: xxxxxxx
secret: xxxxxxxxxxxx
Here is the UNIT test how it will pick up the configuration and work S3ServiceTest.java
package com.sillycat.jobsmonitorapi.service;
import java.util.Date;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
@ActiveProfiles("dev")
public class S3ServiceTest {
@Autowired
S3Service s3Service;
@Test
public void testDummy() {
Assert.assertTrue(true);
}
@Test
public void testLastModified() {
Date date = s3Service.getFileLastUpdate("j2c-production", "feed-2g/jobs_2g.txt.gz");
Assert.assertNotNull(date);
}
}
Here is the Util Class to deal with the time, I tried to use JAVA API 8 this time, no JODA Time
package com.sillycat.jobsmonitorapi.commons;
import java.time.LocalDateTime;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
public class DateTimeUtil {
private static final DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");
/**
*
* @param timeString
* @return
*/
public static DateTime parseFullTimeString(String timeString) {
DateTime dt = formatter.parseDateTime(timeString);
return dt;
}
/**
*
* @param date2
* time from S3
* @param hours
* @return
*/
public static Boolean isUpdateDeleyHours(LocalDateTime updateTime, int hours) {
if (updateTime.isBefore(LocalDateTime.now().minusHours(hours))) {
// S3 is older than now - hours, something wrong
return true;
} else {
return false;
}
}
}
Here is the unit class to handle the DateTimeUtil.java
package com.sillycat.jobsmonitorapi.commons;
import java.time.LocalDateTime;
import java.time.Month;
import java.time.ZoneId;
import java.util.Date;
import org.joda.time.DateTime;
import org.junit.Assert;
import org.junit.Test;
public class DateTimeUtilTest {
@Test
public void dummy() {
Assert.assertTrue(true);
}
@Test
public void parseFullTimeString() {
String timeString = "2017-11-02 10:55:37";
DateTime dt = DateTimeUtil.parseFullTimeString(timeString);
Assert.assertNotNull(dt);
}
@Test
public void isDeleyHours() {
LocalDateTime oldTime = LocalDateTime.of(2017, Month.JANUARY, 14, 19, 30);
Boolean result1 = DateTimeUtil.isUpdateDeleyHours(oldTime, 3);
Assert.assertTrue(result1);
Date now = new Date();
LocalDateTime nowDateTime = LocalDateTime.ofInstant(now.toInstant(), ZoneId.systemDefault());
Boolean result2 = DateTimeUtil.isUpdateDeleyHours(nowDateTime, 3);
Assert.assertFalse(result2);
}
}
References:
http://docs.aws.amazon.com/AmazonS3/latest/dev/RetrievingObjectUsingJava.html
http://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/com/amazonaws/services/s3/model/S3ObjectSummary.html
https://stackoverflow.com/questions/38578937/spring-boot-amazon-aws-s3-bucket-file-download-access-denied
I have a JAVA Spring Boot Project. The requirement is to monitor the file update time in S3.
Here is the pom.xml requirement pom.xml
<!-- AWS -->
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-s3</artifactId>
<version>1.11.251</version>
</dependency>
Here is the implementation Class S3ServiceImpl.java
package com.sillycat.jobsmonitorapi.service;
import java.util.Date;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.amazonaws.services.s3.model.ObjectListing;
import com.amazonaws.services.s3.model.S3ObjectSummary;
import com.sillycat.jobsmonitorapi.config.AWSConfig;
@Service
public class S3ServiceImpl implements S3Service {
private static final Logger logger = LoggerFactory.getLogger(S3ServiceImpl.class);
@Autowired
private AWSConfig awsConfig;
private AmazonS3 s3Client;
private void init() {
logger.info("start to init the s3Client--------");
this.s3Client = AmazonS3ClientBuilder.standard()
.withCredentials(new AWSStaticCredentialsProvider(
new BasicAWSCredentials(awsConfig.getKey(), this.awsConfig.getSecret())))
.withRegion(Regions.US_EAST_1).build();
logger.info("start to init the s3Client end--------");
}
public Date getFileLastUpdate(String bucketName, String path) {
if (this.s3Client == null) {
this.init();
}
Date result = null;
ObjectListing objectList = this.s3Client.listObjects(bucketName, path);
if (objectList != null) {
for (S3ObjectSummary fileSummary : objectList.getObjectSummaries()) {
String tmpKey = fileSummary.getKey();
logger.info("find files {}", tmpKey);
logger.info("file the file with last update {}", fileSummary.getLastModified());
result = fileSummary.getLastModified();
break;
}
}
return result;
}
}
Here is the config class under the config directory AWSConfig.java
package com.sillycat.jobsmonitorapi.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableConfigurationProperties
@ConfigurationProperties(prefix = "aws")
public class AWSConfig {
private String key;
private String secret;
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public String getSecret() {
return secret;
}
public void setSecret(String secret) {
this.secret = secret;
}
}
In side the application.yaml, we will have our configuration as follow:
aws:
key: xxxxxxx
secret: xxxxxxxxxxxx
Here is the UNIT test how it will pick up the configuration and work S3ServiceTest.java
package com.sillycat.jobsmonitorapi.service;
import java.util.Date;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
@ActiveProfiles("dev")
public class S3ServiceTest {
@Autowired
S3Service s3Service;
@Test
public void testDummy() {
Assert.assertTrue(true);
}
@Test
public void testLastModified() {
Date date = s3Service.getFileLastUpdate("j2c-production", "feed-2g/jobs_2g.txt.gz");
Assert.assertNotNull(date);
}
}
Here is the Util Class to deal with the time, I tried to use JAVA API 8 this time, no JODA Time
package com.sillycat.jobsmonitorapi.commons;
import java.time.LocalDateTime;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
public class DateTimeUtil {
private static final DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");
/**
*
* @param timeString
* @return
*/
public static DateTime parseFullTimeString(String timeString) {
DateTime dt = formatter.parseDateTime(timeString);
return dt;
}
/**
*
* @param date2
* time from S3
* @param hours
* @return
*/
public static Boolean isUpdateDeleyHours(LocalDateTime updateTime, int hours) {
if (updateTime.isBefore(LocalDateTime.now().minusHours(hours))) {
// S3 is older than now - hours, something wrong
return true;
} else {
return false;
}
}
}
Here is the unit class to handle the DateTimeUtil.java
package com.sillycat.jobsmonitorapi.commons;
import java.time.LocalDateTime;
import java.time.Month;
import java.time.ZoneId;
import java.util.Date;
import org.joda.time.DateTime;
import org.junit.Assert;
import org.junit.Test;
public class DateTimeUtilTest {
@Test
public void dummy() {
Assert.assertTrue(true);
}
@Test
public void parseFullTimeString() {
String timeString = "2017-11-02 10:55:37";
DateTime dt = DateTimeUtil.parseFullTimeString(timeString);
Assert.assertNotNull(dt);
}
@Test
public void isDeleyHours() {
LocalDateTime oldTime = LocalDateTime.of(2017, Month.JANUARY, 14, 19, 30);
Boolean result1 = DateTimeUtil.isUpdateDeleyHours(oldTime, 3);
Assert.assertTrue(result1);
Date now = new Date();
LocalDateTime nowDateTime = LocalDateTime.ofInstant(now.toInstant(), ZoneId.systemDefault());
Boolean result2 = DateTimeUtil.isUpdateDeleyHours(nowDateTime, 3);
Assert.assertFalse(result2);
}
}
References:
http://docs.aws.amazon.com/AmazonS3/latest/dev/RetrievingObjectUsingJava.html
http://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/com/amazonaws/services/s3/model/S3ObjectSummary.html
https://stackoverflow.com/questions/38578937/spring-boot-amazon-aws-s3-bucket-file-download-access-denied
发表评论
-
Update Site will come soon
2021-06-02 04:10 1672I am still keep notes my tech n ... -
Portainer 2020(4)Deploy Nginx and Others
2020-03-20 12:06 419Portainer 2020(4)Deploy Nginx a ... -
Private Registry 2020(1)No auth in registry Nginx AUTH for UI
2020-03-18 00:56 428Private Registry 2020(1)No auth ... -
Docker Compose 2020(1)Installation and Basic
2020-03-15 08:10 364Docker Compose 2020(1)Installat ... -
VPN Server 2020(2)Docker on CentOS in Ubuntu
2020-03-02 08:04 444VPN Server 2020(2)Docker on Cen ... -
Nginx Deal with OPTIONS in HTTP Protocol
2020-02-15 01:33 343Nginx Deal with OPTIONS in HTTP ... -
PDF to HTML 2020(1)pdftohtml Linux tool or PDFBox
2020-01-29 07:37 396PDF to HTML 2020(1)pdftohtml Li ... -
Elasticsearch Cluster 2019(2)Kibana Issue or Upgrade
2020-01-12 03:25 709Elasticsearch Cluster 2019(2)Ki ... -
Spark Streaming 2020(1)Investigation
2020-01-08 07:19 290Spark Streaming 2020(1)Investig ... -
Hadoop Docker 2019 Version 3.2.1
2019-12-10 07:39 288Hadoop Docker 2019 Version 3.2. ... -
MongoDB 2019(3)Security and Auth
2019-11-16 06:48 232MongoDB 2019(3)Security and Aut ... -
MongoDB 2019(1)Install 4.2.1 Single and Cluster
2019-11-11 05:07 284MongoDB 2019(1) Follow this ht ... -
Monitor Tool 2019(1)Monit Installation and Usage
2019-10-17 08:22 318Monitor Tool 2019(1)Monit Insta ... -
Ansible 2019(1)Introduction and Installation on Ubuntu and CentOS
2019-10-12 06:15 305Ansible 2019(1)Introduction and ... -
Timezone and Time on All Servers and Docker Containers
2019-10-10 11:18 326Timezone and Time on All Server ... -
Kafka Cluster 2019(6) 3 Nodes Cluster on CentOS7
2019-10-05 23:28 272Kafka Cluster 2019(6) 3 Nodes C ... -
K8S Helm(1)Understand YAML and Kubectl Pod and Deployment
2019-10-01 01:21 315K8S Helm(1)Understand YAML and ... -
Rancher and k8s 2019(5)Private Registry
2019-09-27 03:25 350Rancher and k8s 2019(5)Private ... -
Jenkins 2019 Cluster(1)Version 2.194
2019-09-12 02:53 437Jenkins 2019 Cluster(1)Version ... -
Redis Cluster 2019(3)Redis Cluster on CentOS
2019-08-17 04:07 365Redis Cluster 2019(3)Redis Clus ...
相关推荐
javaMonitor java 内存monitor. 报表分析
压缩包中的"724.FileMonitor__TheKingOfDuck"可能是FileMonitor的一个特定版本或者包含相关资源的文件夹。用户解压后,可以找到安装程序或文档资料,了解该版本的特性或使用方法。 7. **最佳实践与注意事项** ...
《FileMonitor:轻松实现文件操作监控的利器》 在日常的计算机使用中,我们经常需要跟踪和记录文件系统的活动,以便于排查问题、分析软件行为或保护数据安全。这时,一款高效的文件操作监控工具显得尤为重要。本文...
"file-monitor-file-watch.rar" 提供的工具或程序可能就是一个实现文件系统监控的解决方案。它关注于跟踪文件和目录的变更,如文件的修改、重命名、移动以及删除等操作。这种功能对于日志管理、数据同步、版本控制...
Java实现文件监控器FileMonitor的实例代码 Java实现文件监控器FileMonitor的实例代码是一种基于Java语言的文件监控机制,能够实时监控文件的变化,包括文件的创建、修改、删除等操作。该机制可以应用于各种文件系统...
Java 监视器模式(Java Monitor Pattern)是一种用于多线程环境中的设计模式,它主要依赖于Java语言的同步机制,如`synchronized`关键字和`wait()`, `notify()`等方法,来确保线程安全和资源的有序访问。在Java中,...
《FileMonitor 文件监控源代码详解》 在IT领域,文件监控是系统管理和开发中的重要环节,它能够帮助我们实时跟踪和记录文件系统的各种操作,如创建、修改、删除等。"FileMonitor" 是一个用于底层文件操作监控的工具...
Java Monitor Pattern设计模式是一种在多线程环境下控制资源访问的重要机制。Monitor Pattern源自于C++中的概念,但在Java中得到了特殊的支持。Java的Monitor Pattern主要依赖于synchronized关键字和对象的wait(), ...
"filemonitor"是一款用于文件夹动态实时监测的工具,它能够帮助用户监控指定目录下的文件变化情况,包括文件的创建、修改、删除等操作。这一功能在很多场景下都极其实用,比如在软件开发中跟踪代码变动、在数据备份...
**FileMonitor v1.09 访问文件监控器** FileMonitor是一款强大的系统工具,用于实时监控和记录计算机上文件和目录的访问、修改、创建和删除等操作。这款软件对于系统管理员、开发者以及普通用户来说都十分有用,...
《全面解析文件监控模块——基于FileMonitor1》 在IT领域,实时监控系统中的文件活动是一项重要的任务,尤其是在数据安全、日志分析和系统管理方面。本文将详细讲解一款名为"FileMonitor1"的文件监控模块,它允许...
Java Monitor Pattern设计模式是用于解决多线程环境下的并发访问问题的一种经典设计模式。它基于监视器对象(Monitor Object)的概念,确保在任一时间点只有一个线程能够访问特定的共享资源,从而实现线程安全。 **...
【fileMonitor】是一款专为Windows系统设计的工具,旨在模拟Linux中的`tail`命令功能,让用户在Windows环境下也能轻松地监控日志文件的变化。在Linux系统中,`tail`命令常用于查看文件的尾部内容,尤其适用于实时...
Java Monitor 机制使用方法解析 Java Monitor 机制是 Java 语言中的一种高级同步原语,用于解决多线程并发编程中的互斥问题。Monitor 机制的核心思想是使用锁和条件变量来管理线程的阻塞和唤醒,从而实现互斥的效果...
软件名称:File Monitor 软件大小:167KB 软件语言:简体中文 软件类别:系统辅助 运行环境:Win9x/Me/NT/2000/XP 软件介绍: File Monitor是一个出色系统文件监视工具,可以监视应用程序进行的文件读写操作,...
《FileMonitor:深度解析系统文件监视工具》 在IT领域,系统监控是确保软件稳定运行和排查问题的关键环节。其中,文件监视作为重要的一环,可以帮助开发者和管理员了解程序对文件系统的操作,从而定位潜在的问题。...
"monitor-changes-of-file-list.zip"这个压缩包文件似乎提供了一个工具或脚本来帮助用户监控文件目录的变化。下面我们将深入探讨文件监控的概念、应用场景以及实现方法。 文件监控,也称为文件变化监测或目录监测,...
Moo0 FileMonitor(系统文件监视器)可以让你轻松的监控到当前系统正在访问和改变的文件访问活动。 Moo0 FileMonitor更新 (1.03): - Added French language support. - Fixed a bug in which the start-up link name...
在Java Web和Java SE项目中,Druid Monitor是一个重要的工具,用于实时监控数据库连接池的状态,帮助开发者优化数据库访问性能,预防并定位数据库相关的问题。 首先,我们要理解Druid的核心功能。数据库连接池是一...
总的来说,`FileMonitor`是JDK7`WatchService`的一个优化版,它的出现使得Java开发者在处理文件监控时能更加高效和便捷。通过理解`FileMonitor`的工作原理和使用方式,开发者可以更好地利用这一工具,提升文件系统...