使用Spring提供Quartz来实现定时任务
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来实现定时任务的更多相关文章
- 使用Spring整合Quartz轻松完成定时任务
一.背景 上次我们介绍了如何使用Spring Task进行完成定时任务的编写,这次我们使用Spring整合Quartz的方式来再一次实现定时任务的开发,以下奉上开发步骤及注意事项等. 二.开发环境及必 ...
- Spring整合Quartz轻松完成定时任务
一.背景 上次我们介绍了如何使用Spring Task进行完成定时任务的编写,这次我们使用Spring整合Quartz的方式来再一次实现定时任务的开发,以下奉上开发步骤及注意事项等. 二.开发环境及必 ...
- Spring 整合 Quartz 实现动态定时任务
复制自:https://www.2cto.com/kf/201605/504659.html 最近项目中需要用到定时任务的功能,虽然Spring 也自带了一个轻量级的定时任务实现,但感觉不够灵活,功能 ...
- 【转】Spring 整合 Quartz 实现动态定时任务
http://blog.csdn.net/u014723529/article/details/51291289 最近项目中需要用到定时任务的功能,虽然spring 也自带了一个轻量级的定时任务实现, ...
- Spring 整合 Quartz 实现动态定时任务(附demo)
最近项目中需要用到定时任务的功能,虽然Spring 也自带了一个轻量级的定时任务实现,但感觉不够灵活,功能也不够强大.在考虑之后,决定整合更为专业的Quartz来实现定时任务功能. 普通定时任务 首先 ...
- Spring 整合 Quartz框架(定时任务)
Maven 无法下载 Quartz 依赖,去官网下载 http://www.quartz-scheduler.org/downloads/ Quartz 官方手册:https://www.w3csch ...
- 初识spring与quartz整合实现定时任务
参考资料: http://kevin19900306.iteye.com/blog/1397744 引用自别人的博客: 特别注意一点,与Spring3.1以下版本整合必须使用Quartz1,最初我拿2 ...
- Spring 3整合Quartz 2实现定时任务--转
常规整合 http://www.meiriyouke.net/?p=82 最近工作中需要用到定时任务的功能,虽然Spring3也自带了一个轻量级的定时任务实现,但感觉不够灵活,功能也不够强大.在考虑之 ...
- spring Quartz多个定时任务的配置
Quartz多个定时任务的配置 1,配置文件与spring整合,需要在spring 的总配置中一入或者在web.xml中spring监听中加上 ztc_cp-spring-quartz.xml 注:定 ...
随机推荐
- Windows Server 2008 R2下将nginx安装成windows系统服务
一直在Linux平台上部署web服务,但是最近的一个项目,必须要用windows,不得已再次研究了nginx在windows下的表现,因为Apache httpd在Windows下表现其实也不算太好, ...
- 【10.9校内练习赛】【搜索】【2-sat】【树链剖分】【A_star k短路】【差分约束+判负环】
在洛谷上复制的题目! P3154 [CQOI2009]循环赛 题目描述 n队伍比赛,每两支队伍比赛一次,平1胜3负0. 给出队伍的最终得分,求多少种可能的分数表. 输入输出格式 输入格式: 第一行包含 ...
- [分享]2013:Linux的黄金之年-十大杰出成就
2013年已经过去.这一年见证了许多里程碑事件,使得2013年可以称得上是一个Linux的黄金之年.其中一些成果在FOSS和Linux世界更可以称得上是举世瞩目的成就. 1.Android的上升趋势 ...
- js异步任务处理方式
一.es6(es2015)之前:使用原始的callback函数,会陷入回掉地域 this.$http.jsonp('/login', (res) => { this.$http.jsonp('/ ...
- [重要更新][Quartus II][14.1正式版]
[Quartus II][14.1正式版] ----14.1版本最大的变化就是增加了2大系列的器件库: MAX 10和Arria 10.这2大系列据Altera中国区代理 骏龙科技的人说,就是为了和X ...
- generator自动生成mybatis的xml配置
generator自动生成mybatis的xml配置.model.map等信息:1.下载mybatis-generator-core-1.3.2.jar包. 网址:http://code. ...
- POJ 3041(最小点覆盖)
题意: 假如你如今正处在一个N*N的矩阵中,这个矩阵里面有K个障碍物,你拥有一把武器,一发弹药一次能消灭一行或一列的障碍物,求最小的弹药消灭所有障碍物 输入为: N K 接下来有K行,每行包括 ...
- golang slice切片的原理以及内置函数cap, len
golang中slice(切片)是常用的类型, slice是对数组进行封装 package main import ( "fmt" "strconv") fun ...
- <jsp:directive.page import=""/>的用法和解释
<jsp:directive.page import="zero.space.ch03.BookBean"/> 相当于 <%@ page import ...
- springboot-线程池简单使用
最近做项目,关于订单创建时候因为需要调用远程http服务获取数据,然后校验并写入数据库和修改数据库, 导致接口效率低,所以想到实现异步操作的方式解决. 在调用远程接口成功的时候即认为接口处理成功,返回 ...