定时器

jiangtan273405 2010-09-15 11:45:08
我在一个页面存了信息和设置了时间(可以存多个信息和设置多个时间的)。我现在希望做一个每隔10分钟就扫描一下那个设置时间是否有到时(里面肯定不可能只有一个而是多个)。到时了就触发其他的事件,没到时间就继续每隔10分钟扫描。
望各位大虾帮帮忙发下代码给小弟参考下。谢谢了。
在线等待中······
...全文
127 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
bastengao 2010-09-18
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 flagiris 的回复:]

引用 3 楼 flagiris 的回复:
Java code

public static void main(String ss[]) {
ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
Runnable doThing = new Runnable() {
pub……

前几天……
[/Quote]
++1
zhaoqiang4153 2010-09-18
  • 打赏
  • 举报
回复
web.xml设置监听MySmsListener
zhaoqiang4153 2010-09-18
  • 打赏
  • 举报
回复
package com.jx.sms.tools;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimerTask;

/**
* 定时发送处理
*
* @author Administrator
*
*/
public class MyTask extends TimerTask {

public void run() {
// System.out.println("call at " + (new Date()));
Connection conn = DataBase_local.getConn();
Statement stmt = null;
ResultSet rs = null;
try {
stmt = conn.createStatement();
rs = stmt.executeQuery("select * from unsendsms");
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
java.util.Calendar c1 = java.util.Calendar.getInstance();
java.util.Calendar c2 = java.util.Calendar.getInstance();
String d1 = df.format(new Date());

while (rs.next()) {
String d2 = rs.getString("sendTime");// 获取短信的发送时间
c1.setTime(df.parse(d1));
c2.setTime(df.parse(d2));
int result = c1.compareTo(c2);// 比较大小
if (result == 0 || result > 0) {
SMS.send(rs.getString("telphone") + ",", rs
.getString("messageContent"), rs.getInt("smsType"));
// 发送待发短信

Statement stmt1 = conn.createStatement();
stmt1.execute("delete from unsendsms where id='"
+ rs.getInt("id") + "'");// 发送完成后删除待发短信
stmt1.close();
}
}
} catch (Exception e) {
e.printStackTrace();
System.out.println("call at " + (new Date()) + " database error");
return;
} finally {
try {
rs.close();
stmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return;
}

}
}

}

package com.jx.sms.tools;

import java.util.Timer;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

public class MySmsListener implements ServletContextListener {

private Timer timer = null;

public void contextInitialized(ServletContextEvent event) {
timer = new Timer(true);
// 设置任务计划,启动和间隔时间
timer.schedule(new MyTask(), 0, 600000);
}

public void contextDestroyed(ServletContextEvent event) {
timer.cancel();
}

}
sunjigen803 2010-09-17
  • 打赏
  • 举报
回复
用监听
zn85600301 2010-09-17
  • 打赏
  • 举报
回复
SPRING 有支持的定时器 可以百度下
aliks 2010-09-17
  • 打赏
  • 举报
回复
我 的博客:
http://lialiks.bokee.com/6335210.html
呵呵
WANGYQ_412 2010-09-15
  • 打赏
  • 举报
回复
利用timer或其他的定时框架建立定时任务!
jiangtan273405 2010-09-15
  • 打赏
  • 举报
回复
我想用spring + quartz来实现。但因为没用过,谁可以发个示例来给我参考下啊!
cw_tkong 2010-09-15
  • 打赏
  • 举报
回复
苹果的小弟 2010-09-15
  • 打赏
  • 举报
回复
写个 线程 线程里面执行你的逻辑
配置 web.xml 监听线程
菖蒲老先生 2010-09-15
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 flagiris 的回复:]
Java code

public static void main(String ss[]) {
ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
Runnable doThing = new Runnable() {
pub……
[/Quote]
前几天刚学到的,希望能有用。。。
菖蒲老先生 2010-09-15
  • 打赏
  • 举报
回复

public static void main(String ss[]) {
ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
Runnable doThing = new Runnable() {
public void run() { System.out.println("dddd"); }
};
scheduler.scheduleAtFixedRate(doThing, 0, 3, TimeUnit.SECONDS);


}

81,095

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧