• Java定时器小实例


    有时候,我们需要在Java中定义一个定时器来轮询操作,比如每隔一段时间查询、删除数据库中的某些数据等,下面记录一下一种简单实现方式

    1,首先新建一个类,类中编写方法来实现业务操作

    public class MailQuartz {
    
    
        @Autowired 
        private MailServiceImpl sendMail;
        
        @Autowired
        private TimerServiceImpl timerServiceImpl;
        
        public void Quartz(){
                String timer = getTimerStatus();
                if(!timer.equals("1")){
                    System.out.println("定时器未开启");
                    return;
                }
                List<T> result = new ArrayList<T>();
                //查询出需要发送邮件的对象
                result = timerServiceImpl.checkSendMail();
                public void deleteOldEInvoices(){
                         timerServiceImpl.deleteOldEInvoices();
                        }
        
        //读取配置文件中的值,开启或者关闭定时器
        public String getTimerStatus(){
            InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("application.properties");
            Properties pro = new Properties();
            try {
                pro.load(inputStream);
            } catch (IOException e) {
                e.printStackTrace();
            }
            return pro.getProperty("timer");
        }
    }
    
                            
    View Code

    这里我们创建了一个类MailQuartz,然后在类中定义了两个方法Quartz和deleteOldEInvoices,并且在这两个方法中,我们实现了调用service处理相应的业务,ok,下面让我们配置其触发方式。

    2,类中的方法触发配置信息,我们写在applicationContext.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"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:aop="http://www.springframework.org/schema/aop"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xmlns:task="http://www.springframework.org/schema/task"
        xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
        http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.1.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
    
    <!-- 定时器(发送票据开具信息给交款人) -->
        <!-- 要调用的工作类 /HebNT/src/heb/nt/service/web/mailQuartz.java -->
        <bean id="MailQuartz" class="heb.nt.service.web.MailQuartz"></bean>
        <!-- 定义调用对象和调用对象的方法 -->
        <bean id="jobtask"
            class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
            <!-- 调用的类 -->
            <property name="targetObject">
                <ref bean="MailQuartz" />
            </property>
            <!-- 调用类中的方法 -->
            <property name="targetMethod">
                <value>Quartz</value>
            </property>
        </bean>
         
        <bean id="jobtask2"
            class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
            <!-- 调用的类 -->
            <property name="targetObject">
                <ref bean="MailQuartz" />
            </property>
            <!-- 调用类中的方法 -->
            <property name="targetMethod">
                <value>deleteOldEInvoices</value>
            </property>
        </bean>
        <!-- 定义触发时间-->
        <bean id="doTime" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
            <property name="jobDetail">
                <ref bean="jobtask" />
            </property>
            <!-- cron表达式 -->
            <property name="cronExpression">
                <!-- 每三分钟执行一次-->
                <value>0 0/5 * * * ? *</value>
            </property>
        </bean>
        <!-- 定义触发时间-->
        <bean id="doTime2" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
            <property name="jobDetail">
                <ref bean="jobtask2" />
            </property>
            <!-- cron表达式 -->
            <property name="cronExpression">
                <!-- 每年每月的一号0点0分0秒执行一次-->
                <value>0 0 0 1 * ? *</value>
            </property>
        </bean>
        <!-- 总管理类 如果将lazy-init='false'那么容器启动就会执行调度程序 -->
        <bean id="startQuertz" lazy-init="false" autowire="no"
            class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
            <property name="triggers">
                <list>
                    <ref bean="doTime" />
                    <ref bean="doTime2" />
                </list>
            </property>
        </bean>
    
    </beans>
    View Code

    查看代码,我们可以发现,需要配置我们类MailQuartz、方法Quartz和deleteOldEInvoices的相关信息,然后触发时间的间隔,我们用corn表达式去约束,这样,我们就可以为实现多个方法实现定时器。

    3,最后呢,为了优化,由于定时器的触发效果是,项目一启动,定时器就会触发,但是在测试阶段或者你不想让定时器触发,因为他会更改你数据库中的测试数据,那么我们就可以在方法之前读取配置文件中的某个变量值,然后做判断,

    String timer = getTimerStatus();    //调用getTimerStatus()方法,取得配置文件中定义的控制值
       if(!timer.equals("1")){      //然后根据值来阻止定时器的运行
        System.out.println("定时器未开启");
        return;
       }

    //读取配置文件中的值,开启或者关闭定时器
     public String getTimerStatus(){
      InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("application.properties");
      Properties pro = new Properties();
      try {
       pro.load(inputStream);
      } catch (IOException e) {
       e.printStackTrace();
      }
      return pro.getProperty("timer");  //这里的timer值就是在application.properties中定义的
     }

  • 相关阅读:
    python学习笔记二
    计网第三章
    python学习笔记一
    Java基础第二十七天总结——网络编程
    Java基础第二十六天总结——IO流
    Java基础第二十五天总结——泛型
    Java基础第二十四天总结——集合
    Java基础第二十三天总结——集合
    Java基础第二十二天总结——枚举类与注解
    Java基础第二十一天总结——日期时间API
  • 原文地址:https://www.cnblogs.com/lovefaner/p/10069656.html
Copyright © 2020-2023  润新知