标签:lombok 压缩 应用程序 针对 nba 文件的 image 数据展现 file
1 Elastic-Job快速入门
1.1 环境搭建
1.1.1.版本要求
JDK要求1.7及以上版本
Maven要求3.0.4及以上版本
zookeeper要求采用3.4.6及以上版本
1.1.2.Zookeeper安装&运行
https://archive.apache.org/dist/zookeeper/ 下载某版本Zookeeper,并解压。
安装可以查看博客:https://www.cnblogs.com/dalianpai/p/12057064.html
1.1.3.创建maven工程
创建maven工程elastic-job-quickstart,并导入以下依赖:
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <!-- https://mvnrepository.com/artifact/com.dangdang/elastic-job-lite-core --> <dependency> <groupId>com.dangdang</groupId> <artifactId>elastic-job-lite-core</artifactId> <version>2.1.5</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
1.2 代码实现
1.2.1.编写定时任务类
此任务在每次执行时获取一定数目的文件,进行备份处理,由File实体类的backedUp属性来标识该文件是否已备
份
public class FileBackupJob implements SimpleJob { //每次任务执行要备份文件的数量 private final int FETCH_SIZE = 1; //文件列表(模拟) public static List<FileCustom> files = new ArrayList<>(); //任务执行代码逻辑 @Override public void execute(ShardingContext shardingContext) { System.out.println("作业分片:"+shardingContext.getShardingItem()); //获取未备份的文件 List<FileCustom> fileCustoms = fetchUnBackupFiles(FETCH_SIZE); //进行文件备份 backupFiles(fileCustoms); } /** * 获取未备份的文件 * @param count 文件数量 * @return */ public List<FileCustom> fetchUnBackupFiles(int count){ //获取的文件列表 List<FileCustom> fileCustoms = new ArrayList<>(); int num=0; for(FileCustom fileCustom:files){ if(num >=count){ break; } if(!fileCustom.getBackedUp()){ fileCustoms.add(fileCustom); num ++; } } System.out.printf("time:%s,获取文件%d个\n", LocalDateTime.now(),num); return fileCustoms; } /** * 文件备份 * @param files */ public void backupFiles(List<FileCustom> files){ for(FileCustom fileCustom:files){ fileCustom.setBackedUp(true); System.out.printf("time:%s,备份文件,名称:%s,类型:%s\n", LocalDateTime.now(),fileCustom.getName(),fileCustom.getType()); } } }
文件实体类如下:
@Data public class FileCustom { /** * 标识 */ private String id; /** * 文件名 */ private String name; /** * 文件类型,如text、image、radio、vedio */ private String type; /** * 文件内容 */ private String content; /** * 是否已备份 */ private Boolean backedUp = false; public FileCustom(String id,String name,String type,String content){ this.id = id; this.name = name; this.type = type; this.content = content; } }
public class JobMain { //zookeeper端口 private static final int ZOOKEEPER_PORT = 2181; //zookeeper链接字符串 localhost:2181 private static final String ZOOKEEPER_CONNECTION_STRING = "192.168.180.113:" + ZOOKEEPER_PORT; //定时任务命名空间 private static final String JOB_NAMESPACE = "elastic-job-example-java"; //执行启动任务 public static void main(String[] args) { //制造一些测试数据 generateTestFiles(); //配置注册中心 CoordinatorRegistryCenter registryCenter = setUpRegistryCenter(); //启动任务 startJob(registryCenter); } //zk的配置及创建注册中心 private static CoordinatorRegistryCenter setUpRegistryCenter(){ //zk的配置 ZookeeperConfiguration zookeeperConfiguration = new ZookeeperConfiguration(ZOOKEEPER_CONNECTION_STRING, JOB_NAMESPACE); //减少zk超时时间 zookeeperConfiguration.setSessionTimeoutMilliseconds(100); //创建注册中心 CoordinatorRegistryCenter zookeeperRegistryCenter = new ZookeeperRegistryCenter(zookeeperConfiguration); zookeeperRegistryCenter.init(); return zookeeperRegistryCenter; } //任务的配置和启动 private static void startJob(CoordinatorRegistryCenter registryCenter){ //String jobName 任务名称, String cron 调度表达式, int shardingTotalCount 作业分片数量 JobCoreConfiguration jobCoreConfiguration = JobCoreConfiguration.newBuilder("fiels-job", "0/3 * * * * ?", 3).build(); //创建SimpleJobConfiguration SimpleJobConfiguration simpleJobConfiguration = new SimpleJobConfiguration(jobCoreConfiguration, FileBackupJob.class.getCanonicalName()); //创建new JobScheduler new JobScheduler(registryCenter, LiteJobConfiguration.newBuilder(simpleJobConfiguration).overwrite(true).build()).init(); } //制造一些测试数据 //生成测试文件 private static void generateTestFiles(){ for(int i=1;i<11;i++){ FileBackupJob.files.add(new FileCustom(String.valueOf(i+10),"文件"+(i+10),"text","content"+ (i+10))); FileBackupJob.files.add(new FileCustom(String.valueOf(i+20),"文件"+(i+20),"image","content"+ (i+20))); FileBackupJob.files.add(new FileCustom(String.valueOf(i+30),"文件"+(i+30),"radio","content"+ (i+30))); FileBackupJob.files.add(new FileCustom(String.valueOf(i+40),"文件"+(i+40),"video","content"+ (i+40))); } System.out.println("生产测试数据完成"); } }
1.2.3.测试
(1)启动main方法查看控制台