java定时器处理超时未支付订单

来源:互联网 发布:波司登羽绒服 知乎 编辑:程序博客网 时间:2024/04/28 04:10

1、核心代码
public class MyTimerTask implements ServletContextListener {private Timer timer = null;// 初始化监听器,创建实例,执行任务public void contextInitialized(ServletContextEvent event) {timer = new Timer();Timer timer = new Timer();timer.schedule(new TimerTask() {public void run() {Connection conn = null;ResultSet rs = null;PreparedStatement st = null;try {conn = DbUtil.getConnection();conn.setAutoCommit(false);java.util.Date date = new Date();System.out.println("开始查询超时未支付订单...");rs = DbUtil.getResultSet(conn,"select id, last_payment from mat_info where pay_status=0 and last_payment <'"+ date + "'");while (rs.next()) {System.out.println("timeout order id is"+ rs.getString("id"));st = conn.prepareStatement("update mat_info set room_id ='' where pay_status=0 and id='"+ rs.getString("id") + "'");st.execute();}conn.commit();} catch (Exception e) {System.out.println(e);try {conn.rollback();} catch (SQLException e1) {// TODO Auto-generated catch blocke1.printStackTrace();}e.printStackTrace();} finally {DbUtil.closeResultSet(rs);DbUtil.closeConnection(conn);}}}, 1000, 15000);}// 销毁监听器,停止执行任务public void contextDestroyed(ServletContextEvent event) {// 确保正在执行的任务是此计时器所执行的最后一个任务。timer.cancel();}}

2、配置

在web.xml里面添加如下配置

<listener>               <listener-class>            com.lcychina.utils.MyTimerTask       </listener-class>     </listener>  



原创粉丝点击