spring定时任务轮询(spring Task)

定时任务轮询比如任务自服务器启动就开始运行,并且每隔5秒执行一次。

以下用spring注解配置定时任务。
1、添加相应的schema

  

完整schema如下:

  

2、配置自动调度的包和定时开关

1
2
3
4
5
6
<!-- 注解扫描包 -->
< context:component-scan base-package="com.ljq.web.controller.annotation" />
<!-- Enables the Spring Task @Scheduled programming model -->
< task:executor id="executor" pool-size="5" />
< task:scheduler id="scheduler" pool-size="10" />
< task:annotation-driven executor="executor" scheduler="scheduler" />

  

3、添加调度测试类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package com.ljq.web.controller.annotation;
 
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
 
/**
  * 调度测试类(每隔5秒执行一次)
  *
  * @author Administrator
  *
  */
@Service
public class TaskTest {
     @Scheduled (cron = "0/5 * * * * ? " )
     public void myTestWork() {
         System.out.println(System.currentTimeMillis());
     }
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Spring Cloud中的负载均衡算法,其中轮询算法是其中一种常用的负载均衡算法。在Ribbon中,默认的负载均衡算法就是轮询算法。轮询算法的原理是按照一定顺序依次将请求分发给各个服务实例,当请求到达最后一个服务实例后,再从头开始分发。这样可以平均地将请求分配给每个服务实例,实现负载均衡的效果。 在Spring Cloud中,我们也可以自定义负载均衡算法。可以通过实现LoadBalancer接口来自定义负载均衡算法,根据具体的业务逻辑进行构建。例如,可以实现一个随机负载均衡算法的业务类,通过生成一个随机数来选择要请求的服务实例。 需要注意的是,Spring Cloud中还提供了其他的负载均衡算法,如加权轮询算法、最少连接算法等。可以根据实际需求选择合适的负载均衡算法来进行服务调用。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [Spring cloud入门-9:服务调用-Ribbon-轮询算法原理&源码解析&自定义负载均衡算法](https://blog.csdn.net/xueping_wu/article/details/122519764)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* [【附答案解析】最常⻅的 SpringCloud 微服务⾯试题(VIP典藏版)](https://download.csdn.net/download/2301_77531618/87868535)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值