quartz執行個體以及主要事項(註解)

來源:互聯網
上載者:User

標籤:http   io   使用   ar   art   sp   on   c   ef   

實現任務類:

package com.vnetoo.nec.base.quartz;

import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * 定時發送資訊
 *
 * @author lingpy
 * @since 1.0
 */
@Component
@Lazy(false)
public class SendMessageJob{
    private static long time = 0;
    
     @Scheduled(cron="0 0/2 *  * * ? ")  //兩分鐘一次
    public void work(){  
         time++;
        System.out.println("SendMessageJob : send message " + time + ",current :" + System.currentTimeMillis());  
    }  
}

 

 

spring.xml  配置

xmlns:task="http://www.springframework.org/schema/task"

xsi:schemaLocation="
        http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.1.xsd

">

<task:annotation-driven/><!-- task任務掃描註解 -->

spring.xml可以添加註解掃描的配置

xmlns:context="http://www.springframework.org/schema/context

xsi:schemaLocation="

http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd

">

<context:component-scan base-package="com.vnetoo.nec" />

 

主要事項:

在Spring配置和Quartz整合內容時,有兩點需要注意

           1、在<Beans>中不能夠設定default-lazy-init="true",否則定時任務不觸發,如果不明確指明default-lazy-init的值,預設是false。可使用@Lazy(false)顯示確定

           2、在<Beans>中不能夠設定default-autowire="byName"的屬性,否則後台會報 org.springframework.beans.factory.BeanCreationException錯誤,這樣就不能通過Bean名稱自 動注入,必須通過明確引用注入

 

quartz執行個體以及主要事項(註解)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.