Spring功能越来越多了,用起来还很舒服方便,Quartz实现的定时任务就是一个。

首先是配置文件:

<?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-2.5.xsd">

<!-- schedulerFactory -->
    <bean id="schedulerFactory"    class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="triggers">
            <list>

      <!-- 有多少定时任务在这里写几个 -->
                <ref local="cronTriggerClaim" />
            </list>
        </property>
    </bean>
    
    <!-- 1.AutoClaimReminderMailService 第一个定时任务-->
    <bean id="cronTriggerClaim" class="org.springframework.scheduling.quartz.CronTriggerBean">
        <property name="jobDetail" ref="jobDetailClaim" />
        <property name="cronExpression">
                <value>0 10 13 ? * MON</value> <!-- 这里设定时间,周一的13点10分00秒 开始-->
        </property>
    </bean>    
    
    <bean id="jobDetailClaim"
        class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
        <property name="targetObject" ref="claimScheduler" />  <!-- 这里找下面的bean-->
        <property name="targetMethod" value="sendRemiderMail" />  <!-- 这里设定定时任务的具体方法-->
    </bean>

<!-- 这个类就是自己写的了,注意要实现上面提到的sendRemiderMail方法,其它可自便-->
    <bean id="claimScheduler" class="com.ibm.XXX.service.auto.AutoClaimReminderMailService">
        <property name="smtpServer"><value>smtp.163.com</value></property>
        <property name="smtpUsername"><value>unknown@163.com</value></property>
        <property name="smtpPassword"><value>puzzle</value></property>
        <property name="fromMailAddress"><value>unknown@163.com</value></property>
        <property name="toMailAddress"><value>unknown1@163.comm,unknown2@163.com</value></property>
        <property name="ccMailAddress"><value>unknow3@163.com,unknown4@163.com</value></property>
        <property name="bccMailAddress"><value>unknown5@163.com,unknown6@163.com</value></property>
    </bean>    
</beans>

下面是com.ibm.XXX.service.auto.AutoClaimReminderMailService类:

public class AutoClaimReminderMailService{
    private static Logger logger = Logger.getLogger(AutoClaimReminderMailService.class);
    
    public void sendRemiderMail() {
        String title="[Need Your Action!]Claim Reminder";
        
        StringBuilder sb=new StringBuilder();
        sb.append("<p>Hi Guys:</p>");
        sb.append("<p><B>Here is the claim reminder bell kindly for your action, pls submit your labor claim in ILC/Cats within today for this week, thanks your cooperation!</B></p>");

sendMail(title,sb.toString());
    }

protected String smtpServer;
    protected String smtpUsername;
    protected String smtpPassword;
    protected String fromMailAddress;
    protected String toMailAddress;
    protected String ccMailAddress;
    protected String bccMailAddress;
    
    /**
     * 无参构造函数
     */
    public AutoClaimReminderMailService(){
        
    }

/**
     * 发送邮件的关键函数
     *
     * @param title
     * @param content
     * @return
     * @throws Exception
     */
    protected boolean sendMail(String title,String content) throws Exception{
        Properties props = new Properties();
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.host", smtpServer);
        // 获得邮件会话对象
        Session session = Session.getDefaultInstance(props,new SmtpAuthenticator(smtpUsername, smtpPassword));
        /** *************************************************** */
        // 创建MIME邮件对象
        MimeMessage mimeMessage = new MimeMessage(session);
        mimeMessage.setFrom(new InternetAddress(fromMailAddress));// 发件人
        
        mimeMessage.setRecipients(Message.RecipientType.TO, getInternetAddressArr(toMailAddress));// To收件人
        mimeMessage.setRecipients(Message.RecipientType.CC, getInternetAddressArr(ccMailAddress));// Cc收件人
        mimeMessage.setRecipients(Message.RecipientType.BCC, getInternetAddressArr(bccMailAddress));// Bcc收件人
        
        mimeMessage.setSubject(title);
        mimeMessage.setSentDate(new Date());// 发送日期
        Multipart mp = new MimeMultipart("related");// related意味着可以发送html格式的邮件
        /** *************************************************** */
        BodyPart bodyPart = new MimeBodyPart();// 正文
        bodyPart.setDataHandler(new DataHandler(content,"text/html;charset=utf8"));// 网页格式
        mp.addBodyPart(bodyPart);
        mimeMessage.setContent(mp);// 设置邮件内容对象
        Transport.send(mimeMessage);// 发送邮件       
        
        return true;
    }
    
    protected InternetAddress[] getInternetAddressArr(String mialAddr) throws Exception{
        String[] arr=mialAddr.split(",");
        
        InternetAddress[] retval=new InternetAddress[arr.length];
        
        for(int i=0;i<arr.length;i++){
            retval[i]=new InternetAddress(arr[i]);
        }
        
        return retval;
    }
}

就到这里,再见吧。

使用Spring提供Quartz来实现定时任务的更多相关文章

  1. 使用Spring整合Quartz轻松完成定时任务

    一.背景 上次我们介绍了如何使用Spring Task进行完成定时任务的编写,这次我们使用Spring整合Quartz的方式来再一次实现定时任务的开发,以下奉上开发步骤及注意事项等. 二.开发环境及必 ...

  2. Spring整合Quartz轻松完成定时任务

    一.背景 上次我们介绍了如何使用Spring Task进行完成定时任务的编写,这次我们使用Spring整合Quartz的方式来再一次实现定时任务的开发,以下奉上开发步骤及注意事项等. 二.开发环境及必 ...

  3. Spring 整合 Quartz 实现动态定时任务

    复制自:https://www.2cto.com/kf/201605/504659.html 最近项目中需要用到定时任务的功能,虽然Spring 也自带了一个轻量级的定时任务实现,但感觉不够灵活,功能 ...

  4. 【转】Spring 整合 Quartz 实现动态定时任务

    http://blog.csdn.net/u014723529/article/details/51291289 最近项目中需要用到定时任务的功能,虽然spring 也自带了一个轻量级的定时任务实现, ...

  5. Spring 整合 Quartz 实现动态定时任务(附demo)

    最近项目中需要用到定时任务的功能,虽然Spring 也自带了一个轻量级的定时任务实现,但感觉不够灵活,功能也不够强大.在考虑之后,决定整合更为专业的Quartz来实现定时任务功能. 普通定时任务 首先 ...

  6. Spring 整合 Quartz框架(定时任务)

    Maven 无法下载 Quartz 依赖,去官网下载 http://www.quartz-scheduler.org/downloads/ Quartz 官方手册:https://www.w3csch ...

  7. 初识spring与quartz整合实现定时任务

    参考资料: http://kevin19900306.iteye.com/blog/1397744 引用自别人的博客: 特别注意一点,与Spring3.1以下版本整合必须使用Quartz1,最初我拿2 ...

  8. Spring 3整合Quartz 2实现定时任务--转

    常规整合 http://www.meiriyouke.net/?p=82 最近工作中需要用到定时任务的功能,虽然Spring3也自带了一个轻量级的定时任务实现,但感觉不够灵活,功能也不够强大.在考虑之 ...

  9. spring Quartz多个定时任务的配置

    Quartz多个定时任务的配置 1,配置文件与spring整合,需要在spring 的总配置中一入或者在web.xml中spring监听中加上 ztc_cp-spring-quartz.xml 注:定 ...

随机推荐

  1. AGC009D Uninity

    一些无关紧要的事: 似乎很久没写题解了……象征性地更一篇.另外很多blog都设了私密,不是很敢公开,不过说不定哪天会公开的. link 题意: 最优树的点分治:使得点分最大层数最小.(听说是经典问题) ...

  2. Python知识(7)--最小二乘求解

    这里展示利用python实现的最小二乘的直接求解方法.其求解原理,请参考:最小二乘法拟合非线性函数及其Matlab/Excel 实现 1.一般曲线拟合 代码如下: # -*- coding:utf-8 ...

  3. Mac安装homebrew安装到指定目录

    第一种直接安装在/usr/local目录下 mac 打开终端输入 ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebr ...

  4. [分享]2013:Linux的黄金之年-十大杰出成就

    2013年已经过去.这一年见证了许多里程碑事件,使得2013年可以称得上是一个Linux的黄金之年.其中一些成果在FOSS和Linux世界更可以称得上是举世瞩目的成就. 1.Android的上升趋势 ...

  5. 如何在ubuntu安装phpstorm

    第一步:使用组合键ctrl+alt+t 打开Terminal,cd /home/xxx(当前登录用户名)/downloads(下载目录) 第二步:下载 phpstorm 附截止发文最新版本链接:htt ...

  6. 介绍一下开源项目FastAnimationWithPOP

    介绍一下开源项目FastAnimationWithPOP JUL 23RD, 2014 这是一个非常easy的动画框架,基于Facebook的POP库. 使用它你就能够在故事版中以0行代码的代价来加入 ...

  7. maven打包出错: Failed to clean project: Failed to delete

    maven打包出错: Failed to clean project: Failed to delete 出现这种错误,通常是由于您已启动了另一个tomcat 进程,导致报错,关闭tomcat进程即可 ...

  8. systemtap 用户态调试2

    [root@localhost ~]# cat user.stpprobe process(@1).function(@2){print_ubacktrace();exit();} session 1 ...

  9. 解析本内置Linux目录结构

    使用声明:1.此版本采用官方原版ISO+俄罗斯HunterTik 的Debian包制作而成2.此IMG包未进行Crack,资源来源于网络,如果你下载的是Crack版,与原作者无关,请自行分辨.“就看人 ...

  10. Linux gcc编译参数

    最近编译一份开源代码,一编译就直接报错.我看了下报错信息,有点诧异.这些信息,放平常顶多就是个warnning而已啊,他这里怎么变成了error呢?我看了下Makefile,发现编译参数多了个-Wer ...