SSH框架,xml配置实现动态定时器
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<!--注册定时执行任务实体-->
<!-- 日定时器 -->
<bean id="runkQuartz" class="com.idazui.lobby.quartz.runkQuartz">
<property name="daoRepo">
<ref bean="daoRepo"/>
</property>
</bean>
<!-- 注册定时器信息 -->
<bean id="taskInfo" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<!-- 防止定时任务冲突执行,延迟作用 -->
<property name="concurrent" value="false" />
<!-- 指定要执行的定时任务类 这里是runkQuartz -->
<property name="targetObject">
<ref local="runkQuartz"/>
</property>
<!-- 指定定时器任务类要执行的方法名称 这里是execute -->
<property name="targetMethod">
<value>run</value>
</property>
</bean>
<!-- 配置定时器任务的调度器 -->
<bean id="quartzTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<!-- 声明要运行的实体 -->
<property name="jobDetail">
<ref bean="taskInfo"/>
</property>
<!-- 设置运行时间 -->
<property name="cronExpression">
<!-- 每天零点-->
<value>0 0 0 * * ?</value>
<!-- 每1分钟执行一次-->
<!-- <value>0 */1 * * * ?</value> -->
</property>
</bean>
<!-- 周定时器 -->
<bean id="weekRunkQuartz" class="com.idazui.lobby.quartz.weekRunkQuartz">
<property name="daoRepo">
<ref bean="daoRepo"/>
</property>
</bean>
<bean id="weekinfo" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<!-- 防止定时任务冲突执行,延迟作用 -->
<property name="concurrent" value="false" />
<!-- 指定要执行的定时任务类 这里是runkQuartz -->
<property name="targetObject">
<ref local="weekRunkQuartz"/>
</property>
<!-- 指定定时器任务类要执行的方法名称 这里是execute -->
<property name="targetMethod">
<value>run</value>
</property>
</bean>
<bean id="weekrunk" class="org.springframework.scheduling.quartz.CronTriggerBean">
<!-- 声明要运行的实体 -->
<property name="jobDetail">
<ref bean="weekinfo"/>
</property>
<!-- 设置运行时间 -->
<property name="cronExpression">
<!-- 每周日零点 -->
<!-- <value>*/5 * * * * ?</value> -->
<value>0 0 0 ? * MON</value>
<!-- <value>0 0 0 ? * WED *</value> -->
<!-- <value>0 59 23 ? * WED</value> -->
</property>
</bean>
<!-- 月定时器 -->
<bean id="monthRunkQuartz" class="com.idazui.lobby.quartz.monthRunkQuartz">
<property name="daoRepo">
<ref bean="daoRepo"/>
</property>
</bean>
<bean id="monthinfo" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<!-- 防止定时任务冲突执行,延迟作用 -->
<property name="concurrent" value="false" />
<!-- 指定要执行的定时任务类 这里是runkQuartz -->
<property name="targetObject">
<ref bean="monthRunkQuartz"/>
</property>
<!-- 指定定时器任务类要执行的方法名称 这里是execute -->
<property name="targetMethod">
<value>run</value>
</property>
</bean>
<bean id="monthrunk" class="org.springframework.scheduling.quartz.CronTriggerBean">
<!-- 声明要运行的实体 -->
<property name="jobDetail">
<ref bean="monthinfo"/>
</property>
<!-- 设置运行时间 -->
<property name="cronExpression">
<!-- 相当于每月第一天零点-->
<!-- <value>0 0 0 L * ? *</value> -->
<!-- 每月最后一天23:59 -->
<!-- <value>0 59 23 L * ?</value> -->
<!-- 每月一号零点 -->
<value>0 0 0 1 * ?</value>
</property>
</bean>
<!-- 心跳监听定时器 -->
<bean id="tokenQuartz" class="com.idazui.lobby.quartz.validUserToken">
<property name="daoRepo">
<ref bean="daoRepo"/>
</property>
</bean>
<bean id="tokeninfo" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<!-- 防止定时任务冲突执行,延迟作用 -->
<property name="concurrent" value="false" />
<!-- 指定要执行的定时任务类 这里是runkQuartz -->
<property name="targetObject">
<ref bean="tokenQuartz"/>
</property>
<!-- 指定定时器任务类要执行的方法名称 这里是execute -->
<property name="targetMethod">
<value>run</value>
</property>
</bean>
<bean id="tokenrunk" class="org.springframework.scheduling.quartz.CronTriggerBean">
<!-- 声明要运行的实体 -->
<property name="jobDetail">
<ref bean="tokeninfo"/>
</property>
<!-- 设置运行时间 -->
<property name="cronExpression">
<!-- 每10秒执行一次-->
<value>0/10 * * * * ?</value>
</property>
</bean> <!-- 这个类用来做需要完成的业务-->
<bean id="doAutoPush" class="com.idazui.lobby.service.DoAutoPush">
<property name="daoRepo">
<ref bean="daoRepo"/>
</property>
</bean>
<!-- 定时任务 -->
<!-- 定义调用对象和调用对象的方法,这个配置和普通的一样的 -->
<bean id="jobtask" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<!-- 调用的类 -->
<property name="targetObject">
<ref bean="doAutoPush" />
</property>
<!-- 调用类中的方法 -->
<property name="targetMethod">
<value>autoPush</value>
</property>
<property name ="concurrent" value ="false" />
</bean>
<!-- 定义触发时间 ,这边就不同了,这里必须将时间设置成无限长,因为我们要去读取数据库的时间来做为定时器的触发时间-->
<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail">
<ref bean="jobtask" />
</property>
<!-- cron表达式 -->
<property name="cronExpression">
<!-- cron将时间设置成无限长 -->
<value>1 0 0 1 1 ? 2099</value>
</property>
</bean> <!-- 注册监听器 -->
<bean id="registerQuartz" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<!-- 注册定时器实体 集合 -->
<property name="triggers">
<list>
<ref bean="quartzTrigger"/>
<ref bean="weekrunk"/>
<ref bean="monthrunk"/>
<ref bean="tokenrunk"/>
<ref bean="cronTrigger"/>
</list>
</property>
</bean> <!--这里就是这里,就这个配置搞了我一天,这个类是用来设置触发时间的,调用这个类的init的方法来进行对时间的配置-->
<!--这个对象一定要注入,这样类才能进行管理,还有在类型要用get set方法,不然会报错。
<property name="scheduler" ref="registerQuartz" /> -->
<bean id="pushServiceImpl" class="com.idazui.lobby.service.DoAutoPushServiceImpl" lazy-init="false" init-method="init">
<property name="scheduler" ref="registerQuartz" />
</bean>
</beans>
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
import java.text.ParseException; import org.apache.log4j.Logger;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.springframework.scheduling.quartz.CronTriggerBean;
import org.springframework.stereotype.Service; /**
* 功能说明:设置下发定时时间service接口实现类
*
* 创建人:wyb
*
*
*/
public class DoAutoPushServiceImpl{
protected Logger log = Logger.getLogger(getClass().getName()); //这个是总管理类
private Scheduler scheduler; public void init(){ try { CronTriggerBean trigger = (CronTriggerBean) scheduler.getTrigger("cronTrigger", Scheduler.DEFAULT_GROUP);
String originConExpression = trigger.getCronExpression();
//以下几行用来测试时间用
/* Date date=new Date();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat rf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String str = "17:03:00";
String str2=df.format(date);
System.out.println(str2);
String bb=str2+" "+str;
Date shijian = rf.parse(bb);*/
//从fan()方法获取规定时间段内的随机时间
Date shijian=fan();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String strDate = format.format(shijian).substring(11, 19);
//因为是要每天做一次定时任务,所以我截取了字符串进行了拼接格式
String[] strTime = strDate.split(":");
String pushTime = strTime[2]+" "+strTime[1]+" "+strTime[0]+" * * ? ";
//以下就是重新对时间的设置,这样就可以达到动态设置效果。
trigger.setCronExpression(pushTime);
scheduler.rescheduleJob("cronTrigger", Scheduler.DEFAULT_GROUP, trigger);
} catch (SchedulerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} /* private String getCronExceptionDB() throws ParseException{
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
long timeNow= System.currentTimeMillis()/1000;
long timeLose=(System.currentTimeMillis()+2*60*60*1000)/1000;
//long timeLose=System.currentTimeMillis()+2*60*60*1000;
System.out.println(timeNow+" "+timeLose);
String dateNow=sdf.format(new Date((timeNow)*1000));
Date shijian=fan();
String ti=getCron(shijian);
return ti;
}*/
/***
*
* @param date
* @param dateFormat : e.g:yyyy-MM-dd HH:mm:ss
* @return
*/
public static String formatDateByPattern(Date date,String dateFormat){
SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
String formatTimeStr = null;
if (date != null) {
formatTimeStr = sdf.format(date);
}
return formatTimeStr;
}
/***
* convert Date to cron ,eg. "0 06 10 15 1 ? 2014"
* @param date : 时间点
* @return
*/
public String getCron(java.util.Date date){
String dateFormat="ss mm HH dd MM ? yyyy";
return formatDateByPattern(date, dateFormat);
}
public Date fan() throws ParseException{
/* Date date=new Date();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat rf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String str = "06:00:00";
String str2=df.format(date);
System.out.println(str2);
String bb=str2+" "+str;
Date today = rf.parse(bb); */
log.debug("进入随机时间方法");
Date today=timec();
log.debug("时间格式:"+today);
return today;
}
public Date timec() throws ParseException{
Date date=new Date();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat rf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//时间段开始
String str = "06:00:00";
String str2=df.format(date);
String bb=str2+" "+str;
Date one=rf.parse(bb);
long timeone=(one.getTime())/1000;
/* System.out.println(timeone);
System.out.println((timeone)/1000);*/
//时间段结束
String strone = "12:00:00";
String strtwo=df.format(date);
String two=strtwo+" "+strone;
Date newdate=rf.parse(two);
long timetwo=(newdate.getTime())/1000;
/* System.out.println(timetwo);
System.out.println((timetwo)/1000);*/
Random ra=new Random();
int onenum=(int)timetwo;
int twonum=(int)timeone;
int three=onenum-twonum;
/* System.out.println(onenum+" **");
System.out.println(twonum+" ^^");*/
int result=ra.nextInt(three)+twonum;
//System.out.println(result);
Date ndate=new Date((result)*1000L);
log.debug("最终获取时间为:"+ndate);
return ndate;
}
public Scheduler getScheduler() {
return scheduler;
} public void setScheduler(Scheduler scheduler) {
this.scheduler = scheduler;
} }
import java.io.File;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random; import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger; public class DoAutoPush {
private static Logger m_logger = Logger.getLogger(DoAutoPush.class);
private DaoRepo daoRepo;
public DaoRepo getDaoRepo() {
return daoRepo;
}
public void setDaoRepo(DaoRepo daoRepo) {
this.daoRepo = daoRepo;
}
public void autoPush(){
System.out.println("成功了");
} }
SSH框架,xml配置实现动态定时器的更多相关文章
- SSH框架中配置log4j的方法
SSH框架中使用log4j的方便之处 1. 动态的改变记录级别和策略,即修改log4j.properties,不需要重启Web应用,这需要在web.xml中设置一下.2. 把log文件定在 /WEB- ...
- Java web.xml 配置技巧—动态欢迎页地址
我们的 Java Web 项目在配置web.xml 欢迎页地址默认是index.html .index.jsp ,不知道有人注意过没有,如果我要配置成/index/user.action 或者 ...
- mybatis使用注解替代xml配置,动态生成Sql
mybatis使用注解替代xml配置时,遇到判断条件是否为null或者为空时,@Select很难搞定,不知道怎么办? mybatis3中增加了使用注解来配置Mapper的新特性,使用 SelectPr ...
- SSH框架项目配置和启动的加载顺序及请求的执行顺序
1:======配置和启动====== (1)配置web.xml 配置<context-param>,其中内容为Spring的配置文件applicationContext.xml.注意&l ...
- SSH框架整合配置所需JAR包(SSH整合)
转载于:http://www.cnblogs.com/kaige123/p/5719662.html Hibernate Jar: 1.hibernate3.jar,这个是hibernate3.0的核 ...
- SSH框架中配置Hibernate使用proxool连接池
一.导入proxool.jar包 案例用的是proxool-0.8.3.jar,一般通过MyEclipse配置的SSH都会包含这个jar,如果没有,就去网上搜下下载导入就好了. 二.新建Proxool ...
- SSH框架的配置
^_^阅读本文前请先浏览 : http://www.cnblogs.com/LiJinfu/p/5842890.html 步骤 : 一.编写web.xml配置文件 该文件路径在项目文件下的WebCon ...
- SSH框架 spring 配置中的: scope="prototype"
"可以利用容器的scope="prototype"来保证每一个请求有一个单独的Action来处理, 避免struts中Action的线程安全问题." 这句话怎么 ...
- Spring框架xml配置中属性ref与value的区别
1.spring批量扫描mybatis的mapper,使用value 2.spring管理mybatis的单个mapper,用的是ref 虽然引用的是同一个bean,但两个对象的属相类型明显不一样,一 ...
随机推荐
- SQLServer2005数据库快照的简单使用
原文:SQLServer2005数据库快照的简单使用 SQLServer2005数据库快照的简单使用 ...
- UWP入门(三) -- StackPanel与Grid的区别
原文:UWP入门(三) -- StackPanel与Grid的区别 ##1.Grid 下布局 <Grid Background="{ThemeResource ApplicationP ...
- 程序的开机关机重启,开机启动,休眠功能delphi实现(使用AdjustTokenPrivileges提升权限)
TShutDownStatus = (sdShutDown,sdReboot,sdLogOff,sdPowerOff); procedure ShutDown(sdStatus : TShutDown ...
- C++字符串的操作(简单全面)
void *memccpy (void *dest, const void *src, int c, size_t n); 从src所指向的对象复制n个字符到dest所指向的对象中.如果复制过程中遇到 ...
- QtScript, QML, Quick1, Quick2, Declarative 之间的关系
QtScript是基于 ECMAScript 的脚本语言 在脚本中可以访问原有C++代码中的QObject类型及其子类的实例,连接信号和槽:也可以创建QObject类型及其子类的实例. 但是QtScr ...
- Qt VS版本添加调试器
Qt的VS版本默认是不带调试器的,可以去百度一个WinDbg,如下图所示. 将其中的cdb.exe添加到Qt Creator构建和运行的Debuggers标签页即可,如下图所示. http://blo ...
- MySQL批量更新一个字段的值为随机数
$arr = []; $str = ''; for ($i=0; $i < 2660; ++$i) { $str .= " WHEN ".$i." THEN &qu ...
- 查看linux系统时间和时区
参考地址:http://lidao.blog.51cto.com/ 一.使用date命令查看系统时间 [root@benbang ~]# date -R Tue, 01 Aug 2017 15:43: ...
- 【React】react学习笔记03-React组件对象的三大属性-state
今天晚上学习了React中state的使用,特做此记录,对于学习的方式,博主仍然推荐直接复制完整代码,对着注释观察现象!: 上文中,我列举了两种React自定义组件的声明,这里我拿方式二进行举例: / ...
- 【linux杂记】Ubuntu查看端口使用情况
转载地址: https://www.linuxidc.com/Linux/2016-01/127345.htm Ubuntu查看端口使用情况,使用netstat命令: 查看已经连接的服务端口(ESTA ...