SpringMVC + SpringBatch 注解方式

最近接触springbatch处理一些批量的业务数据,由于公司项目是SpringMvc架构,网上大部分SpringBatch都是和SpringBoot搭配使用的,在爬无数个坑后,记录下SpringMvc和SpringBatch搭配方式(ps:注解方式)。

前提是您已然熟悉使用SpringMVC和SpringBatch。

1、创建纯粹的SpringMvc项目

我用的是intellij,项目结构如图


SpringMVC + SpringBatch 注解方式_第1张图片
项目结构

pom文件



    4.0.0

    com.test
    spring_mvc_batch
    war
    1.0-SNAPSHOT

    
        5.0.5.RELEASE
        http://localhost:8080
        UTF-8
        1.8
        1.8
        1.8
    

    
        
            junit
            junit
            4.11
            test
        
        
            javax.servlet
            servlet-api
            compile
            2.5
        
        
            jstl
            jstl
            1.2
        
        
            org.springframework.security
            spring-security-core
            4.2.5.RELEASE
        
        
            org.springframework.security
            spring-security-web
            4.2.5.RELEASE
        
        
            org.springframework.security
            spring-security-config
            4.2.5.RELEASE
        

        
            org.springframework
            spring-core
            ${springframework.version}
        
        
            org.springframework
            spring-expression
            ${springframework.version}
        
        
            org.springframework
            spring-beans
            ${springframework.version}
        
        
            org.springframework
            spring-aop
            ${springframework.version}
        
        
            org.springframework
            spring-aspects
            ${springframework.version}
        
        
            org.aspectj
            aspectjweaver
            1.8.9
        
        
            org.codehaus.jackson
            jackson-core-asl
            1.9.13
        
        
            org.codehaus.jackson
            jackson-mapper-asl
            1.9.13
        
        
            org.springframework
            spring-context
            ${springframework.version}
        
        
            org.springframework
            spring-context-support
            ${springframework.version}
        
        
            org.springframework
            spring-web
            ${springframework.version}
        
        
            org.springframework
            spring-webmvc
            ${springframework.version}
        
        
            org.springframework
            spring-test
            ${springframework.version}
            test
        

        
            org.slf4j
            slf4j-api
            1.7.5
        
        
            log4j
            log4j
            1.2.16
        
        
            org.slf4j
            jcl-over-slf4j
            1.7.5
        
        
            org.slf4j
            log4j-over-slf4j
            1.7.5
        
        
            ch.qos.logback
            logback-classic
            1.0.13
        

        
            com.fasterxml.jackson.core
            jackson-core
            2.9.5
        
        
            com.fasterxml.jackson.core
            jackson-databind
            2.9.5
        
        
            com.fasterxml.jackson.core
            jackson-annotations
            2.9.5
        
    

    
        
            
                org.apache.maven.plugins
                maven-compiler-plugin

                
                    ${java.version}
                    ${java.version}
                    UTF-8
                

            
            
                org.apache.maven.plugins
                maven-war-plugin
                2.5
            
        
    



web.xml



    Archetype Created Web Application
    
    
        dispatcher
        
            org.springframework.web.servlet.DispatcherServlet
        
        
            contextConfigLocation
            classpath:dispatcher-servlet.xml
        
        1
    
    
    
        dispatcher
        /
    
    
        index.jsp
    


dispatcher-servlet.xml



    

    
        
            
                
                    
                        
                            text/html;charset=UTF-8
                        
                    
                
            
        
    

    
    

    
    
        
        
        
    


TestController

package com.test;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {

    @RequestMapping("/hello")
    public String hello(){
        return "Hello world.";
    }

}

好啦,如果启动tomcat,看到如下页面,说明一个纯粹的SpringMvc项目已经搭建好了


SpringMVC + SpringBatch 注解方式_第2张图片
image.png

2、下面开始一步一步加入SpringBatch

SpringMVC + SpringBatch 注解方式_第3张图片
修改后的项目结构

下面备注的是需要添加的部分
修改pom.xml,加入SpringBatch依赖



    4.0.0

    com.test
    spring_mvc_batch
    war
    1.0-SNAPSHOT

    
        5.0.5.RELEASE
        http://localhost:8080
        UTF-8
        1.8
        1.8
        1.8
    

    
        
            junit
            junit
            4.11
            test
        
        
            javax.servlet
            servlet-api
            compile
            2.5
        
        
            jstl
            jstl
            1.2
        
        
            org.springframework.security
            spring-security-core
            4.2.5.RELEASE
        
        
            org.springframework.security
            spring-security-web
            4.2.5.RELEASE
        
        
            org.springframework.security
            spring-security-config
            4.2.5.RELEASE
        

        
            org.springframework
            spring-core
            ${springframework.version}
        
        
            org.springframework
            spring-expression
            ${springframework.version}
        
        
            org.springframework
            spring-beans
            ${springframework.version}
        
        
            org.springframework
            spring-aop
            ${springframework.version}
        
        
            org.springframework
            spring-aspects
            ${springframework.version}
        
        
            org.aspectj
            aspectjweaver
            1.8.9
        
        
            org.codehaus.jackson
            jackson-core-asl
            1.9.13
        
        
            org.codehaus.jackson
            jackson-mapper-asl
            1.9.13
        
        
            org.springframework
            spring-context
            ${springframework.version}
        
        
            org.springframework
            spring-context-support
            ${springframework.version}
        
        
            org.springframework
            spring-web
            ${springframework.version}
        
        
            org.springframework
            spring-webmvc
            ${springframework.version}
        
        
            org.springframework
            spring-test
            ${springframework.version}
            test
        

        
            org.slf4j
            slf4j-api
            1.7.5
        
        
            log4j
            log4j
            1.2.16
        
        
            org.slf4j
            jcl-over-slf4j
            1.7.5
        
        
            org.slf4j
            log4j-over-slf4j
            1.7.5
        
        
            ch.qos.logback
            logback-classic
            1.0.13
        

        
            com.fasterxml.jackson.core
            jackson-core
            2.9.5
        
        
            com.fasterxml.jackson.core
            jackson-databind
            2.9.5
        
        
            com.fasterxml.jackson.core
            jackson-annotations
            2.9.5
        

        
        
            org.springframework.batch
            spring-batch-core
            4.1.2.RELEASE
        
        
        
            mysql
            mysql-connector-java
            5.1.18
        
        
            com.alibaba
            druid
            1.0.12
        
        
        
            org.springframework
            spring-jdbc
            ${springframework.version}
        

    

    
        
            
                org.apache.maven.plugins
                maven-compiler-plugin

                
                    ${java.version}
                    ${java.version}
                    UTF-8
                

            
            
                org.apache.maven.plugins
                maven-war-plugin
                2.5
            
        
    



添加数据库配置文件,用于存储springbatch任务执行情况

custom.jdbc.connection.driverClass=com.mysql.jdbc.Driver

custom.jdbc.connection.jdbcUrl=jdbc:mysql://127.0.0.1:3306/xxxx?useUnicossssde=true&characterEncoding=UTF-8&allowMultiQueries=true
custom.jdbc.connection.user=xxxx
custom.jdbc.connection.password=xxxx

修改dispatcher-servlet.xml,添加数据源和对springmvc定时任务的支持



    
    

    
        
            
                
                    
                        
                            text/html;charset=UTF-8
                        
                    
                
            
        
    

    
    

    
    
        
        
        
    


    
    

    
    
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
    

    
    
        
    

    
    
    

添加SpringBatchJob,我们只简单打印一句话标明SpringBatch运行了;由于SpringBatchJob在咱们配置的SpringMvc扫描包内,所以不用再去声明bean了,SpringMvc会扫描到自动注入的。

package com.test;

import org.springframework.batch.core.*;
import org.springframework.batch.core.annotation.AfterJob;
import org.springframework.batch.core.annotation.BeforeJob;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Map;

@Configuration
@EnableBatchProcessing
public class SpringBatchJob {


    @Autowired
    JobBuilderFactory jobBuilderFactory;
    @Autowired
    StepBuilderFactory stepBuilderFactory;

    @Bean
    @Qualifier("batchJob")
    public Job batchJob() {
        return jobBuilderFactory.get(System.currentTimeMillis() + "")
                .start(step())
                .build();
    }

    @Autowired
    public Step step() {
        return stepBuilderFactory.get("step")
                .tasklet((stepContribution, chunkContext) -> {
                    System.out.println("########################## I am SpringBatch! ##########################");
                    return RepeatStatus.FINISHED;
                })
                .build();

    }
}

添加springMvc定时任务

package com.test;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
public class QuartzTask {

    final static Logger logger = LoggerFactory.getLogger(QuartzTask.class);

    @Autowired
    JobLauncher jobLauncher;

    @Autowired
    Job batchJob;

    /**
     * 定时计算。每分触发
     */
    @Scheduled(cron = "0/5 * * * * ?")
    public void run() {
        try {
            //由于注入bean后,每次调用都是同一个springbatch job实例,且springbatch默认任务是不重复执行的,所以需要每次传递不同参数来每次都执行Springbatch
            //如果有中文参数,需要将springbatch相关表,字符编码改为utf-8,且表中字段字符编码也改为utf-8,不然记录springbatch参数到数据库会报错,导致任务一直无法执行
            JobParameters params = new JobParametersBuilder().addDate("timestamp",new Date()).toJobParameters();
            jobLauncher.run(batchJob, params);

            logger.info("job run ... ");
        } catch (Exception e) {
        }

    }

}

好啦,这时再启动tomcat后,到了任务执行时间就会在控制台输出啦


控制台输出

你可能感兴趣的:(SpringMVC + SpringBatch 注解方式)