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

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

java定時(shí)器

系統(tǒng) 2430 0

以前項(xiàng)目中寫過類似的定時(shí)器,今天復(fù)習(xí)1下,自己建了個(gè)工程.

實(shí)現(xiàn)原理:創(chuàng)建servlet,應(yīng)用服務(wù)器自動(dòng)加載此servlet,在web.xml設(shè)置定時(shí)器的各個(gè)參數(shù)

開發(fā)工具:myeclipse6.0

應(yīng)用服務(wù)器:tomcat6.0

1、創(chuàng)建web工程TestTimer

2、創(chuàng)建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();
???????
??????? // 定時(shí)器開關(guān)
??????? String startTask = getInitParameter("startTask");
??????? System.out.println(startTask);
???????
??????? // 開始運(yùn)行時(shí)間
??????? Calendar calendar = Calendar.getInstance();
??????? calendar.set(Calendar.HOUR_OF_DAY,Integer.parseInt(getInitParameter("startTime")));
??????? Date time = calendar.getTime();

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

}

?

//Task為任務(wù)類

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){???

???????????? //此處寫自己需要循環(huán)的業(yè)務(wù)邏輯
????????????? 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>
??? <!-- // 定時(shí)器開關(guān)? -->
???? <param-name>startTask</param-name>
???? <param-value>true</param-value>
??? </init-param>
??? <init-param>
??? <!-- // 開始運(yùn)行時(shí)間? HH-->
???? <param-name>startTime</param-name>
???? <param-value>12</param-value>
??? </init-param>
??? <init-param>
??? <!-- // 緩沖時(shí)間? 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>

目錄結(jié)構(gòu):


java定時(shí)器
?

java定時(shí)器


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號(hào)聯(lián)系: 360901061

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

【本文對(duì)您有幫助就好】

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

發(fā)表我的評(píng)論
最新評(píng)論 總共0條評(píng)論
主站蜘蛛池模板: 免费一区二区 | 亚欧成人毛片一区二区三区四区 | 国产精品视频一区二区三区不卡 | 久久永久免费视频 | 狼狼色丁香久久婷婷综合五月 | 九九热在线免费视频 | 成人午夜精品网站在线观看 | 国产精品二区页在线播放 | 亚洲rv国产rv日本rv | 中文字幕一区二区三 | 日本免费不卡 | 欧美精品xxx| 亚洲一区中文字幕在线 | 色在线综合 | 色综合久久婷婷天天 | 九九久久精品这里久久网 | 亚洲精品成人一区二区aⅴ 亚洲精品成人一区二区www | 色片网 | 欧美特黄a级猛片a级 | 亚洲国产成人久久综合野外 | 色香视频在线 | 黄色小视频在线免费观看 | 日韩成人在线网站 | 免费视频爱爱太爽在线观看 | 婷婷综合五月中文字幕欧美 | 妖精视频国产 | 免费看又爽又黄禁片视频1000 | 成人午夜影院在线观看 | 久久天堂一区二区三区 | 天天操夜夜操夜夜操 | 日本xxx高清免费视频 | 特级毛片在线播放 | 色一色综合 | 亚洲欧美一区二区三区久久 | 免费在线一级片 | 在线欧美视频 | 青青青视频自偷自拍视频1 青青青手机版视频在线观看 | 99视频在线永久免费观看 | 亚洲视频天天射 | 久久一本热 | 精品热久国产福利视频 |