• Quartz使用


    一、 quartz下载

    官网:http://www.quartz-scheduler.org/

    下载压缩包解压:

    二、Quartz使用

    1.创建maven工程,导入spring和quartz相关依赖

    2.创建作业类

    public class MailJob {
    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() {
        //TODO 作业方法
      }
      public String getSmtpServer() {
        return smtpServer;
    }
    
    public void setSmtpServer(String smtpServer) {
        this.smtpServer = smtpServer;
      }
    }

    3.在spring配置文件中配置作业类

    <!-- 注册自定义作业类 -->
      <bean id="myJob" class="com.itheima.jobs.MailJob">
      <property name="username" value="20152543@163.com"/>
      <property name="password" value="123456789"/>
      <property name="smtpServer" value="smtp.163.com"/>
    </bean>

    4.在spring配置文件中配置JobDetail

    <!-- quartz任务详情 -->
      <bean id="jobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
      <!-- 注入目标对象 -->
      <property name="targetObject" ref="myJob"/>
      <!-- 注入目标方法 -->
      <property name="targetMethod" value="execute"/>
    </bean>

    5.在spring配置文件中配置触发器

    <!-- 配置触发器 -->
      <bean id="myTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
      <property name="jobDetail" ref="jobDetail"/>
      <property name="cronExpression" value="0/5 * 12 * * ?"/>
    </bean>

    6.在spring配置文件中配置scheduler

    <bean id="schedulerFactoryBean" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
      <property name="triggers">
        <list>
          <ref bean="myTrigger"/>
        </list>
      </property>
    </bean>
    /**
     * 发送邮件的作业
     * @author zhaoqx
     *
     */
    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 test(){
            System.out.println("run方法执行了");
        }
        
        public void execute() {
            System.out.println("要发邮件了。。。");
            try {
                //查询工单类型为新单的所有工单
                List<Workbill> list = workbillDao.findAll();
                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("20152543@cqu.edu.cn");
                        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;
        }
    }
  • 相关阅读:
    006-Spring Boot自动配置-Condition、Conditional、Spring提供的Conditional自动配置
    005-Spring Boot配置分析-配置文件application、Environment、PropertySource、@Value、EnvironmentPostProcessor、Profiles
    新浪云php与java连接MySQL数据库
    基于网页内容数据采集 PHP开发学习笔记
    淘宝理财 中证500 中证300 基金收益计算
    响应式Web设计(Responsive Web design)
    FOJ 1075
    锐捷上网认证常见问题及解决办法
    session与cookie的区别
    php email邮箱正则验证
  • 原文地址:https://www.cnblogs.com/naixin007/p/9096168.html
Copyright © 2020-2023  润新知