目录

1、项目背景

2、技术介绍

3、实现代码

4、程序演示

5、打成jar包


1、项目背景

最近在工作中碰到了一个问题,一个叫aura的系统每天都会接收到许多xml,其中有些xml会包含错误信息,这些包含错误信息的xml如果不及时删除并重新导入正确的xml的话,会导致系统中的其他xml无法正常入库。由于每天如果人工处理的话工作量比较大,而且也影响aura系统的日常处理运行,因此开发了一个基于Spring+Quartz+Dom4j实现一个小程序,用来每天处理那些错误的xml,并启动相应的程序重新导入这些xml。

2、技术介绍

Spring:用来降低代码的耦合性,提高程序的执行效率

Quartz:设置定时任务,让程序每天跑一次

Dom4j:修改xml文件

3、实现代码

FileHelper.java

package Tools;

import java.io.*;
import java.util.ArrayList;
import java.util.List; /**
* @description:
* @author: wu linchun
* @time: 2021/1/31 17:28
*/ public class FileHelper {
//读取文件中的内容
public List<String> readFile(File file) throws IOException {
BufferedReader reader = null;
StringBuffer sbf = new StringBuffer();
List<String> ssr = new ArrayList<String>();
reader = new BufferedReader(new FileReader(file));
String tempStr;
while ((tempStr = reader.readLine()) != null) {
ssr.add(tempStr);
}
reader.close();
return ssr;
}
}

FoldHelper.java

package Tools;

import java.io.File;
import java.util.ArrayList;
import java.util.List; /**
* @description:
* @author: wu linchun
* @time: 2021/1/31 19:17
*/ public class FoldHelper { /**
* 获取某个文件夹里面的所有文件名
* @param file
* @return
*/
public String[] readDirectory(File file) {
//List<String> fileName_list=new ArrayList<String>();
String[] fileName_arr = new String[file.list().length];
if (file.isDirectory()) {
fileName_arr = file.list();
}
return fileName_arr;
}
}

ConfigHelper.java

package Tools;

import org.dom4j.*;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter; import java.io.*;
import java.util.ArrayList;
import java.util.List; /**
* @description:
* @author: wu linchun
* @time: 2021/2/1 20:17
*/ public class ConfigHelper {
private FileHelper fileHelper; public void setFileHelper(FileHelper fileHelper) {
this.fileHelper = fileHelper;
} /**
* 修改xml中的指定节点的值
*
* @param url
* @param flag
* @param defaultFolderlocation
* @throws DocumentException
* @throws IOException
*/
public void setConfig(String url, String flag, String defaultFolderlocation) throws DocumentException, IOException {
SAXReader reader = new SAXReader();
Document document = reader.read(new File(url));
Element root = document.getRootElement(); // 获取第一个节点
Element flagNode = (Element) root.elements("Flag").get(0);
Element defaultFolderlocationNode = (Element) root.elements("DefaultFolderlocation").get(0); // 设置<flag>节点的值
flagNode.setText(flag); defaultFolderlocationNode.setText(defaultFolderlocation); // 格式化输出流,同时指定编码格式。也可以在FileOutputStream中指定。
OutputFormat format = OutputFormat.createPrettyPrint();
format.setEncoding("utf-8"); XMLWriter writer = new XMLWriter(new FileOutputStream(url), format);
writer.write(document);
writer.close(); }
}

Deal.java

package Tools;

import java.io.File;
import java.io.IOException;
import java.util.List; /**
* @description:
* @author: wu linchun
* @time: 2021/1/31 19:31
*/ public class Deal { private FileHelper fileHelper;
private FoldHelper foldHelper; public void setFileHelper(FileHelper fileHelper) {
this.fileHelper = fileHelper;
} public void setDirectoryHelper(FoldHelper foldHelper) {
this.foldHelper = foldHelper;
} /**
* 找出文件名以“_U_output.xml”结尾的,且xml内含有"insured is null"的xml并将其删除
*
* @param file
* @throws IOException
*/
public void deal_file(File file) throws IOException {
String[] fileName_arr = foldHelper.readDirectory(file);
for (String ss : fileName_arr) {
if (ss.contains("_U_output.xml")) {
String file_path = file.getAbsolutePath() + "\\" + ss;
System.out.println(file_path);
File target_file = new File(file_path);
List<String> testList = fileHelper.readFile(target_file);
if (fileHelper.readFile(target_file).get(1).contains(" insured is null")) {
target_file.delete();
System.out.println("delete" + " " + file_path);
}
}
}
}
}

MyJob.java

package MyJob;

import Tools.ConfigHelper;
import Tools.Deal;
import org.dom4j.DocumentException;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.context.support.ClassPathXmlApplicationContext; import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date; /**
* @description: 设置定时任务要执行的工作
* @author: wu linchun
* @time: 2021/1/31 20:48
*/ public class MyJob implements Job { @Override
public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean.xml");
Deal deal = (Deal) applicationContext.getBean("deal");
ConfigHelper configHelper = (ConfigHelper) applicationContext.getBean("confighelper"); SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
Date date = new Date();
String fold = String.valueOf(sdf.format(date));
File file = new File("D:\\aura\\mlc\\xml\\state\\mlc_bak\\" + fold);
try {
deal.deal_file(file);
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(fold);
//System.out.println("<Flag>修改为False");
String url = "D:\\aura\\mlc_ExtractDecision\\config\\config.xml"; //把<Flag>节点改为false,并修改要跑的文件夹
try {
configHelper.setConfig(url, "False", "D:/aura/mlc/xml/state/mlc_bak/" + fold);
} catch (DocumentException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("<Flag>修改为False");
System.out.println("<DefaultFolderlocation>文件夹修改为" + fold); //执行ExtractDecision.bat
String cmd = "cmd /c start D:\\aura\\mlc_ExtractDecision\\ExtractDecision.bat";
try {
Runtime.getRuntime().exec("cmd /c start D:\\aura\\mlc_ExtractDecision\\ExtractDecision.bat");
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("执行了ExtractDecision.bat");
//把<Flag>节点改为True
try {
configHelper.setConfig(url, "True", "D:/aura/mlc/xml/state/mlc_bak/" + fold);
} catch (DocumentException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("<Flag>修改为True");
}
}

Run.java

import MyJob.MyJob;
import Tools.Deal;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext; import java.io.File;
import java.io.IOException; /**
* @description: 启动类
* @author: wu linchun
* @time: 2021/1/31 19:07
*/ public class Run {
public static void main(String[] args) throws IOException, SchedulerException {
//1、调度器(Schedular),从工厂中获取调度实例(默认:实例化new StdSchedulerFactory();)
Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
//2、任务实例(JobDetail)
JobDetail jobDetail = JobBuilder.newJob(MyJob.class) //加载任务类,与HelloJob完成绑定,要求HelloJob实现Job接口
.withIdentity("job1", "group1") //参数1:任务的名称(唯一实例);参数2:任务组的名称
.build();
//3、触发器(Trigger)
Trigger trigger = TriggerBuilder.newTrigger()
.withIdentity("trigger1", "group1") //参数1:触发器的名称(唯一实例);参数2:触发器组的名称
.startNow() //马上启动触发器
.withSchedule(SimpleScheduleBuilder.repeatSecondlyForever(15)) //这里的15为每15秒执行一次
.build();
//让调度器关联任务和触发器,保证按照触发器定义的条件执行任务
scheduler.scheduleJob(jobDetail, trigger);
//启动
scheduler.start();
}
}

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>org.example</groupId>
<artifactId>aura_monitor</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.2.RELEASE</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>commons-dbutils</groupId>
<artifactId>commons-dbutils</artifactId>
<version>1.4</version>
</dependency>
<!--核心包-->
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.3.2</version>
</dependency>
<!--工具-->
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz-jobs</artifactId>
<version>2.3.2</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.7</version>
<scope>compile</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/dom4j/dom4j -->
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>1.6.1</version>
</dependency> </dependencies> <build>
<finalName>dscApp</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<!-- JAR Maven 管理-->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<archive>
<manifest>
<!-- 配置主程序 java -jar 默认Class -->
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<!--Main程序入口-->
<mainClass>Run</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<!-- maven 打包集成插件 -->
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
<configuration>
<descriptorRefs>
<!-- 将依赖一起打包到 JAR -->
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<!-- 配置主程序 java -jar 默认Class -->
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<!--Main程序入口-->
<mainClass>Run</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
</project>

bean.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="foldHelper" class="Tools.FoldHelper" scope="prototype"></bean> <bean id="fileHelper" class="Tools.FileHelper" scope="prototype"></bean> <bean id="deal" class="Tools.Deal" scope="prototype">
<property name="directoryHelper" ref="foldHelper"></property>
<property name="fileHelper" ref="fileHelper"></property>
</bean> <bean id="confighelper" class="Tools.ConfigHelper" scope="prototype">
<property name="fileHelper" ref="fileHelper"></property>
</bean> </beans>

4、程序演示

先在MyJob.java中打一个断点,方便一步一步演示程序

5、打成jar包

在pom.xml中添加打包

 <build>
<finalName>dscApp</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<!-- JAR Maven 管理-->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<archive>
<manifest>
<!-- 配置主程序 java -jar 默认Class -->
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<!--Main程序入口-->
<mainClass>Run</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<!-- maven 打包集成插件 -->
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
<configuration>
<descriptorRefs>
<!-- 将依赖一起打包到 JAR -->
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<!-- 配置主程序 java -jar 默认Class -->
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<!--Main程序入口-->
<mainClass>Run</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>

依赖(注意:因为是maven项目,所有相应导入的依赖也要一起打进jar包)

使用 java -jar 命令启动jar包

最新文章

  1. CSS系列:CSS3新增选择器
  2. 转 用C API 操作MySQL数据库
  3. Linux权限
  4. arguments.callee 调用自身 caller,callee,apply and call
  5. jquery+css3实现3d滚动旋转
  6. WPF三种基本触发器与【与或】逻辑触发器
  7. 【概念笔记】JAVA基础 - part1
  8. pthread多线程编程的学习小结
  9. C#调用MySql
  10. 2014 IGF 评选(转)
  11. Android eclipse下数据开源框架GreenDao的配置
  12. 【足迹C++primer】表达式求值
  13. 通用型CRM还是行业型CRM?-定制为王
  14. MySQL 架构
  15. Django-restframework 之频率源码分析
  16. Python 目录指引
  17. mysql怎么修改密码
  18. 收集JavaScript中常用的方法函数
  19. idea整合SVN以及SVN的使用
  20. 输入5个学生的信息(包括学号,姓名,英语成绩,计算机语言成绩和数据库成绩), 统计各学生的总分,然后将学生信息和统计结果存入test.txt文件中

热门文章

  1. 研一入坑Go 文件操作
  2. 华为设备配置telnet远程登陆命令
  3. 在vue中的form表单中下拉框中的数据来自数据库查询到的数据
  4. 独立按键控制led灯
  5. 畅联云平台(www.24hlink.cn)支持的用传列表
  6. redis五种数据结构详解
  7. 【Devexpress】Gridcontorl的列隐藏后再显示位置发生了变化
  8. 关于python实现html转word(docx)
  9. HCIE Routing&amp;Switching之MPLS静态LSP配置
  10. Blazor Server完美实现Cookie Authorization and Authentication