网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

springtask的使用方法和cron表达式解析

spring 容器依赖

网站建设、成都网站设计服务团队是一支充满着热情的团队,执着、敏锐、追求更好,是创新互联的标准与要求,同时竭诚为客户提供服务是我们的理念。创新互联把每个网站当做一个产品来开发,精雕细琢,追求一名工匠心中的细致,我们更用心!


  org.springframework
  spring-context
  5.0.5.RELEASE

开启任务注解驱动。即扫描的时候扫描 springtask 相关的注解。

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


  
  

准备 Spring 容器 + 定时任务

为了使用 springtask 需要准备 spring 容器和定时任务。通过 main 方法创建 spring 容器。@Scheduled 注解创建定时任务。

package com.mozq.task;

import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
public class TaskDemo {
  @Scheduled(cron="* * * * * ?")
  public void sendOrderMessage(){
    System.out.println("发送订单消息");
  }

  public static void main(String[] args) throws InterruptedException {
    ClassPathXmlApplicationContext app = new ClassPathXmlApplicationContext("spring-task.xml");
    Thread.sleep(1000);
  }
}

参考文档

https://docs.spring.io/spring/docs/5.1.9.RELEASE/spring-framework-reference/integration.html#scheduling

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持创新互联。


文章名称:springtask的使用方法和cron表达式解析
网页URL:http://bjjierui.cn/article/pchhhs.html

其他资讯