com.billy.servlet.TestServlet.javapackagecom.billy.servlet;importjava.io.IOException;im" />

亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

java定時器

系統 2103 0

以前項目中寫過類似的定時器,今天復習1下,自己建了個工程.

實現原理:創建servlet,應用服務器自動加載此servlet,在web.xml設置定時器的各個參數

開發工具:myeclipse6.0

應用服務器:tomcat6.0

1、創建web工程TestTimer

2、創建servlet=>com.billy.servlet.TestServlet.java

?

package com.billy.servlet;
import java.io.IOException;
import java.util.Calendar;
import java.util.Date;
import java.util.Timer;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.billy.Task;


public class TestServlet extends HttpServlet {
?
?private Timer timer1 = null;
?private Task task1;
?/**
? * Constructor of the object.
? */
?public TestServlet() {
??super();
?}

?/**
? * Destruction of the servlet. <br>
? */
?public void destroy() {
??super.destroy(); // Just puts "destroy" string in log
??// Put your code here
??????? if(timer1!=null){???
??????????? timer1.cancel();???
??????? }??
?}

?/**
? * The doGet method of the servlet. <br>
? *
? * This method is called when a form has its tag value method equals to get.
? *
? * @param request the request send by the client to the server
? * @param response the response send by the server to the client
? * @throws ServletException if an error occurred
? * @throws IOException if an error occurred
? */
?public void doGet(HttpServletRequest request, HttpServletResponse response)
???throws ServletException, IOException {
??System.out.println("doGet");
?}

?/**
? * The doPost method of the servlet. <br>
? *
? * This method is called when a form has its tag value method equals to post.
? *
? * @param request the request send by the client to the server
? * @param response the response send by the server to the client
? * @throws ServletException if an error occurred
? * @throws IOException if an error occurred
? */
?public void doPost(HttpServletRequest request, HttpServletResponse response)
???throws ServletException, IOException {
??System.out.println("doPost");
?}

?/**
? * Initialization of the servlet. <br>
? *
? * @throws ServletException if an error occurs
? */
?public void init() throws ServletException {
??// Put your code here
??System.out.println("init");
??ServletContext context = getServletContext();
???????
??????? // 定時器開關
??????? String startTask = getInitParameter("startTask");
??????? System.out.println(startTask);
???????
??????? // 開始運行時間
??????? Calendar calendar = Calendar.getInstance();
??????? calendar.set(Calendar.HOUR_OF_DAY,Integer.parseInt(getInitParameter("startTime")));
??????? Date time = calendar.getTime();

??????? // 緩沖時間(分鐘)
??????? Long intervalTime = Long.parseLong(getInitParameter("intervalTime"));
??????? System.out.println(intervalTime);
???????
???????
??????? // 啟動定時器
??????? if(startTask.equals("true")){
??????????? timer1 = new Timer(true);
??????????? task1 = new Task(context);
??????????? timer1.schedule(task1, time, intervalTime * 1000 * 60);???
??????? }
?}

}

?

//Task為任務類

package com.billy;

import java.util.TimerTask;

import javax.servlet.ServletContext;

public class Task extends TimerTask{
?
?private ServletContext context;???
??? static int i = 1;
?
??? private static boolean isRunning = true;???
???????
??? public Task(ServletContext context){???
??????? this.context = context;
??? }???
???????
???????????
??? @Override??
??? public void run() {???
??????? if(isRunning){???

???????????? //此處寫自己需要循環的業務邏輯
????????????? System.out.println("上傳文件" + i);
????????????? i++;
??????? }???
??? }???

}

?

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
?xmlns=" http://java.sun.com/xml/ns/j2ee "
?xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance "
?xsi:schemaLocation=" http://java.sun.com/xml/ns/j2ee
? http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd ">
? <servlet>
??? <servlet-name>TestServlet</servlet-name>
??? <servlet-class>com.billy.servlet.TestServlet</servlet-class>
??? <init-param>
??? <!-- // 定時器開關? -->
???? <param-name>startTask</param-name>
???? <param-value>true</param-value>
??? </init-param>
??? <init-param>
??? <!-- // 開始運行時間? HH-->
???? <param-name>startTime</param-name>
???? <param-value>12</param-value>
??? </init-param>
??? <init-param>
??? <!-- // 緩沖時間? MM-->
???? <param-name>intervalTime</param-name>
???? <param-value>1</param-value>
??? </init-param>
??? <load-on-startup>300</load-on-startup>
? </servlet>

? <servlet-mapping>
??? <servlet-name>TestServlet</servlet-name>
??? <url-pattern>/wangweiTest</url-pattern>
? </servlet-mapping>
? <welcome-file-list>
??? <welcome-file>index.jsp</welcome-file>
? </welcome-file-list>
</web-app>

目錄結構:


java定時器
?

java定時器


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 欧美日韩国产在线观看 | 真人实干一级毛片aa免费 | 婷婷精品进入 | 黄色网址亚洲 | 久久福利资源站免费观看i 久久高清 | 青青青爽视频在线观看 | 狠狠亚洲| 四虎4hu永久免费视频大全 | 在线91精品亚洲网站精品成人 | 国产一级强片在线观看 | 日本一区二区三区久久 | 天天干天天射天天爽 | 四虎影院欧美 | 在线观看一级毛片免费 | a色毛片免费视频 | 色综合中文字幕在线亚洲 | 久久精品女人毛片国产 | 四虎黄色影视库 | 国产欧美高清 | 99九九久久 | 亚洲国产欧美国产综合一区 | 91精品国产91久久久久久青草 | 激情五月色婷婷在线观看 | 日韩高清中文字幕 | 午夜体验区 | 香蕉碰碰人人a久久动漫精品 | 日本免费成人网 | 欧美一及| 伊人色综合琪琪久久社区 | a一级免费视频 | 亚洲综合精品一区二区三区中文 | 狠狠狠很橹影院 | 欧洲毛片 | 午夜免费福利影院 | 中文字幕亚洲综合久久2 | 成人a毛片在线看免费全部播放 | 欧洲亚洲综合一区二区三区 | 啪啪一级片 | 国外成人免费高清激情视频 | 国产专区在线 | 99视频九九精品视频在线观看 |