目录

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包

Spring+Quartz+Dom4j实现一个小项目的更多相关文章

  1. 用struts2标签如何从数据库获取数据并在查询页面显示。最近做一个小项目,需要用到struts2标签从数据库查询数据,并且用迭代器iterator标签在查询页面显示,可是一开始,怎么也获取不到数据,想了许久,最后发现,是自己少定义了一个变量,也就是var变量。

    最近做一个小项目,需要用到struts2标签从数据库查询数据,并且用迭代器iterator标签在查询页面显示,可是一开始,怎么也获取不到数据,想了许久,最后发现,是自己少定义了一个变量,也就是var变 ...

  2. Spring MVC 学习笔记2 - 利用Spring Tool Suite创建一个web 项目

    Spring MVC 学习笔记2 - 利用Spring Tool Suite创建一个web 项目 Spring Tool Suite 是一个带有全套的Spring相关支持功能的Eclipse插件包. ...

  3. 跟我一起用node-express搭建一个小项目(mongodb)[二]

    我的小项目主要是会用到MongoDB. 呵呵,我也是现学现卖. 都说小公司十八般武艺样样稀疏,没有办法啊! 兵来兵挡,将来将挡!自己是个兵呢?还是一个将呢! 没有公司培养,就自己培养自己呗.差的远一点 ...

  4. 【源码项目+解析】C语言/C++开发,打造一个小项目扫雷小游戏!

    一直说写个几百行的小项目,于是我写了一个控制台的扫雷,没有想到精简完了代码才200行左右,不过考虑到这是我精简过后的,浓缩才是精华嘛,我就发出来大家一起学习啦,看到程序跑起来能玩,感觉还是蛮有成就感的 ...

  5. spring boot的一个小项目小型进销存系统

    项目所需的依赖: <dependency> <groupId>org.springframework.boot</groupId> <artifactId&g ...

  6. iOS 模仿一个小项目,总结一下里边的模块

      ManoBoo:  参考链接:http://www.jianshu.com/p/fd4c46c31508  这个小的项目是参考ManoBoo的简书的,链接在上方,自己在仿做的过程中,也离不开Man ...

  7. Winform窗体用对象数组做一个小项目

    首先我我们看一下需求:我们要做的是显示员工信息,实现项目经理给员工评分的功能! 首先项目经理是评分的人所以没有用,因为我们自己写,评分的就是我们自己.所以我们要做的是先在vs也就是我们的环境里建一个项 ...

  8. 基于Asp.net core + EF + Sqlite 5分钟快速上手一个小项目

    虽然该方法不会用在实际开发中,但该过程对于初学者还是非常友好的,真应了麻雀虽小,五脏俱全这句话了.好了不多废话了,直接开始!! 1.建立一个名为test的Asp.net core web应用程序 这一 ...

  9. 麻雀虽小,五脏俱全。基于Asp.net core + Sqlite 5分钟快速上手一个小项目

    虽然该方法不会用在实际开发中,但该过程对于初学者还是非常友好的,真应了麻雀虽小,五脏俱全这句话了.好了不多废话了,直接开始!! 1.建立一个名为test的Asp.net core web应用程序 这一 ...

随机推荐

  1. 如何编写 Pipeline 脚本

    前言 Pipeline 编写较为麻烦,为此,DataKit 中内置了简单的调试工具,用以辅助大家来编写 Pipeline 脚本. 调试 grok 和 pipeline 指定 pipeline 脚本名称 ...

  2. 『现学现忘』Git后悔药 — 34、git commit --amend 命令

    目录 1.git commit --amend 命令说明 2.使用场景 (1)场景一 (2)场景二 3.git commit --amend 命令原理 这是我们Git中的第三种后悔药. 1.git c ...

  3. 齐博x1商业模块仅限一个国际域名使用

    应用市场的所有商业模块 仅授权一个国际域名,大家不要试图复制到其它国际域名下使用. 仅支持一个国际域名使用,二级域名不限,但前提需要先用 www.开头的国际域名先安装,然后再到二级域名安装,并且二级域 ...

  4. LcdTools如何编写MIPI指令(初始化代码)

    在LcdTools帮助文档中查看MIPI读写指令描述,如下图 编写LCM初始化代码就是配置LCM Driver IC寄存器值,一般只需用MipiWrite()指令写参数即可:下面介绍MipiWrite ...

  5. 题解 UVA439 骑士的移动 Knight Moves

    前言 最近板子题刷多了-- 题意 一个 \(8\times 8\) 的棋盘,问马从起点到终点的最短步数为多少. \(\sf Solution\) 要求最短路径嘛,显然 bfs 更优. 读入 这个读入处 ...

  6. Centos镜像下载

    1.进入官网,并点击下图所示的红框(alternative downloads) 官网网址:https://www.centos.org/download/  2.在往下翻,可以看到如下图的历史版本, ...

  7. javaWEB中的四种域对象

    javaWEB中的四种域对象 (1)ServletContext ServletContext是最大的Web域对象,在整个工程内有效,可以存储一些需要全局部署的配置文件,也可以存储其他信息,不过因为它 ...

  8. 常用类.String类

    package 常用类.String;import java.util.Arrays;import java.util.Locale;public class demo01 { public stat ...

  9. .NET周报【11月第1期 2022-11-07】

    国内文章 开源·安全·赋能 - .NET Conf China 2022 https://mp.weixin.qq.com/s/_tYpfPeQgyEGsnR4vVLzHg .NET Conf Chi ...

  10. Python学习之实例3

    一.文字读取并打印拼接字符串 1 with open('G:\python\char.txt') as f: #使用open()函数以只读模式打开文件 2 s=f.read() #使用read()方法 ...