免费发布供求信息
热门搜: 无锡  赣州  南京  工业  阳电  作用  区别  消毒柜  纽曼  散热器  新能源  插槽 
当前位置: 首页 » 投稿 » 美文摘要 » 正文

scheduled注解(scheduled怎么配置有返回值的方法)

放大字体  缩小字体 发布日期:2024-07-08 11:48:44  作者:[db:新闻资讯作者]  浏览次数:20
核心提示:本文目录scheduled怎么配置有返回值的方法Spring使用@Scheduled进行定时任务,定的时间可否变spring的@scheduled定时怎么加返回

本文目录

  • scheduled怎么配置有返回值的方法
  • Spring使用@Scheduled进行定时任务,定的时间可否变
  • spring的@scheduled定时怎么加返回值
  • springboot 怎么用@scheduled注解
  • java sprinng @Scheduled 定时器注解问题
  • scheduled的使用注解的方式进行调度 可以设置启动时间 还有结束时间么
  • 使用@Scheduled注解方式的定时器,怎么让它停止
  • @Scheduled(cron = “0/5 * * * * *“)将时间改为配置

scheduled怎么配置有返回值的方法

1、spring的@Scheduled注解 需要写在实现上、2、 定时器的任务方法不能有返回值(如果有返回值,spring初始化的时候会告诉你有个错误、需要设定一个proxytargetclass的某个值为true、具体就去百度google吧)3、实现类上要有组件的注解@Component

Spring使用@Scheduled进行定时任务,定的时间可否变

定时任务的实现方式有多种,例如JDK自带的Timer+TimerTask方式,Spring 3.0以后的调度任务(Scheduled Task),Quartz等。Timer+TimerTask是最基本的解决方案,但是比较远古了,这里不再讨论。Spring自带的Scheduled Task是一个轻量级的定时任务调度器,支持固定时间(支持cron表达式)和固定时间间隔调度任务,支持线程池管理。以上两种方式有一个共同的缺点,那就是应用服务器集群下会出现任务多次被调度执行的情况,因为集群的节点之间是不会共享任务信息的,每个节点上的任务都会按时执行。Quartz是一个功能完善的任务调度框架,特别牛叉的是它支持集群环境下的任务调度,当然代价也很大,需要将任务调度状态序列化到数据库。Quartz框架需要10多张表协同,配置繁多,令人望而却步...经过折中考虑,还是选择了Spring的Scheduled Task来实现定时任务。如下:1. Spring配置文件application-context.xml中添加task命名空间和描述。 view plain copy《beans xmlns=““ xmlns:task=““ xsi:schemaLocation=“ /spring-beans.xsd /spring-task.xsd“》 2. 添加调度器和线程池声明。 view plain copy《task:executor id=“taskExecutor“ pool-size=“10“ /》 《task:annotation-driven executor=“taskExecutor“ /》 3. 实现调度方法。基本结构如下: view plain copypackage com.netease.yx.service; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Service; @Service public class ScheduledService { @Scheduled(cron = “0 0 5 * * *“) public void build() { System.out.println(“Scheduled Task“); } } @Scheduled注解支持秒级的cron表达式,上述声明表示每天5点执行build任务。前文已经提过,这种方式在单台应用服务器上运行没有问题,但是在集群环境下,会造成build任务在5点的时候运行多次,遗憾的是,Scheduled Task在框架层面没有相应的解决方案,只能靠程序员在应用级别进行控制。如何控制看1. 无非是一个任务互斥访问的问题,声明一把全局的逗锁地作为互斥量,哪个应用服务器拿到这把逗锁地,就有执行任务的权利,未拿到逗锁地的应用服务器不进行任何任务相关的操作。2.这把逗锁地最好还能在下次任务执行时间点前失效。在项目中我将这个互斥量放在了redis缓存里,1小时过期,这个过期时间是由任务调度的间隔时间决定的,只要小于两次任务执行时间差,大于集群间应用服务器的时间差即可。完整定时任务类如下: view plain copypackage com.netease.yx.service; import javax.annotation.Resource; import org.apache.commons.lang3.time.DateUtils; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Service; import com.netease.yx.service.ICacheService; @Service public class ScheduledService { @Resource private ICacheService cache = null; private static String CACHE_LOCK = “cache_lock“; private static int EXPIRE_PERIOD = (int)DateUtils.MILLIS_PER_HOUR / 1000; @Scheduled(cron = “0 0 5 * * *“) public void build() { if (cache.get(CACHE_LOCK) == null) { cache.set(CACHE_LOCK, true, EXPIRE_PERIOD); doJob(); } } }

spring的@scheduled定时怎么加返回值

首先要配置我们的spring.xmlxmlns 多加下面的内容、然后xsi:schemaLocation多加下面的内容、最后是我们的task任务扫描注解 view plaincopy《task:annotation-driven/》 我的配置扫描位置是: view plaincopy《context:annotation-config/》《bean class=“org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor“/》《context:component-scan base-package=“com.test“/》 扫描的是com.test这样的包下的内容、下面需要接口和实现(我的这几个java文件都是com.test的包下的、) view plaincopypublic interface IMyTestService {public void myTest(); } view plaincopy@Component //import org.springframework.stereotype.Component; public class MyTestServiceImpl implements IMyTestService {@Scheduled(cron=“0/5 * * * * ? “) //每5秒执行一次@Overridepublic void myTest(){System.out.println(“进入测试“);} } 执行后控制台就会打印出 进入测试 了需要注意的几点:1、spring的@Scheduled注解 需要写在实现上、2、 定时器的任务方法不能有返回值(如果有返回值,spring初始化的时候会告诉你有个错误、需要设定一个proxytargetclass的某个值为true、具体就去百度google吧)3、实现类上要有组件的注解@Component

springboot 怎么用@scheduled注解

有两种方法:第一种当然你可以把Scheduled写到xml文件中进行配置。第二种在你的类前面添加@PropertySource(“classpath:root/test.props“)然后修改你的@Scheduled(cron=“0/5 * * * * ? “) 为 @Scheduled(cron=“${jobs.schedule}“) 最后test.props 添加 jobs.schedule = 0/5 * * * * ?

java sprinng @Scheduled 定时器注解问题

Spring 中配置定时器 《bean id=“zntask“ class=“com.tjsoft.egoveva.bps.service.imp.TaskServiceImp“》 《property name=“taskdao“》 《ref bean=“taskDAO“/》 《/property》 《/bean》 如果是一个普通的类,不需要注入dao层 《bean id=“zntask“ class=“com.soft.util.quartzTask“》《/bean》 上面的两个bean节点根据自己的情况任选一个,下面的bean节点都是需要的。 《!-- 每隔12小时 执行一次 --》 《bean id=“ZntaskTopSaleJobDetail“ class=“org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean“》 《property name=“targetObject“ ref=“zntask“》《/property》 《property name=“targetMethod“ value=“znrwdb“》《/property》 《/bean》 《bean id=“zntaskTrigger“ class=“org.springframework.scheduling.quartz.CronTriggerBean“》 《property name=“jobDetail“》 《ref bean=“ZntaskTopSaleJobDetail“/》 《/property》 《property name=“cronexpression“》 《value》0 0 0,12 * * ?《/value》 《/property》 《/bean》 《bean class=“org.springframework.scheduling.quartz.SchedulerFactoryBean“》《property name=“triggers“》 《list》 《ref bean=“zntaskTrigger“/》 《/list》《/property》 《/bean》 时间的配置如下: 《value》0 26 16 * * ?value》 时间大小由小到大排列,从秒开始,顺序为 秒,分,时,天,月,年 *为任意 ?为无限制。由此上面所配置的内容就是,在每天的16点26分启动znrwdb方法

scheduled的使用注解的方式进行调度 可以设置启动时间 还有结束时间么

开机时当进入要求输入密码的界面 按Ctrl和Alt的同时按两下Del键 在用户名中输入Administrator并输入相应的密码回车 以这个帐户更改一下时间试试 主板上有个圆形的钮扣式电池 (即CMOS电池)没电了 更换块新的试试 可能是中毒了 下载360安全卫士 查杀一下病毒

使用@Scheduled注解方式的定时器,怎么让它停止

1,通过属性暴露状态2,通关状态或钩子方法控制执行流程3,慎用spring的调度功能。

@Scheduled(cron = “0/5 * * * * *“)将时间改为配置

有两种方法:第一种当然你可以把Scheduled写到xml文件中进行配置。第二种在你的类前面添加@PropertySource(“classpath:root/test.props“)然后修改你的@Scheduled(cron=“0/5 * * * * ? “) 为 @Scheduled(cron=“${jobs.schedule}“) 最后test.props 添加 jobs.schedule = 0/5 * * * * ?

 
关键词: 注解
 
 
相关推荐
热门点击
 
网站首页 | 网站地图 | 广告服务 | 网站留言 | RSS订阅