zoukankan      html  css  js  c++  java
  • quartz 的简单使用

    0.依赖:

    <!-- 引入quartz对应的依赖 -->
            <dependency>
                <groupId>org.quartz-scheduler</groupId>
                <artifactId>quartz</artifactId>
                <version>2.2.3</version>
            </dependency>
            <dependency>
                <groupId>org.quartz-scheduler</groupId>
                <artifactId>quartz-jobs</artifactId>
                <version>2.2.3</version>
            </dependency>

    1.配置。

    <!-- ******************定时任务quartz配置********************* -->
    <!-- 注册自定义作业类 -->
        <bean id="myJob" class="com.shan.bos.service.jobs.MailJob">
            <property name="username" value="lsXXX"/>
            <property name="password" value="lXXX"/>
            <property name="smtpServer" value="XX.com"/>
        </bean>
        
        <!-- 配置JobDetail -->
        <bean id="jobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
            <!-- 注入目标对象 即目标类 -->
            <property name="targetObject" ref="myJob"/>
            <!-- 注入目标方法 -->
            <property name="targetMethod" value="execute"/>
        </bean>
        
        <!-- 配置触发器 -->
        <bean id="myTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
            <!-- 注入任务详情对象 -->
            <property name="jobDetail" ref="jobDetail"/>
            <!-- 注入cron表达式,通过这个表达式指定触发的时间点 -->
            <property name="cronExpression">
                <value>0/10 * * * * ? 2019</value>
            </property>
        </bean>
        
        <!-- 配置调度工厂 -->
        <bean id="schedulerFactoryBean" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
            <!-- 注入触发器 -->
            <property name="triggers">
                <list>
                    <ref bean="myTrigger"/>
                </list>
            </property>
        </bean>

    2. 定时的任务

    package com.shan.bos.service.jobs;
    
    import java.util.List;
    import java.util.Properties;
    
    import javax.annotation.Resource;
    import javax.mail.Authenticator;
    import javax.mail.Message.RecipientType;
    import javax.mail.PasswordAuthentication;
    import javax.mail.Session;
    import javax.mail.Transport;
    import javax.mail.internet.InternetAddress;
    import javax.mail.internet.MimeMessage;
    
    import com.shan.bos.dao.IWorkbillDao;
    import com.shan.bos.domain.Workbill;
    
    
    /**
     * 发送邮件的作业
     * @author Sea
     *
     */
    public class MailJob {
        @Resource
        private IWorkbillDao workbillDao;
    
        private String username;
        private String password;
        private String smtpServer;
    
        public String getUsername() {
            return username;
        }
    
        public void setUsername(String username) {
            this.username = username;
        }
    
        public String getPassword() {
            return password;
        }
    
        public void setPassword(String password) {
            this.password = password;
        }
    
        public void execute() {
            System.out.println("要发邮件了。。。");
            try {
                //查询工单类型为新单的所有工单
                List<Workbill> list = workbillDao.findAll();
                System.out.println(list.get(0).toString());
                if(null != list && list.size() > 0){
                    final Properties mailProps = new Properties();
                    mailProps.put("mail.smtp.host", this.getSmtpServer());
                    mailProps.put("mail.smtp.auth", "true");
                    mailProps.put("mail.username", this.getUsername());
                    mailProps.put("mail.password", this.getPassword());
    
                    // 构建授权信息,用于进行SMTP进行身份验证
                    Authenticator authenticator = new Authenticator() {
                        protected PasswordAuthentication getPasswordAuthentication() {
                            // 用户名、密码
                            String userName = mailProps.getProperty("mail.username");
                            String password = mailProps.getProperty("mail.password");
                            return new PasswordAuthentication(userName, password);
                        }
                    };
                    // 使用环境属性和授权信息,创建邮件会话
                    Session mailSession = Session.getInstance(mailProps, authenticator);
                    for(Workbill workbill : list){
                        // 创建邮件消息
                        MimeMessage message = new MimeMessage(mailSession);
                        // 设置发件人
                        InternetAddress from = new InternetAddress(mailProps.getProperty("mail.username"));
                        message.setFrom(from);
                        // 设置收件人
                        InternetAddress to = new InternetAddress("lshan511094523@163.com");
                        message.setRecipient(RecipientType.TO, to);
                        // 设置邮件标题
                        message.setSubject("系统邮件:新单通知");
                        // 设置邮件的内容体
                        message.setContent(workbill.toString(), "text/html;charset=UTF-8");
                        // 发送邮件
                        Transport.send(message);
                    }
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    
        public String getSmtpServer() {
            return smtpServer;
        }
    
        public void setSmtpServer(String smtpServer) {
            this.smtpServer = smtpServer;
        }
    }
    View Code
  • 相关阅读:
    动态查询 母表和子表的 一种方法
    js的一些正则 整理 长期更新
    fmt 标签格式化 日期
    一些关于 checkbox的前台 jquery 操作 记录
    jQuery 追加元素的方法如append、prepend、before,after(转)
    (Oracle)DBMS_SYSTEM工具-01[20180510]
    MySQL->元数据[20180510]
    MySQL->复制表[20180509]
    MySQL->索引的维护[20180504]
    MySQL-ALTER TABLE命令学习[20180503]
  • 原文地址:https://www.cnblogs.com/lshan/p/9164846.html
Copyright © 2011-2022 走看看