第十九章、Spring Boot2.0 Quartz1

 课时八十、Spring Boot2.0 新特性

在 2018年3 月 1 号,Spring Boot 2.0.0.RELEASE正式发布,这是 Spring Boot 1.0 发布 4 年之后第一次重大修订,这里介绍一下新的特性

一、JDK版本最低Java 8,同时支持Java9

Spring Boot 2.0要求Java 8作为最低版本。 许多现有的API已被更新以利用Java 8的特性,例如:接口上的默认方法,函数回调以及新的API

二、第三方库升级

Spring Boot 2.0建立在Spring Framework 5之上,官方已尽可能升级到其它第三方最新稳定版本的jar。 本版本中一些显着的依赖性升级包括:Tomcat 8.5、Flyway 5、Hibernate 5.2、Thymeleaf 3。
 

三、提供响应式 Web 编程支持

(1)使用 Spring WebFlux/WebFlux.fn 提供响应式 Web 编程支持。
(2)为各种组件的响应式编程提供了自动化配置,如:Reactive Spring Data、Reactive Spring Security 等。
(3)用于响应式 Spring Data Cassandra, MongoDB, Couchbase 和 Redis 的自动化配置和启动器 POM。

四、支持HTTP/2

为Tomcat,Undertow和Jetty提供HTTP / 2支持。

五、支持Kotlin

引入对 Kotlin 1.2.x 的支持,并提供了一个 runApplication 函数,让你通过惯用的 Kotlin 来运行 Spring Boot 应用程序。

六、Actuator改进

全新的Actuator架构,支持 Spring MVC, WebFlux 和 Jersey

七、支持Quartz

为了对支持Quartz增加了一个新的starter: spring-boot-starter-quartz 

八、banner支持GIF

为了好玩,Spring Boot 2.0现在支持动画GIF banner

课时八十一、Spring Boot 2.0小彩蛋 :动态 banner

Spring Boot 2.0 提供了很多新特性,其中就有一个小彩蛋:动态 banner,今天我们就先拿这个来尝尝鲜。,具体怎么使用呢?我们在之前的文章介绍过了banner,使用起来还是很简单的,对于GIF的支持,使用起来也是很简单的。

banner配置说明

修改banner.gif的路径
# Banner image file location (jpg or png can also be used).
spring.banner.image.location=classpath:banner.gif

关闭banner显示
# setting banner options[OFF|CONSOLE|LOG].
spring.main.banner-mode=OFF

banner输出模式支持3种:
* CONSOLE:控制台输出,默认方式;
 * OFF:关闭;
* LOG:日志输出方式;

课时八十二、Spring Boot 2.0 quartz:hello world

在使用Quartz的时候,需要用到Quartz的几个对象 :Job(任务), jobDetail(任务定义:使用JobDetail来定义定时任务的实例),Trigger(触发器:任务在什么时候会执行)。

开发步骤

public class MyJob extends QuartzJobBean{

    @Override
    protected void executeInternal(JobExecutionContext arg0) throws JobExecutionException {
        System.out.println("hello,job");
    }

}
**
 * 定义JobDetail和Trigger.
 * @author Administrator
 *
 */
@Configuration
public class MyJobConfig {
    
    //JobDetail.
    @Bean
    public JobDetail myJobDetail() {
        return JobBuilder.newJob(MyJob.class).withIdentity("myJob").storeDurably().build();
    }
    
    //Trigger.
    @Bean
    public Trigger myJobTrigger() {
        //定义具体什么执行时间.  设置一个每3秒执行一次的计划表.
        SimpleScheduleBuilder simpleScheduleBuilder = SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(3).repeatForever();
        //定义触发器.
        return TriggerBuilder.newTrigger().forJob(myJobDetail()).withIdentity("myJobTrigger").withSchedule(simpleScheduleBuilder).build();
    }
    
}

@SpringBootApplication
public class Springboot2QuartzApplication {

    public static void main(String[] args) {
        SpringApplication.run(Springboot2QuartzApplication.class, args);
    }
}
 

课时八十三、Spring Boot 2.0 quartz:job data property和inject service

在实际当中,我们会在Job类中使用Job Data Property和注入Spring的bean等等情况,要如何解呢?

1、job data property 注入步骤

1、    @Bean
    public JobDetail myJobDetail() {
        return JobBuilder.newJob(MyJob.class).withIdentity("myJob").storeDurably()
                .usingJobData("name","沈爱国")
                .usingJobData("age",20)
                .build();
    }

2、写set方法

    private String name;
    private int age;

    public void setName(String name) {
        this.name = name;
    }

    public void setAge(int age) {
        this.age = age;
    }

3、直接打印

    @Override
    protected void executeInternal(JobExecutionContext arg0) throws JobExecutionException {
        System.out.println("hello,job name"+name+"age:"+age);
        helloService.print();
}

2、inject service 注入步骤

1、编写方法


@Service
public class HelloService {


    public void print(){
        System.out.println("print");
    }
}

2、注入

    /**
     * 在spring boot 2.0 时候 直接进行注入
     * 在spring boot 1.0 时候 需要配置
     * **/

    @Autowired
    private HelloService  helloService;

3、调用

    @Override
    protected void executeInternal(JobExecutionContext arg0) throws JobExecutionException {
        System.out.println("hello,job name"+name+"age:"+age);
        helloService.print();


    }

课时八十四、Spring Boot 2.0 quartz:Job持久化

在前面的代码中,Job信息还是存在内存中的,那么如何进行存储到数据库呢?

一、添加依赖

<dependency>
           <groupId>mysql</groupId>
           <artifactId>mysql-connector-java</artifactId>
        </dependency>
        
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
          </dependency>
 

二、添加配置

### datasource
spring.datasource.platform=mysql
spring.datasource.url = jdbc:mysql://localhost:3306/quartz
spring.datasource.username = root
spring.datasource.password = root
spring.datasource.driverClassName = com.mysql.jdbc.Driver
#spring.datasource.schema=classpath:schema/tables_mysql.sql 


### jpa
spring.jpa.database = MYSQL
spring.jpa.show-sql = true
spring.jpa.hibernate.ddl-auto = update


###quartz
spring.quartz.job-store-type=jdbc
spring.quartz.jdbc.initialize-schema=embedded

三、创建Quartz 相关的表

创建的脚本是 tables-mysql.sql,其他的数据库脚本在 org/quartz/impl/dbcjobstore/tables_@@platform@@.sql 下

四、测试

运行项目数据表的数据就创建

猜你喜欢

转载自blog.csdn.net/weixin_38492276/article/details/81146675