1. 程式人生 > >springmvc中實現quartz定時任務(每分鐘的第3秒執行任務排程方法)

springmvc中實現quartz定時任務(每分鐘的第3秒執行任務排程方法)

1:實現觸發器,最大的問題是jar包的處理(*.jar定時jar和sourcecodesource code):

此處,最關鍵的jar為第二個,名字最長。

maven依賴:

		<dependency>
			<groupId>org.apache.servicemix.bundles</groupId>
			<artifactId>org.apache.servicemix.bundles.spring-context-support</artifactId>
			<version>4.0.7.RELEASE_2</version>
		</dependency>
		<dependency>
			<groupId>org.quartz-scheduler</groupId>
			<artifactId>quartz</artifactId>
			<version>1.8.6</version>
		</dependency>

2:觸發器在web.xml中配置:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
	id="WebApp_ID" version="3.0">
	<display-name>webdemo1</display-name>
	  <!-- 監聽spring上下文容器 -->
  <listener>
  	<listener-class>
  			org.springframework.web.context.ContextLoaderListener 
  	</listener-class>	
  </listener>
  <!-- 載入spring的xml配置檔案到spring的上下文容器中 -->
  <context-param>
  		<param-name>contextConfigLocation</param-name>
  		<param-value>classpath*:applicationContext-*.xml</param-value>
  </context-param>
    <!-- 配置springmvc DispatcherServlet  -->
  <servlet>
  	<servlet-name>mvc</servlet-name>
  	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  	<init-param>
  		<param-name>contextConfigLocation</param-name>
  		<param-value>/WEB-INF/springmvc.xml</param-value>
  	</init-param>
  	<load-on-startup>1</load-on-startup>
  </servlet>
  <!-- 配置DispatcherServlet需要攔截的url -->
  <servlet-mapping>
  	<servlet-name>mvc</servlet-name>
  	<url-pattern>*.html</url-pattern>
  </servlet-mapping>
	
	<welcome-file-list>
		<welcome-file>index.html</welcome-file>
		<welcome-file>index.htm</welcome-file>
		<welcome-file>index.jsp</welcome-file>
		<welcome-file>default.html</welcome-file>
		<welcome-file>default.htm</welcome-file>
		<welcome-file>default.jsp</welcome-file>
	</welcome-file-list>
</web-app>

3:springmvc的配置:

<?xml version="1.0" encoding="UTF-8"?>
<beans
	xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context"  
	xmlns:mvc="http://www.springframework.org/schema/mvc"  
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
	http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
	http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd
	http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
		<!-- springmvc配置 -->
		<!-- 通過component-scan讓spring掃描package下的所有類,讓spring的註解生效-->
		<context:component-scan base-package="com.tsxs"></context:component-scan>
		<!-- 配置springmvc的檢視渲染器,讓其字首為:/ 字尾為: .jsp 將檢視渲染到 /views/<method返回值>.jsp中 -->
		<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
			<property name="prefix" value="/WEB-INF/views/"></property>
			<property name="suffix" value=".jsp"></property>
		</bean>
</beans>

4:定時任務配置檔案

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd"
	default-autowire="byName" default-lazy-init="false">
	<!-- default-autowire="byName" default-lazy-init="false"此兩個值可以不配置 -->
	<description>Quartz Job Setting</description>
  <!-- A.配置排程的任務對應bean的id和自定義class-->
  <bean id="myQuartz" class="com.tsxs.tools.Quartz" />
  <!-- B.配置排程任務對應的bean的id和執行的方法,作業不併發排程-->
  <bean id="myDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
    <property name="targetObject" ref="myQuartz" />
    <property name="targetMethod" value="tips" />
    <property name="concurrent" value="false" />
  </bean>
  <!-- C.配置排程任務執行的觸發的時間-->
  <bean id="myTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
  <property name="jobDetail" ref="myDetail" />
     <property name="cronExpression">
     <!-- 每分鐘的第3秒執行任務排程 -->
      <value>3 * * * * ?</value>
    </property>
  </bean>
  <!-- D.Quartz的排程工廠,排程工廠只能有一個,多個排程任務在list中新增 -->
  <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
    <property name="triggers">
      <list>
         <!-- 所有的排程列表-->
        <ref bean="myTrigger" />
<!-- <ref bean="myTrigger1" />
        <ref bean="myTrigger2" />
        對應的bean配置:id="myDetail1" 和 id="myTrigger2" 可以對應的並行多配置-對應執行JavaBean和執行時間(各自觸發time)
  -->
      </list>
    </property>
  </bean>
</beans>

注:時間配置:可以看quartz配置或者網路搜尋“quartz定時配置”

5:定時任務執行JavaBean:

package com.tsxs.tools;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

public class Quartz {
//	public class Quartz implements Job{
//此處可以不實現Job介面的execute方法
//	private Date date;
	/**
	 * 定時任務,執行方法
	 * */
	public void tips(){
		String time = new SimpleDateFormat("MMM d,yyyy KK:mm:ss a",Locale.ENGLISH).format(System.currentTimeMillis());
		System.out.println("time:"+time);
	}

//	@Override
//	public void execute(JobExecutionContext context) throws JobExecutionException {
//		date = context.getFireTime();
//	}
}


6:執行結果:

time:Jun 24,2015 00:05:03 PM
time:Jun 24,2015 00:06:03 PM
time:Jun 24,2015 00:07:03 PM
time:Jun 24,2015 00:08:03 PM
time:Jun 24,2015 00:09:03 PM

注:

①:定時任務執行JavaBean可以不實現Job介面的execute方法

②:在定時任務配置中:設定default-lazy-init="true",否則定時任務不觸發,如果不明確指明default-lazy-init的值,預設是false

③:在定時任務配置中:設定default-autowire="byName"的屬性,可能導致後臺會報org.springframework.beans.factory.BeanCreationException錯誤(此時,就不能通過Bean名稱自動注入,必須通過明確引用注入)

相關推薦

springmvc實現quartz定時任務分鐘3執行任務排程方法

1:實現觸發器,最大的問題是jar包的處理(*.jar定時jar和sourcecodesource code): 此處,最關鍵的jar為第二個,名字最長。 maven依賴: <dependency> <groupId>org.apache

python定時程式隔一段時間執行指定函式

import os import time def print_ts(message): print "[%s] %s"%(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()), message) def run(

關於Java Web 使用Spring使用Quartz(定時呼叫、實現固定時間執行), 觸發定時執行某些任務的例項

第一步:pom.xml中Maven下載需要的jar架包。 <!--```````````定時器```````````--> <dependency> <grou

ssm配置Quartz定時排程任務

1.首先在你web專案中pom.xml配置相關依賴jar包,非maven專案自行新增對應版本的jar檔案 pom.xml: <!-- quartz定時器相關依賴jar版本 --> <dependency> <groupId>or

spring+springmvc+mybatis寫quartz定時任務

1:pom.xml裡面加上quartz包的配置 <dependency> <groupId>org.quartz-scheduler</groupId>

Windows新增Mongodb定時備份任務採用bat指令碼匯入xml計劃任務的方式

最近公司部分客戶的服務出現了本地mongodb資料丟失被盜的現象,前期經排查發現為遠端木馬病毒攻擊導致(勒索病毒)。由於部分客戶的網路狀況不是很良好,服務直接暴露在公網。因此要將客戶伺服器產生的資料進行定期的本地備份。由於之前接觸的mongodb都是停留在增刪改查中,沒有深入

tomcatquartz定時任務每次都被執行了兩次

這兩天發現部署到tomcat中的quartz定時任務每回都被執行了兩次,但是在myeclipse執行時又不會,後來搜了網上,才發現該問題只發生於部署在tomcat伺服器上,由tomcat的自啟動導致。 導致該問題的原因是你的tomcat的conf目錄中的server.xm

在spring實現quartz的動態排程開始、暫停、停止等

需求: 需要在頁面設定某個時間,然後點選按鈕後,執行某個排程,並且可以在頁面刪除某個排程 1、導包 <dependency> <groupId>org.quartz-scheduler</groupId> <

使用IDEA 實現springboot 熱部署 spring boot devtools版

apple convert lang start class tool 但是 原理 tty 第一步:添加springboot的配置文件 首先我先貼出我的配置 添加依賴包 <!-- spring boot devtools 依賴包. --> &

SpringMVC學習(九)——SpringMVC實現文件上傳

enc 一個人 ast max fonts common clas c學習 本地磁盤 這一篇博文主要來總結下SpringMVC中實現文件上傳的步驟。但這裏我只講單個文件的上傳。 環境準備 SpringMVC上傳文件的功能需要兩個jar包的支持,如下: 工程中肯定要導入

UWP實現大爆炸效果

ID eight 爆炸效果 foo 更新 The 選中 wid 重寫 自從老羅搞出大爆炸之後,各家安卓都內置了類似功能。UWP怎麽能落下呢,在這裏我們就一起擼一個簡單的大爆炸實現。 閑話不說,先上效果: 因為代碼太多,所以我打算寫成一個系列,下面是第一篇的正文: 首先

UWP實現大爆炸效果

cti setter val sele osi enume rail += ddd 上一回實現了一個寬度不均勻的Panel,這次我們編寫一個簡單的BigbangView主體。 首先創建一個模板化控件,刪掉Themes/Generic.xaml中的<Style Targ

在python實現線性回歸linear regression

lsa d+ 分享圖片 通過 nsq mps mile edi mfp 1 什麽是線性回歸 確定因變量與多個自變量之間的關系,將其擬合成線性關系構建模型,進而預測因變量 2 線性回歸原理 最小二乘法OLS(ordinary learst squares) 模型的y與實際值y

SpringMVCcontroller接收Json資料重要

SpringMVC中controller接收Json資料 1.jsp頁面傳送ajax的post請求: function postJson(){ var json = {"username" : "imp", "password" : "123456"};

java實現PDF轉圖片頁轉換成一張圖片,可單頁轉換或指定頁數

話不多說,直接上程式碼 public class PDF2IMAGE { public static void main(String[] args) { if(args!=null && args.length>=4) {

scrapy框架實現登入人人網最新登入方式

      上篇部落格說到登入人人網的時候,如果同一個賬號出錯超過三次,那麼將會出現四個漢字的驗證碼,這裡我們利用打碼平臺來破解驗證碼並傳入(實際上,如果簡單點可以通過肉眼觀察出現的驗證碼,然後input輸入結果。)如下圖所示,通過上節的分析我們知道密碼是通過加密傳

scrapy框架實現登入人人網最新登入方式

        最近在弄scrapy框架的問題,感覺裡面好玩的東西有很多,無意中在bilibili中看到關於在scrapy實現登入人人網的視訊,人人網可能使用者少,所以在現在的一些部落格和教程裡面看到最新的登入方法幾乎沒有,於是自己寫了這篇部落格。 &

tp5使用crontab實現資料庫的自動備份分鐘小時、每天……

效果展示(每分鐘備份一次): 之前搞過一次資料庫自動備份,但是沒搞出來……後來得知Linux系統的一個命令:crontab。完美的解決了程式定時執行的難題 crontab詳解 一、cron服務 service crond start //啟動服務

SpringMVC+jade實現高效能模板引擎簡單配置

最近在研究一個前後端通用的高效能模板引擎,大概搜尋了下資料,有很多類似的模板引擎,比如Jade,Mustache.js,Dust.js,Nunjucks,EJS等等,當然只適用於前端或者只適用於後端的模板引擎就不算啦,比如(jquery template,fre

Spring Scheduled + Redis 實現分散式定時

1、需要了解的技術點: 1.1、Redis的命令:SETNX,EXPIRE; 1.2、Spring的Scheduled定時器註解,觸發器,任務,排程器; 1.3、Spring的applicati