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;
    }
}

 

posted on 2018-05-27 15:14  bofeng  阅读(185)  评论(0编辑  收藏  举报