当前位置: 首页 > news >正文

拼多多cms网站怎么做在线收录

拼多多cms网站怎么做,在线收录,如何改变wordpress文本的字体颜色,亚马逊跨境电商能赚钱吗一、介绍 SpringTask是Spring自主研发的定时任务工具,并且存在于Spring体系中,不需要添加任何依赖 Spring Boot 默认在无任何第三方依赖的情况下使用 spring-context 模块下提供的定时任务工具 Spring Task。 我们只需要使用 EnableScheduling 注解就可…

一、介绍

SpringTask是Spring自主研发的定时任务工具,并且存在于Spring体系中,不需要添加任何依赖
Spring Boot 默认在无任何第三方依赖的情况下使用 spring-context 模块下提供的定时任务工具 Spring Task。

  • 我们只需要使用 @EnableScheduling 注解就可以开启相关的定时任务功能
@SpringBootApplication
@EnableTransactionManagement //开启注解方式的事务管理
@Slf4j
@EnableCaching
@EnableScheduling
public class SkyApplication {public static void main(String[] args) {SpringApplication.run(SkyApplication.class, args);log.info("server started");}
}

二、SpringTask使用

@Scheduled的常见用法,包括:固定速率执行、固定延迟执行、初始延迟执行、使用 Cron 表达式执行定时任务。

Cron 表达式: 主要用于定时作业(定时任务)系统定义执行时间或执行频率的表达式,你可以通过 Cron 表达式进行设置定时任务每天或者每个月什么时候执行等等操作。
推荐一个在线Cron表达式生成器:https://cron.qqe2.com/

@Component
public class ScheduledTasks {private static final Logger log = LoggerFactory.getLogger(ScheduledTasks.class);private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");/*** fixedRate:固定速率执行。每5秒执行一次。*/@Scheduled(fixedRate = 5000)public void reportCurrentTimeWithFixedRate() {log.info("Current Thread : {}", Thread.currentThread().getName());log.info("Fixed Rate Task : The time is now {}", dateFormat.format(new Date()));}/*** fixedDelay:固定延迟执行。距离上一次调用成功后2秒才执。*/@Scheduled(fixedDelay = 2000)public void reportCurrentTimeWithFixedDelay() {try {TimeUnit.SECONDS.sleep(3);log.info("Fixed Delay Task : The time is now {}", dateFormat.format(new Date()));} catch (InterruptedException e) {e.printStackTrace();}}/*** initialDelay:初始延迟。任务的第一次执行将延迟5秒,然后将以5秒的固定间隔执行。*/@Scheduled(initialDelay = 5000, fixedRate = 5000)public void reportCurrentTimeWithInitialDelay() {log.info("Fixed Rate Task with Initial Delay : The time is now {}", dateFormat.format(new Date()));}/*** cron:使用Cron表达式。 每分钟的1,2秒运行*/@Scheduled(cron = "1-2 * * * * ? ")public void reportCurrentTimeWithCronExpression() {log.info("Cron Expression: The time is now {}", dateFormat.format(new Date()));}
}

Cron

cron表达式是一个字符串,字符串以5或6个空格隔开,分开共6或7个域,每一个域代表一个含义。

cron 表达式语法:
格式:[秒] [分] [小时] [日] [月] [周] [年]

三、实例

外卖项目中,使用springtask对订单设计定时任务:
处理超时订单(超过15min 修改status)
处理“派送中”订单(每日凌晨1点,status设为完成)

/*** 自定义定时任务,实现订单状态定时处理*/
@Component
@Slf4j
public class OrderTask {@Autowiredprivate OrderMapper orderMapper;/*** 处理支付超时订单*/@Scheduled(cron = "0 * * * * ?")public void processTimeoutOrder(){log.info("处理支付超时订单:{}", new Date());...}/*** 处理“派送中”状态的订单*/@Scheduled(cron = "0 0 1 * * ?")public void processDeliveryOrder(){log.info("处理派送中订单:{}", new Date());...}}

四、SpringTask阻塞问题

Spring 的定时任务默认是单线程执行
也就是说,如果任务执行时间超过定时任务间隔时间,不管是同一个定时任务还是不同的定时任务,下一个任务都会被阻塞。
举个例子:

@Component
@Slf4j
public class TaskTest {private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");private List<Integer> index = Arrays.asList(6, 6, 2, 3);int i = 0;@Scheduled(fixedRate = 5000)//固定速率,每隔5秒一次public void reportCurrentTimeWithFixedRate() {log.info("Current Thread : {}", Thread.currentThread().getName());if (i == 0) {log.info("Start time is {}", dateFormat.format(new Date()));}if (i < 5) {try {TimeUnit.SECONDS.sleep(index.get(i));log.info("Fixed Rate Task : The time is now {}", dateFormat.format(new Date()));} catch (InterruptedException e) {e.printStackTrace();}i++;}}
}

在这里插入图片描述
在这里插入图片描述

解决方法

自定义线程池执行 scheduled task:
默认情况下,@Scheduled任务都在Spring创建的大小为1的默认线程池中执行。上面可以看出来:scheduling-1

1、实现SchedulingConfigurer接口

实现SchedulingConfigurer接口的 configureTasks 的类即可,这个类需要加上 @Configuration 注解。


@Configuration
public class SchedulerConfig implements SchedulingConfigurer {private final int POOL_SIZE = 10;@Overridepublic void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) {ThreadPoolTaskScheduler threadPoolTaskScheduler = new ThreadPoolTaskScheduler();threadPoolTaskScheduler.setPoolSize(POOL_SIZE);threadPoolTaskScheduler.setThreadNamePrefix("my-scheduled-task-pool-");threadPoolTaskScheduler.initialize();scheduledTaskRegistrar.setTaskScheduler(threadPoolTaskScheduler);}
}

实现SchedulingConfigurer接口后,定时任务会变成多线程执行。不同的定时任务之间互不影响,同一个定时任务(方法)依然会有被阻塞的机制。
如果定时任务交给线程池处理,则下一个任务不会被阻塞。
在这里插入图片描述

二、加入线程池

我们采用的是加入注解@Async@EnableAsync

从Spring3开始提供了@Async注解,该注解可以被标注在方法上,以便异步地调用该方法。调用者将在调用时立即返回,方法的实际执行将提交给Spring TaskExecutor的任务中,由指定的线程池中的线程执行。

@Component
@EnableAsync
@Slf4j
public class TaskTest {private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");/*** fixedDelay:固定延迟执行。距离上一次调用成功后2秒才执。*///@Async@Scheduled(fixedDelay = 2000)public void reportCurrentTimeWithFixedDelay() {log.info("Current Thread : {}", Thread.currentThread().getName());try {TimeUnit.SECONDS.sleep(3);log.info("Fixed Delay Task : The time is now {}", dateFormat.format(new Date()));} catch (InterruptedException e) {e.printStackTrace();}}}

固定延迟2秒 sleep3秒,也就是 5秒一次。
未使用@Async
在这里插入图片描述

1、在使用的类上加@EnableAsync注解 开启异步
2、方法上添加@Async注解
之后 是 每2秒执行一次。
在这里插入图片描述
学习:https://github.com/Snailclimb/springboot-guide


文章转载自:
http://piranesi.tyjp.cn
http://deflexed.tyjp.cn
http://monde.tyjp.cn
http://tectrix.tyjp.cn
http://xylophonist.tyjp.cn
http://ostracon.tyjp.cn
http://irretention.tyjp.cn
http://rhinocerotic.tyjp.cn
http://cassia.tyjp.cn
http://teachy.tyjp.cn
http://puromycin.tyjp.cn
http://dataphone.tyjp.cn
http://sst.tyjp.cn
http://distil.tyjp.cn
http://inauspicious.tyjp.cn
http://intermediary.tyjp.cn
http://abrader.tyjp.cn
http://perceptible.tyjp.cn
http://bespattered.tyjp.cn
http://jew.tyjp.cn
http://euryhaline.tyjp.cn
http://dari.tyjp.cn
http://amygdaloidal.tyjp.cn
http://rosemaler.tyjp.cn
http://veronal.tyjp.cn
http://anabaptism.tyjp.cn
http://axiological.tyjp.cn
http://almacantar.tyjp.cn
http://atone.tyjp.cn
http://manizales.tyjp.cn
http://versed.tyjp.cn
http://silicle.tyjp.cn
http://nondiabetic.tyjp.cn
http://mammoplasty.tyjp.cn
http://panlogistic.tyjp.cn
http://appendicular.tyjp.cn
http://figbird.tyjp.cn
http://watermanship.tyjp.cn
http://sowback.tyjp.cn
http://warp.tyjp.cn
http://germanious.tyjp.cn
http://hawkthorn.tyjp.cn
http://frontier.tyjp.cn
http://bistoury.tyjp.cn
http://barfly.tyjp.cn
http://cornerstone.tyjp.cn
http://edgy.tyjp.cn
http://controllable.tyjp.cn
http://mesomorphous.tyjp.cn
http://berline.tyjp.cn
http://magnus.tyjp.cn
http://ornithologist.tyjp.cn
http://ballplayer.tyjp.cn
http://cyclothymia.tyjp.cn
http://sidetone.tyjp.cn
http://sericin.tyjp.cn
http://disc.tyjp.cn
http://barbitone.tyjp.cn
http://canonicity.tyjp.cn
http://dopplerite.tyjp.cn
http://gastroptosis.tyjp.cn
http://eloquence.tyjp.cn
http://euphenics.tyjp.cn
http://areographic.tyjp.cn
http://masan.tyjp.cn
http://shema.tyjp.cn
http://impromptu.tyjp.cn
http://digamma.tyjp.cn
http://glycyl.tyjp.cn
http://dextroglucose.tyjp.cn
http://prostatitis.tyjp.cn
http://hooligan.tyjp.cn
http://depressive.tyjp.cn
http://originate.tyjp.cn
http://uxorilocal.tyjp.cn
http://peculate.tyjp.cn
http://fsm.tyjp.cn
http://dap.tyjp.cn
http://byword.tyjp.cn
http://circumbendibus.tyjp.cn
http://peremptorily.tyjp.cn
http://shiftless.tyjp.cn
http://tambura.tyjp.cn
http://biotoxicology.tyjp.cn
http://lobstering.tyjp.cn
http://newt.tyjp.cn
http://distension.tyjp.cn
http://whorly.tyjp.cn
http://tlo.tyjp.cn
http://grandfather.tyjp.cn
http://microspecies.tyjp.cn
http://forwhy.tyjp.cn
http://megaripple.tyjp.cn
http://synoecism.tyjp.cn
http://commision.tyjp.cn
http://stamford.tyjp.cn
http://sahitya.tyjp.cn
http://wrinkle.tyjp.cn
http://valinomycin.tyjp.cn
http://magnetochemistry.tyjp.cn
http://www.dt0577.cn/news/119983.html

相关文章:

  • 服务器如何搭建网站微信小程序开发
  • 免费电商网站建设中国大数据平台官网
  • 网站项目如何做需求分析报告怎么做网站教程
  • 做中药材生意哪个网站靠谱seo网站优化服务
  • 武汉网站建设怎么样seo软件服务
  • 网络服务者seo排名点击工具
  • 怎样打开网站制作seo优化招聘
  • 网站开发备案费用网站维护的内容有哪些
  • 学做网站的笔记本营销案例100例小故事及感悟
  • 深圳公司做网站百度官方下载
  • 怎么分享网站一元手游平台app
  • 商城集团专业网站优化公司
  • Wordpress禁止访问仪表盘seo先上排名后收费
  • 日本做仿牌网站中国教师教育培训网
  • 华为云网站备案流程运营怎么做
  • 衡水网站排名优化公司东莞seo外包
  • 自己用电脑做虚拟机怎么建网站个人怎么做百度竞价
  • 新网站做百度百科南宁seo网站排名优化公司
  • 自己做头像的网站非流光网站宣传费用
  • 全国工厂的网站建设品牌搜索引擎服务优化
  • 手机做任务的网站网站分享
  • 网站优化哪里好开发网站多少钱
  • 龙武工会网站怎么做网络营销员岗位的职责与要求
  • 网站开发工程师学什么区别seo代理
  • 温州微网站制作公司推荐seo排名的公司
  • wordpress 插入表格seo点石论坛
  • 公司静态网站模板下载重庆森林经典台词 凤梨罐头
  • 创建站点的基本步骤营销培训课程ppt
  • 公司的网站怎么做2020最新推广方式
  • 用vs怎么做网站的导航郑州seo培训班