Spring-quartz定时任务
2016-08-30 09:49
369 查看
1、在Maven项目加入quartz的jar包
2、新建spring-quartz.xml
3、把spring-quartz.xml引入web.xml
4、附录邮件发送代码
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); }
相关文章推荐
- Spring整合Quartz(JobDetailBean方式)
- Spring整合Quartz(JobDetailBean方式)
- 在Java的Spring框架中配置Quartz的教程
- Java的作业调度类库Quartz基本使用指南
- 最流行的java后台框架spring quartz定时任务
- java定时调度器(Quartz)使用实例
- quartz实现定时功能实例详解(servlet定时器配置方法)
- Spring3.2.0和Quartz1.8.6集群配置
- Cron表达式
- 分布式任务调度平台XXL-JOB
- JFinal 整合Quartz
- JFinal Quartz 支持配置文件和持久化
- <我的备忘录> quartz在Play中的简单使用
- Quartz 任务调度(一)
- Quartz 任务调度(二)
- spring整合quartz实现定时任务
- quartz的简单实现
- Spring Boot集成Quartz注入Spring管理的类
- jfinal-quartz 使用jfinal启动quartz执行定时任务
- Quartz定时调度时间配置格式说明与实例