使用 Spring scheduling 实现简单跑批 demo

3 篇文章 0 订阅
1 篇文章 0 订阅
工作上需要做一个秒杀系统,在web端将秒杀信息插入redis中缓存起来,后台使用跑批任务将redis中的数据依次插入mysql中。由web端和redis来控制秒杀数量,后台只实现插入mysql库中。
不知道这个方法是否适合做秒杀系统,写出来希望大家来指点一下。

代码片段

Spring版本:4.3.6.RELEASE

  • 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"
       xmlns:task="http://www.springframework.org/schema/task"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
        http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd">


    <context:component-scan base-package="indi.smt.receiver"/>

    <context:annotation-config />

    <task:annotation-driven scheduler="myScheduler" executor="myExecutor"/>

    <task:executor id="myExecutor" pool-size="5" />

    <task:scheduler id="myScheduler" pool-size="10" />

    <task:scheduled-tasks>
        <task:scheduled ref="jobComponent" method="doJob" cron="11/3 * * * * ?"/>
    </task:scheduled-tasks>

</beans>
  • Job类
package indi.smt.receiver.task;

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

/**
 * @author 無痕剑
 * @date 2017/3/7 14:31
 */
@Component("jobComponent")
public class Job {

    @Scheduled(cron = "/3 * * * * *")
    public void doJob(){
        System.out.println("执行。。。。");
    }
}
  • AppTest类
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @author 無痕剑
 * @date 2017/3/7 16:46
 */
public class AppTest {

    public static void main(String[] args) {
        ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:/spring-*.xml");
        for (int i = 1;;i++) {
            System.out.println("正在循环"+i+"。。。");
            try {
                Thread.sleep(10000l);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

这里需要注意的问题:

  1. 最关键的一点,导致我的程序一直报错,是由于cron = "*/5 * * * * *"这个cron表达式,有关cron表达式可以参考网上其他博客,非常详细,在使用在线cron表达式解析的时候,参考该条非常有必要。在这里,表示每5秒执行一次,必须是“*/5”,不能只写”/5”,否则会报错:

      java.lang.IllegalStateException: Encountered invalid @Scheduled method 'doJob': For input string: ""
    
  2. spring.xml中,为<task:annotation-driven/>标签配置以下两个标签是为了多线程使用,如果只使用单线程,可以不写以下两个标签:<task:executor/> 和 <task:scheduler/>

  3. 使用<task:scheduled-tasks>标签和Job.doJob()方法上配置注解@Scheduled(cron = "/5 * * * * *")只需要使用一个就可以了,这里我更喜欢注解模式。
  4. 启动项目实际上只需要一句ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:/spring-*.xml"); 就会自己执行了。
  5. 在spring.xml中配置
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Scheduling 是操作系统中的一个重要功能,其主要目的是为了有效地利用系统资源,并提高系统的性能。Scheduling实现原理主要包括以下几个方面: 1. 调度算法:调度算法是指操作系统中用来选择下一个要执行的进程的规则。常见的调度算法有先来先服务(FCFS)、最短作业优先(SJF)、时间片轮转(RR)等。不同的调度算法会对系统的吞吐量、响应时间、等待时间等指标产生不同的影响。 2. 进程优先级:操作系统中的进程通常会被赋予不同的优先级,优先级高的进程会被优先调度执行。不同的操作系统对进程优先级的实现方式有所不同,比如 Windows 中采用的是基于时间片和优先级的抢占式调度,Linux 中采用的是基于动态优先级的时间片轮转调度。 3. 上下文切换:当操作系统从一个进程切换到另一个进程时,需要保存当前进程的状态并加载下一个进程的状态。这个过程称为上下文切换,它会带来一定的开销。操作系统中通常会采用一些优化措施来减少上下文切换的次数,比如预测下一个要执行的进程、减少进程的创建和销毁等。 4. 多核处理器的调度:在多核处理器中,操作系统需要将不同的进程分配到不同的核心上执行。这需要考虑到负载均衡、数据共享、线程通信等因素,同时还需要协调不同核心之间的竞争和互斥。 总之,Scheduling实现原理涉及到许多方面,需要综合考虑各种因素来实现高效的调度。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值