利用java提供的timer类和timerTask类进行简单的定时任务操作(即排程任务)。
package com.linming.task;
import java.util.Timer;
/**
* @author lin ming
* @date 2012-4-11 下午08:23:41
* @description
*/
public class TimeTask {
/**
* @param args
*/
public static void main(String[] args) {
Timer timer = new Timer();
timer.schedule(new TestTask(), 1000);//程序运行一分钟后执行
// timer.schedule(new TestTask(), 1000, 1000);//程序运行一分钟后执行,且间隔一分钟后循环执行
// timer.schedule(new TestTask(), DateTime);//DateTime为设定的执行时间对象
// timer.schedule(new TestTask(), DateTime, time);//time为延迟时间
}
}
TestTask类
package com.linming.task;
import java.util.TimerTask;
/**
* @author lin ming
* @date 2012-4-11 下午08:25:41
* @description
*/
public class TestTask extends TimerTask {
@Override
public void run() {
System.out.println("定时任务在这里执行");
}
}
基于Spring配置的简单定时任务实现(这里提供核心源码)
applicationContext.xml
<bean id="emailTracker" class="com.linming.task.EmailTracker">
<property name="sendMail" ref="sendMail"/>
</bean>
<bean id="scheduledTask" class="org.springframework.scheduling.timer.ScheduledTimerTask">
<!-- wait 10 seconds before starting repeated execution -->
<property name="delay" value="0" />
<!-- run every 300 seconds -->
<property name="period" value="300000" />
<property name="timerTask" ref="emailTracker" />
</bean>
<bean id="timerFactory" class="org.springframework.scheduling.timer.TimerFactoryBean">
<property name="scheduledTimerTasks">
<list>
<ref bean="scheduledTask" />
</list>
</property>
</bean>
EmailTracker
public class EmailTracker extends TimerTask {
private SendMail sendMail;
@Override
public void run() {
// 发送邮件的具体实现
}
public SendMail getSendMail() {
return sendMail;
}
public void setSendMail(SendMail sendMail) {
this.sendMail = sendMail;
}
.....
}
再增加一个小示例,源码如下(直接运行能看到结果):
package com.pratise.test;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.util.Timer;
import java.util.TimerTask;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
/**
* @author F3226080
* @date Apr 13, 2012
*
*/
public class JTableDemo {
private Vector vector1 = new Vector();
// private Vector vector2 = new Vector();
private Vector vector3 = new Vector();
JTable student;
JScrollPane pane3;
JPanel panel;
JFrame frame;
int i = 1;//主要用于区别是否成功刷新了页面
public JTableDemo() {
vector1.addElement("提单号");
vector1.addElement("dd");
vector1.addElement("总重量");
vector1.addElement("总卷数");
vector1.addElement("客户名称");
vector1.addElement("有效开始时间");
vector1.addElement("有效结束时间");
vector1.addElement("备注");
}
public void createTable(){
student = new JTable (vector3,vector1);
student.setPreferredScrollableViewportSize(new Dimension(1000, 100));//设置表格的大小
student.setRowHeight (30);//设置每行的高度为20
student.setSelectionForeground (Color.black);//设置所选择行的前景色
student.setGridColor (Color.black);//设置网格线的颜色
student.setShowGrid (true);//是否显示网格线
student.setShowHorizontalLines (true);//是否显示水平的网格线
student.setShowVerticalLines (true);//是否显示垂直的网格线
student.doLayout ();//使表布局行与列
student.setBackground (Color.lightGray);//表格背景
pane3 = new JScrollPane (student);
panel = new JPanel (new GridLayout (0, 1));
panel.setPreferredSize (new Dimension (1000,400));
panel.add (pane3);
frame = new JFrame ("xxxxxxxxx");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
frame.setContentPane (panel);//将Panel加入窗口中
frame.setBounds(200, 200, 1000, 300);
frame.setVisible(true);
}
public static void main(String[] args) {
JTableDemo demo = new JTableDemo();
demo.createTable();
demo.excuteTimerTask();
}
public void excuteTimerTask(){
final Timer timer = new Timer();
TimerTask tt=new TimerTask() {
@Override
public void run() {
vector3.clear();
//你需要处理的查询结果
Vector vector2 = new Vector();
if(i==1) {
vector2.addElement("1021");
vector2.addElement("1021");
vector2.addElement("1021");
vector2.addElement("1021");
vector2.addElement("1021");
vector2.addElement("1021");
vector2.addElement("1021");
vector2.addElement("1021");
} else if(i==3){
vector2.addElement("1111111");
vector2.addElement("1111111");
vector2.addElement("1111111");
vector2.addElement("1111111");
vector2.addElement("1111111");
vector2.addElement("1111111");
vector2.addElement("1111111");
vector2.addElement("1111111");
} else{
vector2.addElement("2222");
vector2.addElement("222");
vector2.addElement("222");
vector2.addElement("22");
vector2.addElement("1111111");
vector2.addElement("1111111");
vector2.addElement("1111111");
vector2.addElement("1111111");
}
i++;
vector3.addElement(vector2);
student = new JTable (vector3,vector1);
pane3 = new JScrollPane (student);
frame.setContentPane(panel);
frame.validate();
}
};
timer.schedule(tt, 1000, 1000);
}
}