• 使用spring scheduler完成定时调度


    1.在spring-task.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:task="http://www.springframework.org/schema/task"
           xmlns:context="http://www.springframework.org/schema/context"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/task
            http://www.springframework.org/schema/task/spring-task.xsd
             http://www.springframework.org/schema/context
              http://www.springframework.org/schema/context/spring-context.xsd">
    
        <!--com.zhiyou100.app:表示所要运行类的的包名-->
        <context:component-scan base-package="com.zhiyou100.app"></context:component-scan>
        <!--pool-size:表示线程池的个数有100-->
        <task:scheduler id="taskScheduler" pool-size="100"/>
        <task:scheduled-tasks scheduler="taskScheduler">
            <!--ref:表示定时调度的类注入到spring中的名字(首字母小写其余按照驼峰法命名)
                method:实际运行类中的方法
                cron:表示多久运行一次,* * * * * ?:表示每秒运行一次-->
            <task:scheduled ref="task" method="hello" cron="* * * * * ?"/>
            <task:scheduled ref="task" method="word" cron="* * * * * ?"/>
        </task:scheduled-tasks>
    </beans>

    2.将spring-task.xml中的配置引入spring.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"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context
             http://www.springframework.org/schema/context/spring-context.xsd">
        
        <!--注入注释扫面com.zhiyou100下的文件-->
        <context:component-scan base-package="com.zhiyou100">
            <!--将不包含controller层的内容注入到Spring里面-->
            <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller">    </context:exclude-filter>
        </context:component-scan>
    
        <!--引入定时任务资源-->
        <import resource="spring-task.xml"/>
    </beans>

    3.编写测试类:Task,注意要将此类注入spring中(添加@Component)

    package com.zhiyou100.app;
    
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    import org.springframework.stereotype.Component;
    
    /**
     * 用于测试定时调度
     */
    @Component
    public class Task {
    
        /**
         * hello:表示定时运行的方法
         */
        public void hello(){
            System.out.println("hello");
        }
    
    
        /**
         * word:表示定时运行的方法
         */
        public void word(){
            System.out.println("word");
        }
    
        /**
         * 测试方法
         * @param args
         */
        public static void main(String[] args) {
            new ClassPathXmlApplicationContext("spring.xml");
        }
    
    }
    

    4.运行结果如下

  • 相关阅读:
    使用CefSharp在.NET中嵌入Google kernel
    在WPF中嵌入WebBrowser可视化页面
    C#获取字符串的拼音和首字母
    .NET Core IdentityServer4实战 第六章-Consent授权页
    .NET Core IdentityServer4实战 第Ⅴ章-单点登录
    .NET Core IdentityServer4实战 第Ⅳ章-集成密码登陆模式
    一个C#程序员学习微信小程序路由的笔记
    C#串口通讯概念以及简单实现
    一个C#程序员学习微信小程序的笔记
    使用Http-Repl工具测试ASP.NET Core 2.2中的Web Api项目
  • 原文地址:https://www.cnblogs.com/pigdata/p/10305619.html
Copyright © 2020-2023  润新知