SpringBoot整合JAVA定时任务之SpringTask

1、SpringTask

SpringTask是spring官方给我们推荐的一个轻量级的任务调度框架,SpringTask是不能通过程序的方式控制开启或关闭。

2、SpringTask的使用

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;

@Component
public class TestTask {

    @Scheduled(cron = "0/3 * * * * ?")   //每三秒钟打印一次
    public void test() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = simpleDateFormat.format(System.currentTimeMillis());
        System.out.println("你好......"+format);
    }
}
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;

@SpringBootApplication
@EnableScheduling   //SpringTask任务开启
public class TestApplication {

    public static void main(String[] args) {
        SpringApplication.run(TestApplication.class, args);
    }

}

测试结果:
在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您可以通过配置 Shiro 的过滤器链来实现让 Shiro 放行整个 Spring Task 定时任务类。在 Shiro 的配置文件中,配置一个自定义的过滤器链,将所有与 Spring Task 相关的 URL 都配置为不需要经过 Shiro 进行认证和授权的路径。 首先,定义一个自定义的过滤器链,例如 `TaskFilterChainDefinition`,继承自 `DefaultFilterChainManager`。然后,在该类中重写 `createDefaultFilterChainManager` 方法,配置过滤器链。示例代码如下: ```java public class TaskFilterChainDefinition extends DefaultFilterChainManager { @Override protected void createDefaultFilterChainManager() { // 添加不需要经过 Shiro 过滤器的路径,这里假设定时任务类路径为 /task/** addFilterChain("/task/**", "anon"); // 添加其他需要经过 Shiro 过滤器的路径 // ... super.createDefaultFilterChainManager(); } } ``` 接下来,在 Shiro 的配置文件中,使用自定义的过滤器链。示例代码如下: ```java @Configuration public class ShiroConfig { // ... @Bean public ShiroFilterFactoryBean shiroFilterFactoryBean(DefaultWebSecurityManager securityManager) { ShiroFilterFactoryBean factoryBean = new ShiroFilterFactoryBean(); factoryBean.setSecurityManager(securityManager); // 设置自定义的过滤器链 factoryBean.setFilterChainDefinitionMap(new TaskFilterChainDefinition().getFilterChainMap()); return factoryBean; } // ... } ``` 通过以上配置,Shiro 将会放行整个 Spring Task 定时任务类,即 `/task/**` 路径下的所有请求。请根据您的实际路径进行相应的调整和配置。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值