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. LOJ.114.K大异或和(线性基)

    题目链接 如何求线性基中第K小的异或和?好像不太好做. 如果我们在线性基内部Xor一下,使得从高到低位枚举时,选base[i]一定比不选base[i]大(存在base[i]). 这可以重构一下线性基, ...

  2. php curl 发送get和post请求示例

    <?php final class HttpClient { const TIME_OUT = 10; static function get($url) { $ch = curl_init() ...

  3. Swift 内存管理

    1.Object-C 经历两个阶段: 1.手动引用计数内存管理(Manual Reference Counting,MRC) 2.自动引用计数内存管理(Automatic Refernce Count ...

  4. Android WebView H5开发拾遗

    上篇介绍了一些WebView的设置,本篇为一些补充项. 首先加载HTML5的页面时,会出现页面空白的现象,原因是需要开启 DOM storage API 功能: webSettings.setDomS ...

  5. 重温JavaScript获取CSS样式的方法(兼容各浏览器)

    众所周知,CSS样式有三种类型:行内样式.内部样式和外部样式,JavaScript获取CSS样式时分为两种情况:行内样式获取法 和 非行内样式获取法 . 一.行内样式获取相对简单,通过element. ...

  6. 嵌入式设备hacking(转)

    原帖地址:http://drops.wooyun.org/papers/5157 0x00 IPCAM hacking TOOLS github-binwalk firmware-mod-kit ID ...

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

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

  8. linux centos 系统php支持jpeg的安装方法

    linux php支持jpeg首先要安裝libjpeg,运行下面的命令: yum install libjpeg* libpng* freetype* gd* 耐心等待完成,重启(service ht ...

  9. 转:在两个页面间翻转设置Animation动作的一些总结

    今天碰到两个页面之间翻转的动作设计问题,发现了一些问题,故做个总结,很多都写在注释部分: 1.首先,我们来手动创建两个view以及相应的viewController.是手动,不是用IB (1)刚开始只 ...

  10. __super

    __super::member_function(); The __super keyword allows you to explicitly state that you are calling ...