监听器、定时器
来源:互联网 发布:重庆seo外包服务商 编辑:程序博客网 时间:2024/05/29 13:18
1. 建立 监听类 SalesPaymentListener
package com.eas.kingdee.salespayment.listener;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.ServletContextListener;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.web.context.support.WebApplicationContextUtils;
import com.eas.kingdee.salespayment.runner.SalesPaymentRunnerManager;
/**
*
* @author youzhen_qin
*
*/
public class SalesPaymentListener implements ServletContextListener{
*
* @author youzhen_qin
*
*/
public class SalesPaymentListener implements ServletContextListener{
private static WebApplicationContext webApplicationContext;
private static ApplicationContextHelper helper = new ApplicationContextHelper();
@Override
public void contextDestroyed(ServletContextEvent arg0) {
private static ApplicationContextHelper helper = new ApplicationContextHelper();
@Override
public void contextDestroyed(ServletContextEvent arg0) {
}
@Override
public void contextInitialized(ServletContextEvent arg0) {
webApplicationContext = WebApplicationContextUtils.getRequiredWebApplicationContext(arg0.getServletContext());
helper.setApplicationContext(webApplicationContext);
//run 定时器
SalesPaymentRunnerManager.initRunner(arg0);
}
public void contextInitialized(ServletContextEvent arg0) {
webApplicationContext = WebApplicationContextUtils.getRequiredWebApplicationContext(arg0.getServletContext());
helper.setApplicationContext(webApplicationContext);
//run 定时器
SalesPaymentRunnerManager.initRunner(arg0);
}
}
2. 定时器类
package com.eas.kingdee.salespayment.runner;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import javax.servlet.ServletContextEvent;
import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContext;
import com.eas.kingdee.salespayment.listener.ApplicationContextHelper;
import com.eas.kingdee.salespayment.task.SalesPaymentTask;
import com.eas.kingdee.salespayment.task.SalesPaymentTask;
public class SalesPaymentRunnerManager {
private static final Logger logger = Logger.getLogger(SalesPaymentRunnerManager.class);
public static final void initRunner(ServletContextEvent applicationContext) {
logger.info("initRunner.");
ApplicationContextHelper helper = new ApplicationContextHelper();
final ApplicationContext context = helper.getContext();
new Thread(){
public void run() {
logger.info("saledaily xt push thread started.");
while(true){
try {
long inteval = 1*60L * 60L * 1000L; //1 小时跑一次
sleep(inteval);
// if(betweenTime("19:00:00","23:50:00")){
if(betweenTime("18:00:00","18:02:00")){
SalesPaymentTask task = new SalesPaymentTask();
boolean pushSuccess = task.pushMsg(context); //具体执行的业务操作
logger.info("pushSuccess-----:"+pushSuccess);
}
} catch (Exception e1) {
logger.error("定时推送通知任务失败", e1);
return ;
}
}
}
}.start();
}
public static boolean betweenTime(String beginStr,String endStr) throws ParseException{
DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Calendar cal = Calendar.getInstance();
//今天
Date thisTime = cal.getTime();
String thisStr = format.format(thisTime); //当前时间
beginStr = thisStr.substring(0,11)+beginStr; //开始时间
endStr = thisStr.substring(0,11)+endStr; //
//第二天
Date beginDate = format.parse(beginStr);
Date endDate = format.parse(endStr);
if(thisTime.after(beginDate) && thisTime.before(endDate)){
return true;
}else{
return false;
}
}
public static final void stopRunner() {
private static final Logger logger = Logger.getLogger(SalesPaymentRunnerManager.class);
public static final void initRunner(ServletContextEvent applicationContext) {
logger.info("initRunner.");
ApplicationContextHelper helper = new ApplicationContextHelper();
final ApplicationContext context = helper.getContext();
new Thread(){
public void run() {
logger.info("saledaily xt push thread started.");
while(true){
try {
long inteval = 1*60L * 60L * 1000L; //1 小时跑一次
sleep(inteval);
// if(betweenTime("19:00:00","23:50:00")){
if(betweenTime("18:00:00","18:02:00")){
SalesPaymentTask task = new SalesPaymentTask();
boolean pushSuccess = task.pushMsg(context); //具体执行的业务操作
logger.info("pushSuccess-----:"+pushSuccess);
}
} catch (Exception e1) {
logger.error("定时推送通知任务失败", e1);
return ;
}
}
}
}.start();
}
public static boolean betweenTime(String beginStr,String endStr) throws ParseException{
DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Calendar cal = Calendar.getInstance();
//今天
Date thisTime = cal.getTime();
String thisStr = format.format(thisTime); //当前时间
beginStr = thisStr.substring(0,11)+beginStr; //开始时间
endStr = thisStr.substring(0,11)+endStr; //
//第二天
Date beginDate = format.parse(beginStr);
Date endDate = format.parse(endStr);
if(thisTime.after(beginDate) && thisTime.before(endDate)){
return true;
}else{
return false;
}
}
public static final void stopRunner() {
}
}
}
3. web.xml 配置
<listener>
<listener-class>
com.eas.kingdee.salespayment.listener.SalesPaymentListener
</listener-class>
</listener>
0 0
- 监听器、定时器
- 定时器,监听器
- 定时器Timer&监听器Listener
- web监听器、定时器的应用
- web监听器、定时器的应用
- 简单应用定时器和监听器
- web监听器Listener结合定时器Timer
- 监听器实现定时任务(java定时器)
- 监听器加定时器完成自动运行的任务
- java 监听器ServletContextListener,服务启动加载及定时器
- javaWeb监听器结合定时器实现定时执行任务
- node.js + MongoDB + AngularJS - 4 事件、监听器、定时器、回调
- java 定时器和一些监听器的 理解,不是原创
- JavaWeb中定时器随着容器启动运行(包含监听器)
- 监听器
- 监听器
- 监听器
- 监听器
- HDU4907Task schedule(并查集)
- Nginx配置文件详细说明
- cmd杀死端口线程
- 递归的陷阱
- 农历两百年算法(1901~2100)【C语言代码】
- 监听器、定时器
- 生活的宁静 生活的踏实
- 为什么网站需要https?
- JVMS Specification(3)-The class File Format
- avro学习入门一
- 统计文件内容行数
- xampp3.2 apache 配置多端口 多域名
- 麦蒂自曝8月曾同科比合练 未来有望复出再战NBA
- 三大框架ssh中导入导出EXCEL数据