スプリングタイマー-デフォルトモード-シングルスレッド

著作権表示:この記事は、CS 4.0の著作権契約に従っているCSDNブロガー "F・F"のオリジナル記事です。転載するには、元のソースリンクとこの声明を添付してください。
元のリンク:https : //blog.csdn.net/weixin_42310288/article/details/80555792

 

構成と使用

    Springのドキュメントによると、デフォルトではシングルスレッドモードが使用されます。これは、複数のタスクの実行が互いに影響し合うことを意味します。たとえば、タスクAの実行に時間がかかる場合、タスクBはタスクAの実行が完了するのを待ってから実行を開始する必要があります。

ステップ1:Spring.xmlのタイマー機能をオンにする


<?xml version="1.0" encoding="UTF-8"?> 

<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 

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

<!-- 让Spring找到被@Component注解的类 -->
<context:component-scan base-package="com.ff.job" /> 

<!--定时器开关 -->
<task:annotation-driven  />

</beans>

ステップ2:タイマークラスを作成する


package com.ff.job;

 

import java.util.concurrent.TimeUnit;

 

import org.apache.log4j.Logger;

import org.springframework.scheduling.annotation.Scheduled;

import org.springframework.stereotype.Component;

 

@Component

public class TaskSchedulingJob {

	

	private static final Logger LOG = Logger.getLogger(TaskSchedulingJob.class);

 

	@Scheduled(cron = "0/5 * * * * ? ")

	public void aTask() {

		LOG.info("a任务每5秒执行一次,执行时间为10秒");

		try {

			TimeUnit.SECONDS.sleep(10);

		} catch (InterruptedException e) {

			LOG.error(e.getMessage(), e);

		}

	}

 

	@Scheduled(cron = "0/5 * * * * ? ")

	public void bTask() {

		LOG.info("b任务每5秒执行一次");

	}

}

ステップ3:Webサーバーを起動して結果を確認する

11:24:15 [spring-job] INFO(TaskSchedulingJob.java:26)-bタスクは5秒ごとに実行11:24:20 [spring-job] INFO(TaskSchedulingJob.java:16)-aタスクは5秒ごと1回実行すると、実行時間は10秒です11:24:30 [spring-job] INFO(TaskSchedulingJob.java:26)-bタスクは5秒ごとに実行されます11:24:35 [spring-job] INFO(TaskSchedulingJob.java: 16)-タスクは5秒ごとに実行され、実行時間は10秒です11:24:45 [spring-job] INFO(TaskSchedulingJob.java:26)-bタスクは5秒ごとに実行され、結果分析:予想されるbタスクは5秒ごと1秒に1回実行されますが、タスクaの実行時間は10秒であり、タスクbの実行に影響します。これは、cron条件を満たす次の5秒までしか実行できません。

 

 

 

 

 

 

元の記事を22件公開 いいね3 ビジター3442

おすすめ

転載: blog.csdn.net/ChyoD1811/article/details/100016391