我在网上看了下关于定时任务的代码。
如下:
本例依据Java自身提供的接口实现,通过监听器(Listener)和定时器(Timer)定时执行某个任务(Task)。
专业的开源工具可参考Quartz:http://www.opensymphony.com/quartz/MyListener:
import java.util.Timer;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;public class MyListener implements ServletContextListener {
private Timer timer = null; public void contextInitialized(ServletContextEvent event) {
timer = new Timer(true);
//设置任务计划,启动和间隔时间
timer.schedule(new MyTask(), 0, 86400000);
} public void contextDestroyed(ServletContextEvent event) {
timer.cancel();
}
}
MyTask:
import java.util.TimerTask;public class MyTask extends TimerTask { public void run() {
// System.out.println("call at " + (new Date()));
// TODO 此处添加具体任务代码
}}
web.xml配置:
<listener>
<listener-class>com.fastunit.samples.listener.MyListener</listener-class>
</listener>我想问的是,如果我的项目是个java项目。怎么配置这个监听?
引入了spring框架
如下:
本例依据Java自身提供的接口实现,通过监听器(Listener)和定时器(Timer)定时执行某个任务(Task)。
专业的开源工具可参考Quartz:http://www.opensymphony.com/quartz/MyListener:
import java.util.Timer;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;public class MyListener implements ServletContextListener {
private Timer timer = null; public void contextInitialized(ServletContextEvent event) {
timer = new Timer(true);
//设置任务计划,启动和间隔时间
timer.schedule(new MyTask(), 0, 86400000);
} public void contextDestroyed(ServletContextEvent event) {
timer.cancel();
}
}
MyTask:
import java.util.TimerTask;public class MyTask extends TimerTask { public void run() {
// System.out.println("call at " + (new Date()));
// TODO 此处添加具体任务代码
}}
web.xml配置:
<listener>
<listener-class>com.fastunit.samples.listener.MyListener</listener-class>
</listener>我想问的是,如果我的项目是个java项目。怎么配置这个监听?
引入了spring框架
解决方案 »
- jsp访问action
- 用j2ee的deployment tool部署时出错
- 大家好,我用的是struts架构,在action执行到最后return mapping.findForward("FORWARD");时,我还想在浏览器中保留当前页面,不想转向到
- 200分求两台服务器之间高效通讯的架构或方法
- java 找不到符号
- 急问:Hibernate3中获得查询子集出错
- J2EE新手,怎么编译不了?问题简单,好拿分!----------在线等
- webwork与struts的区别?另求webwork中文学习资料
- 求ANT 介绍文章
- Spring clould 注册成eureka服务端启动报错
- Quartz定时器
- 如何用STRING给MYBATIS配置文件传值(在线等,着急)
quartz怎么写一个自己的配置文件?
commons-collections-3.2.jar
commons-logging-1.0.4.jar
commons-pool.jar
jta-1.1.jar
quartz-1.6.0.jar
spring.jar
2,java代码package com;import java.util.Date;public class YouObject
{
public void youMethod()
{
System.out.println(new Date()+":每隔一分钟执行一次");
}
}3,java代码
applicationContext.xml<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<!-- <property name="autoStartup" value="false"/> 自动转为手动-->
<bean
class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list><!-- 在测试时,除非自己模块测试可以开启,其它模块不要自己开启 -->
<ref bean="cronTrigger" />
</list>
</property>
<property name="quartzProperties">
<props>
<prop key="org.quartz.threadPool.threadCount">1</prop>
</props>
</property>
</bean>
<!-- 下面是每格一秒做的线程-->
<bean id="cronTrigger"
class="org.springframework.scheduling.quartz.CronTriggerBean">
<!-- 知道要执行的对象 -->
<property name="jobDetail" ref="jobDetailBean" />
<!-- 下面是设置运行时间,详细见工程中doc文件下的文档
<property name="cronExpression" value="* * * * * ?" />
-->
<property name="cronExpression" value="0 * 14 * * ?" />
</bean>
<!--
<bean id="jobDetailBean"
-->
<bean id="jobDetailBean"
class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<!-- 指定要执行的对象-->
<property name="targetObject" ref="youObject" />
<!--指定要执行的对象的方法 -->
<property name="targetMethod" value="youMethod" />
</bean>
<bean id="youObject" class="com.YouObject"></bean>
</beans>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/*.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<!-- acegi -->
<listener>
<listener-class>
org.springframework.web.util.Log4jConfigListener
</listener-class>
</listener>
</web-app>
在你的myeclipse中写入以上代码后,发布工程就可以了,希望对你有帮助
没有web.xml 这才是问题。
你在web.xml 中配置
<listener>
<listener-class>com.fastunit.samples.listener.MyListener</listener-class>
</listener>
表示你启动web 服务器的时候,会实例化对象com.fastunit.samples.listener.MyListener,并且调用
public void contextInitialized(ServletContextEvent event) {
timer = new Timer(true);
//设置任务计划,启动和间隔时间
timer.schedule(new MyTask(), 0, 86400000);
}
这个方法的代码。如果你想实现同样的效果的话,只需要在写个一main 方法在里面,并且创建com.fastunit.samples.listener.MyListener对象,然后调用contextInitialized(null)方法即可。