在企业级应用中,经常会制定一些“计划任务”
即在某个时间点做某件事情
中译: 石英,水晶; 现在常用于钟表的意思…
Quartz对任务调度进行了高度抽象,提出了3个核心概念:
并在org.quartz
包中通过接口和类进行了描述…
开发工具: Myelicps2014
这里选用Quartz 的1.8.6版本,此版本在项目中应用较多,也较为稳定
(需要朋友可以私聊…或自己找…)
对员工的工作任务进行提醒,实现每3秒钟进行一次任务提醒,定时器在10秒钟后关闭
Plan.java
public class Plan {
private String time;
private String task;
//get/set/toString();
}
业务逻辑层:主要就是执行大量业务的. 往后学习会发现service才是代码最多的…
天真的我最开始尽然认为 Service最简单,后来才发现它啥都干!!
PlanService.java
public class PlanService {
//模拟一个展示所有工作的场景业务...
public List<Plan> findPlanList() {
Plan plan1 = new Plan("09:00", "站立会");
Plan plan2 = new Plan("11:20", "需求探讨会");
List<Plan> list = new ArrayList<Plan>();
list.add(plan1);
list.add(plan2);
return list;
}
//指定用户输出,该用户惊天的一个定时任务!!
public void showPlan(String name) {
System.out.println(name + "的任务是:");
//调用上面的findPlanList(); 方法打印业务...
List<Plan> list = this.findPlanList();
for (Plan plan : list) {
System.out.println(plan.getTime() + "\t" + plan.getTask());
}
}
}
PlanTask.java
//任务类,主要完成执行任务的方法
public class PlanJob implements Job {
//实现Job接口实现execute();
//获取业务逻辑类对象
private PlanService planService = new PlanService();
@Override
//execute()方法,在实现类中, 实现该方法以执行具体任务
//通过参JobExecutionContext 可以获取调度上下文的各种信息,如:任务名称等
public void execute(JobExecutionContext context) throws JobExecutionException {
JobDataMap jobDataMap = context.getJobDetail().getJobDataMap();
//JobDataMap是用来在执行过程中存储必要的数据对象。JobDataMap实现了Java Map接口
String str = jobDataMap.get("username").toString(); //获取JobDataMap中存储的数据 put()|get()
//调用业务逻辑方法;
planService.showPlan(str);
}
}
RunTask.java
public class RunTask {
public void run() throws Exception {
//通过JobDetail创建一个任务实例 任务名 任务开发组 执行的任务类;
JobDetail jobDetail = new JobDetail("myJob", "myGroup", PlanJob.class);
//JobDetail.getJobDataMap(); 获取一个JobDataMap可存储一些数据方便后期使用...
JobDataMap jobDataMap = jobDetail.getJobDataMap();
jobDataMap.put("username", "张帆");
//使用SimpleTrigger触发器
//SimpleTrigger是Trigger的子类,用于创建固定时间间隔的触发规则: 触发器名,常量表示触发器间隔执行,3000毫秒
SimpleTrigger simpleTrigger = new SimpleTrigger("myTrigger",SimpleTrigger.REPEAT_INDEFINITELY, 3000);
//设置开始执行时间 当前系统时间 + 1000毫秒 =1秒)
simpleTrigger.setStartTime(new Date(System.currentTimeMillis() + 1000));
//创建一个调度器工厂
SchedulerFactory schedulerFactory = new StdSchedulerFactory();
Scheduler scheduler = schedulerFactory.getScheduler(); //工厂创建一个调度器;
scheduler.scheduleJob(jobDetail, simpleTrigger); //将:任务 和 触发器结合在一起;
//调度器启动...触发任务
scheduler.start();
Thread.sleep(10000); //主线程休眠1000毫秒,触发任务,是多线程执行,即使主线程休眠程序依旧会执行...
scheduler.shutdown(); //主线程启动,调度器关闭...(调度器不是多线程所以,触发任务被关闭程序结束...)
}
}
Test.java
public class Test {
public static void main(String[] args) throws Exception {
RunTask runTask = new RunTask();
runTask.run();
System.out.println("主程序结束");
}
}
PlanJob.java
//任务类,主要完成执行任务的方法
public class PlanJob implements Job {
//实现Job接口实现execute();
//获取业务逻辑类对象
private PlanService planService = new PlanService();
@Override
//execute()方法,在实现类中, 实现该方法以执行具体任务
//通过参JobExecutionContext 可以获取调度上下文的各种信息,如:任务名称等
public void execute(JobExecutionContext context) throws JobExecutionException {
JobDataMap jobDataMap = context.getJobDetail().getJobDataMap();
//JobDataMap是用来在执行过程中存储必要的数据对象。JobDataMap实现了Java Map接口
String str = jobDataMap.get("username").toString(); //获取JobDataMap中存储的数据 put()|get()
//新增
//execute(JobExecutionContext context)参数,可以获取该任务的名称、绑定的触发器的名称和每次任务触发时间
System.out.print("TriggerName(触发器): " + context.getTrigger().getName()
+ "&TriggerTime:" +new Date()+ "&JobName(任务名):" + context.getJobDetail().getName() + "\t");
//调用业务逻辑方法;
planService.showPlan(str);
}
}
CronTrigger也是Trigger的子类
触发器 | 应用场景 | 使用方式 |
---|---|---|
SimpleTrigger | 固定时间间隔的调度任务 | 通过设置触发器的属性:开始时间、结束时间、重复次数、重复间隔等 |
CronTrigger | 指定时间点的调度任务 | 通过定义 Cron表达式 |
CronTrigger允许用户更精准地控制任务的运行日期和时间,而不仅仅是定义工作的频度
CronTrigger通过Cron表达式定义准确的运行时间点。
CronTrigger cronTrig = new CronTrigger(“触发器名”, “组名”, “Cron表达式”);
Cron表达式由6~7个由空格分隔的时间元素组成。第7个元素可选
Cron表达式的每个字段,都可以显式地规定一个值
(如49)、一个范围(如1-6)、一个列表(如1,3,5)或者一个通配符(如)*
Cron表达式有几个特殊的字符,说明如下
符号 | 作用 |
---|---|
- | 中划线,表示一个范围 |
, | 使用逗号间隔的数据,表示一个列表 |
* | 表示每一个值,它可以用于所有字段。例如:在小时字段表示每小时 |
? | 该字符仅用于“月份中的哪一天”字段和“星期几”字段,表示不指定值 |
/ | 通常表示为x/y,x为起始值,y表示值的增量。 |
L | 表示最后一天,仅在日期和星期字段中使用 |
# | 只能用于“星期几”字段,表示这个月的第几个周几。例如:“6#3”指这个月第三个周五 |
RunTask .Java
public class RunTask {
public void run() throws Exception {
//通过JobDetail创建一个任务实例 任务名 任务开发组 执行的任务类;
JobDetail jobDetail = new JobDetail("myJob", "myGroup", PlanJob.class);
JobDataMap jobDataMap = jobDetail.getJobDataMap();
jobDataMap.put("username", "张帆");
//新触发器
//使用cron表达式指定执行时间 调用器名 开发组 指定执行时间(cron:每天16时16分20秒执行..)
CronTrigger trigger =new CronTrigger("myTrigger","myGroup","20 16 16 * * ?");
//创建一个调度器工厂
SchedulerFactory schedulerFactory = new StdSchedulerFactory();
Scheduler scheduler = schedulerFactory.getScheduler(); //工厂创建一个调度器;
scheduler.scheduleJob(jobDetail, trigger); //将:任务 和 触发器结合在一起;
//调度器启动...触发任务
scheduler.start();
//注释...不然主线程关闭..
//Thread.sleep(10000); //主线程休眠1000毫秒,触发任务,是多线程执行,即使主线程休眠程序依旧会执行...
//scheduler.shutdown(); //主线程启动,调度器关闭...(调度器不是多线程所以,触发任务被关闭程序结束...)
}
}
Spring对Quartz提供了支持
对Quartz的核心类进行了封装,使开发人员更便捷地实现任务调度
使用声明的方式配置计划任务,大大简化了操作步骤,而且也降低了代码耦合
通过扩展QuartzJobBean来创建Quartz任务
需要实现它的executeInternal()方法
通过配置JobDetailBean创建一个任务实例,并注入任务数据
通过配置SimpleTriggerBean创建触发器规则
通过配置SCronTriggerBean创建触发器 cron 规则
配置SchedulerFactoryBean注册任务和触发器
通过启动Spring容器启动任务调度
PlanTask.java
/*
任务类 extends QuartzJobBean 重写方法
executeInternal() 用来定义要执行的计划任务,并且通过该基类能够以属性的方式注入任务数据
*/
public class PlanTask extends QuartzJobBean {
//获取业务逻辑对象
private PlanService planService;
/**
* 要定时执行的内容
*/
@Override
protected void executeInternal(JobExecutionContext context)
throws JobExecutionException {
JobDataMap map = context.getJobDetail().getJobDataMap();
planService.showPlan(map.getString("username"));
}
//业务逻辑的Set方便Spring注入!!
public void setPlanService(PlanService planService) {
this.planService = planService;
}
}
Test.java
public class Test {
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
}
}
applicationContext.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<bean id="planService" class="com.wsm.service.PlanService">bean>
<bean id="jobDetailBean" class="org.springframework.scheduling.quartz.JobDetailBean">
<property name="jobClass" value="com.wsm.task.PlanTask" />
<property name="jobDataAsMap">
<map>
<entry key="username" value="张三丰">entry>
<entry key="planService" value-ref="planService">entry>
map>
property>
bean>
<bean id="tiggerBean" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="jobDetailBean">property>
<property name="cronExpression" value="10 30 16 * * ?">property>
bean>
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<ref bean="tiggerBean" />
property>
bean>
beans>
这个操作对于传统项目
来说是常见的操作!!建议了解学习
本人这里这是给案例, 深入还需自己了解…
jxl是一个韩国人写的java操作excel的工具
在开源世界中,有两套比较有影响的API可 供使用一个是POI,一个是jExcelAPI
jxl.jar,放入项目classpath,安装就完成了。 资源私
为了方便操作这里定一个pojo 实体类, 模拟Excel里的数据. 方便读写
要指定Java可是面向对象的, 万物皆可对象!!
把Exce里的每一行数据, 当作一个Java对象Student 学员管理系统
Student.java
public class Student {
private int id;
private String name;
private int age;
private String address;
public Student() {
super();
}
public Student(int id, String name, int age, String address) {
super();
this.id = id;
this.name = name;
this.age = age;
this.address = address;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
Writer.java
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import jxl.Workbook;
import jxl.format.Alignment;
import jxl.format.Border;
import jxl.format.BorderLineStyle;
import jxl.format.Colour;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
public class Writer {
public static void main(String[] args) throws Exception {
//自定义Student集合写入Excel
List<Student> list = new ArrayList<Student>();
list.add(new Student(1, "admin", 20, "南京"));
list.add(new Student(2, "test", 25, "上海"));
//打开文件,前提文件存在!!
//也可以直接用Java 在指定盘符创建一个Excel文件!!这里就不搞了!!
WritableWorkbook book = Workbook.createWorkbook(new File("D://test.xls"));
// 生成名为“第一页”的工作表,参数0表示这是第一页
WritableSheet sheet = book.createSheet(" 第一页 ", 0);
//数据字串格式化 字体为TIMES宋体,字号16,加粗显示
WritableFont font1 = new WritableFont(WritableFont.TIMES, 16,WritableFont.BOLD);
//WritableCellFormat类,这个类非常重要,通过它可以指定单元格的各种属性,后面的单元格格式化中会有更多描述。
WritableCellFormat format1 = new WritableCellFormat(font1);
// 把水平对齐方式指定为居中
format1.setAlignment(jxl.format.Alignment.CENTRE);
//合并第一列第一行到第4列第一行的所有单元格
//行列下标从 0 开始这里四个参数为
//起始列,起始行,结束列,结束行 的合并操作;
sheet.mergeCells(0, 0, 3, 0);
//在第一列第一行, 数据为 "学员管理系统" 采用format1格式... (这里的0列0行已将是四个单元的合并了...)
sheet.addCell(new Label(0, 0, "学员管理系统", format1)); //标题
//内容数据↓↓↓
//字体Arial 9号字体 加粗
WritableFont font2 =new WritableFont(WritableFont.ARIAL, 9, WritableFont.BOLD, false);;
WritableCellFormat titleFormat = new WritableCellFormat (font2);
//单元格黄色 田字边框 垂直居中对齐
titleFormat.setBackground(Colour.YELLOW);
titleFormat.setBorder(Border.ALL, BorderLineStyle.THIN);
titleFormat.setAlignment(Alignment.CENTRE);
//加入列数据!!
sheet.addCell(new Label(0, 1, "编号" ,titleFormat));
sheet.addCell(new Label(1, 1, "名称",titleFormat));
sheet.addCell(new Label(2, 1, "年龄",titleFormat));
sheet.addCell(new Label(3, 1, "地址",titleFormat));
//循环遍历集合生产内容...
for (int i = 0 ,row=2; i < list.size(); i++,row++) {
Student s = (Student)list.get(i);
sheet.addCell(new Label(0, row,s.getId()+"" ));
sheet.addCell(new Label(1, row,s.getName()+"" ));
sheet.addCell(new Label(2, row,s.getAge()+"" ));
sheet.addCell(new Label(3, row,s.getAddress()+"" ));
}
book.write();
book.close();
}
}
如果读一个excel,需要知道它有多少行和多少列,如下操作
Reader.java
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
public class Reader {
public static void main(String[] args) throws Exception{
//打开文件,前提文件存在!!
Workbook book = Workbook.getWorkbook( new File( "D://test.xls" ));
//获得第一个工作表对象, 一个Excelz中存在多个
Sheet sheet = book.getSheet(0);
//得到行数
int rows = sheet.getRows();
//int columnum = sheet.getColumns(); 得到列数
//双层循环遍历!
for (int i = 2; i < rows; i++) {
//因为行,列 下标0开始:而我们要从第三行读数据!!
//获取第 i 行数据,返回cell [] 单元格数组;
Cell [] cells = sheet.getRow(i);
for (int j = 0; j < cells.length; j++) {
//遍历单元格数组!!
Cell c = cells[j]; //获取每一个单元格;
System.out.print(c.getContents()+"\t"); //输出单元格数据!
}
System.out.println(); //一行结束换行输出!
}
}
}
ExcelAPI还有其他的一些功能,比如插入图片等,这里就不再一一介绍,读者可以自己探索
加油!! 奥利给!