您的位置:首页 > 编程语言 > Java开发

Spring-quartz定时任务

2016-08-30 09:49 369 查看
1、在Maven项目加入quartz的jar包

2、新建spring-quartz.xml

<?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"此两个值可以不配置 -->
<description>Quartz Job Setting</description>
<!-- A.配置调度的任务对应bean的id和自定义class-->
<!-- 每天凌晨4点跑,打包数据成2张图片和一个TXT文件 -->
<bean id="SendMailQuartz" class="cn.cloudwalk.syeb.core.common.quartz.SendMailQuartz" />
<!-- B.配置调度任务对应的bean的id和执行的方法,作业不并发调度-->
<bean id="SendMailQuartzMethod" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="SendMailQuartz" />
<property name="targetMethod" value="sendMail" />
<property name="concurrent" value="false" />
</bean>
<!-- C.配置调度任务执行的触发的时间-->
<bean id="sendMailTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="SendMailQuartzMethod" />
<property name="cronExpression">
<!-- 每15分钟执行任务调度 -->
<!--  <value>0 0/15 * * * ?</value> -->
<!-- 每天凌晨3点执行任务调度 -->
<value>0 0 9 * * ?</value>
</property>
</bean>
<!-- D.Quartz的调度工厂,调度工厂只能有一个,多个调度任务在list中添加 -->
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<!-- 所有的调度列表-->
<ref bean="sendMailTrigger" />
</list>
</property>
</bean>
</beans>


3、把spring-quartz.xml引入web.xml

<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/spring-mvc.xml,classpath:spring/spring-quartz.xml</param-value>
</init-param>


4、附录邮件发送代码

@RequestMapping(value = "/send")
public void send(String[] to, String[] cc, String subject, String content) throws MessagingException {
// 配置发送邮件的环境属性
final Properties props = new Properties();
/*
* 可用的属性: mail.store.protocol / mail.transport.protocol / mail.host /
* mail.user / mail.from
*/
// 表示SMTP发送邮件,需要进行身份验证
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.host", mailSmtpHost);
// 发件人的账号
props.put("mail.user", mailUser);
// 访问SMTP服务时需要提供的密码
props.put("mail.password", mailPassword);
//收件人
props.put("mail.sendTo", sendTo);

// 构建授权信息,用于进行SMTP进行身份验证
Authenticator authenticator = new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
// 用户名、密码
String userName = props.getProperty("mail.user");
String password = props.getProperty("mail.password");
return new PasswordAuthentication(userName, password);
}
};
// 使用环境属性和授权信息,创建邮件会话
Session mailSession = Session.getInstance(props, authenticator);
// 创建邮件消息
MimeMessage message = new MimeMessage(mailSession);
// 设置发件人
InternetAddress form = new InternetAddress(
props.getProperty("mail.user"));
message.setFrom(form);

// 设置收件人
String toStr = props.getProperty("mail.sendTo");
to = toStr.split(",");
InternetAddress[] sendTo = new InternetAddress[to.length];
for (int i = 0; i < to.length; i++) {
sendTo[i] = new InternetAddress(to[i]);
}
message.setRecipients(RecipientType.TO, sendTo);

// 设置抄送
if(cc!=null && !"".equals(cc)) {
InternetAddress[] sendCc = new InternetAddress[cc.length];
for (int i = 0; i < cc.length; i++) {
sendCc[i] = new InternetAddress(cc[i]);
}
message.setRecipients(RecipientType.CC, sendCc);
}

// 设置邮件标题
if(subject!=null && !"".equals(subject)) {
message.setSubject(subject);
}

// 设置邮件的内容体
if(content!=null && !"".equals(content)) {
message.setContent(content, "text/html;charset=UTF-8");
}

// 发送邮件
Transport.send(message);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  quartz