Java计划任务(任务调度),定时器,定时安排任务,如定时发送Excel表格到用户邮箱(quartz+poi+javax.mail)

文章中使用到的发送邮件代码就不写了,之前发布过--https://blog.csdn.net/yan95520/article/details/89366606

1.在pom.xml中导入jar包(maven工程)



    org.quartz-scheduler
    quartz
    2.2.1
    
        
            c3p0
            c3p0
        
    



    org.apache.poi
    poi
    3.13


    org.apache.poi
    poi-ooxml
    3.13

2.编写工作类,用于完成计划任务的行为事件,如发文件邮件清数据等(包含制作Excel表格)

@Component("myjob")
public class MyJob {
    
    @Autowired
    private EmailUtil emailUtil;

    public void clearMemberTickct() {
        System.out.println("清空程序运行");
        //调用清空数据的方法
        System.out.println("清空" + 3 + "条数据");
    }

    public void sendMemberReport() {
        System.out.println("文本邮件程序运行");
        //调用查询最近登录信息
        emailUtil.sendEmail("[email protected]", "每日会员报表", "会员最近登录信息");
    }


    public void sendExcel() throws IOException {
        System.out.println("表格邮件程序运行");
        List list = service.findAll();
        HSSFWorkbook wk = new HSSFWorkbook();//生成excel文件
        HSSFSheet sheet = wk.createSheet("会员");//创建表
        sheet.createRow(2).createCell(3).setCellValue("会员信息报表");//创建行2同时创建列3并给列3赋值
        HSSFRow titleRow = sheet.createRow(3);//创建行3
        titleRow.createCell(2).setCellValue("会员ID");//创建列2同时赋值
        titleRow.createCell(3).setCellValue("会员名称");//创建列3同时赋值
        titleRow.createCell(4).setCellValue("最近登录时间");//创建列4同时赋值
        int n = 4;
        for (Member m : list) {
            HSSFRow dataRow = sheet.createRow(n);
            dataRow.createCell(2).setCellValue(m.getId());
            dataRow.createCell(3).setCellValue(m.getNickName());
            dataRow.createCell(4).setCellValue(m.getUuidCreateTime() == null ? "" : m.getUuidCreateTime().toString());
            n++;
        }
        OutputStream os = new FileOutputStream("D:/report.xls");// 创建输出流  report.xls文件
        wk.write(os);//将刚刚生成的excel文件的数据写入report.xls文件
        os.close();//清空缓冲区
        //发送Excel表格到指定会员邮箱中
        emailUtil.sendEmail("[email protected]", "每日会员报表", "请查看每日报表", new String[]{"D:/report.xls"});
    }
}

 

3.使用配置文件控制计划任务(applicationContext-task.xml)

xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns="http://www.springframework.org/schema/beans"
  xmlns:aop="http://www.springframework.org/schema/aop"
  xmlns:context="http://www.springframework.org/schema/context"

xmlns:tx="http://www.springframework.org/schema/tx"
  xmlns:cache="http://www.springframework.org/schema/cache"
   xmlns:p="http://www.springframework.org/schema/p"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
     http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
     http://www.springframework.org/schema/aop
     http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
     http://www.springframework.org/schema/context
     http://www.springframework.org/schema/context/spring-context-4.0.xsd
     http://www.springframework.org/schema/tx
     http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
     http://www.springframework.org/schema/cache
     http://www.springframework.org/schema/cache/spring-cache-4.0.xsd">
     
     
   

   
   <bean id="clearuuid"
        class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
      

<property name="targetObject">
         <ref bean="myjob" />      
      property>


      <property name="targetMethod" value="clearMemberTickct" />
      <property name="concurrent" value="false" />
   bean>
   
   
   <bean id="clear_ticket"
        class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
      <property name="jobDetail" ref="clearuuid" />
      <property name="cronExpression" value="0 11 11 * * ? *" />
   bean>
   
   
   <bean id="send_email"
        class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
      <property name="targetObject">
         <ref bean="myjob" />
      property>
      <property name="targetMethod" value="sendMemberReport" />
      <property name="concurrent" value="false" />
   bean>
   
   
   <bean id="sendMemberReport"
        class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
      <property name="jobDetail" ref="send_email" />
      <property name="cronExpression" value="20/10 * * * * ?" />
   bean>

   
   <bean id="send_email2"
        class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
      <property name="targetObject">
         <ref bean="myjob" />
      property>
      <property name="targetMethod" value="sendExcel" />
      <property name="concurrent" value="false" />
   bean>
   
   
   <bean id="sendMemeberExcel"
        class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
      <property name="jobDetail" ref="send_email2" />
      <property name="cronExpression" value="20/10 * * * * ?" />
   bean>
   
      
      <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
         <property name="triggers">
            <list>
               
               
               
               <ref bean="sendMemeberExcel" />
            list>
         property>
      bean>
beans>

 

你可能感兴趣的:(Java计划任务(任务调度),定时器,定时安排任务,如定时发送Excel表格到用户邮箱(quartz+poi+javax.mail))