spring定时器

来源:互联网 发布:网络攻防技术pdf 编辑:程序博客网 时间:2024/05/01 18:34
package edu.xjtu.sei.timer;import java.net.HttpURLConnection;import java.net.URL;import java.util.List;import java.util.TimerTask;import edu.xjtu.sei.skyeye.state.dao.BotWallDAO;import edu.xjtu.sei.skyeye.state.po.BotWall;public class BotWallTimer extends TimerTask {private BotWallDAO botWallDAO;public void setBotWallDAO(BotWallDAO botWallDAO) {this.botWallDAO = botWallDAO;}@Overridepublic void run() {List<BotWall> list=botWallDAO.findAll();for (int i = 0; i < list.size(); i++) {BotWall bw=list.get(i);String ip=bw.getIp();String  urlStr = "http://"+ip+":9066";boolean resule=exists(urlStr);if(resule){botWallDAO.timerMassage(ip, 1);}else {botWallDAO.timerMassage(ip, 0);}}} static boolean exists(String URLName) {       try {           //设置此类是否应该自动执行 HTTP 重定向(响应代码为 3xx 的请求)。           HttpURLConnection.setFollowRedirects(false);           //到 URL 所引用的远程对象的连接           HttpURLConnection con = (HttpURLConnection) new URL(URLName)                  .openConnection();           /* 设置 URL 请求的方法, GET POST HEAD OPTIONS PUT DELETE TRACE 以上方法之一是合法的,具体取决于协议的限制。*/           con.setRequestMethod("HEAD");           //从 HTTP 响应消息获取状态码           return (con.getResponseCode() == HttpURLConnection.HTTP_OK);       } catch (Exception e) {           return false;        }    }}

一直通过http请求看连接是否有效。

<bean id="infoCenterAutoBuildTask"          class="edu.xjtu.sei.skyeye.state.timer.BotWallTimer">          <property name="botWallDAO" ref="botWallDAO" />      </bean>      <bean id="scheduledTask"          class="org.springframework.scheduling.timer.ScheduledTimerTask">          <property name="delay" value="10000" />          <property name="period" value="1800000" />            <property name="timerTask" ref="infoCenterAutoBuildTask" />      </bean> 

spring配置



原创粉丝点击