zoukankan      html  css  js  c++  java
  • 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中定义的
     }

  • 相关阅读:
    爬虫的基本原理、requests模块、模拟登陆网站、爬取视频网站、cookie池和代理池、正向代理和反向代理
    git初识、
    签发token、校验token、多方式登录签发token的实现、自定义认证反爬规则的认证类、admin使用自定义User表:新增用户密码密文、群查接口各种筛选组件数据准备、drf搜索过滤组件、drf排序过滤组件、drf基础分页组件
    频率认证源码分析、自定义频率认证组件、JWT认证、drf-jwt插件
    auth组件的权限六表 自定义User的权限六表 六表之间的数据访问 三大认证整体源码分析 自定义认证类 系统权限类 自定义权限类
    视图类与序列化类传参、二次封装response类、视图家族、GenericAPIView视图基类、mixins视图6大工具类、generic中的工具视图、视图集、GenericAPIView 与 APIView 作为两大继承视图的区别、工具视图集、路由组件(了解)
    修改文件后缀
    python统计自己微信好友并抓取信息
    关于今日头条小程序(字节跳动小程序)相关问题
    关于飞鱼文档不详细无法拉取数据问题
  • 原文地址:https://www.cnblogs.com/lovefaner/p/10069656.html
Copyright © 2011-2022 走看看