Spring Boot2.0之 整合XXL-Job

Spring Boot2.0之 整合XXL-Job

参考git上面的 springboot demo

 

创建maven工程:

pom:


  4.0.0
  com.toov5
  springboot-xxl-job
  0.0.1-SNAPSHOT
   
  	
		3.0.1
		2.2

		4.3.14.RELEASE
		2.9.4
		1.8.13
		1.7.25
		2.3.23
		4.12

		9.2.24.v20180105
		4.0.51
		4.5.5
		
		1.3
		4.1
		3.7
		1.5

		0.9.5.2
		5.1.45
		1.3.1
		3.4.5

		2.4.13
		2.3.0

		1.5.10.RELEASE
	 
  
		
			
				
				org.springframework.boot
				spring-boot-starter-parent
				${spring-boot.version}
				pom
				import
			

			
			
				org.eclipse.jetty
				jetty-server
				${jetty-server.version}
			
			
				org.eclipse.jetty
				jetty-util
				${jetty-server.version}
			
			
				org.eclipse.jetty
				jetty-http
				${jetty-server.version}
			
			
				org.eclipse.jetty
				jetty-io
				${jetty-server.version}
			

		
	

	
		
		
			org.springframework.boot
			spring-boot-starter-web
		
		
			org.springframework.boot
			spring-boot-starter-test
			test
		

		
		
			com.xuxueli
			xxl-job-core
			1.9.2-SNAPSHOT
		

	

	
		
			
			
				org.springframework.boot
				spring-boot-maven-plugin
				${spring-boot.version}
				
					
						
							repackage
						
					
				
			
		
	
  
  
  

   controller

 

 

注:

### xxl-job admin address list, such as "http://address" or "http://address01,http://address02"
xxl.job.admin.addresses=http://127.0.0.1:8080/xxl-job-admin

是调度中心的地址

 

 

Spring Boot2.0之 整合XXL-Job_第1张图片

 

 修改job名字:与注解一致

Spring Boot2.0之 整合XXL-Job_第2张图片

 

config  通过config这个类 读取properties中的信息 然后注入到spring boot 容器中

 

项目结构:

Spring Boot2.0之 整合XXL-Job_第3张图片

 

 

Icontroller (做spring boot测试用的)

 

package com.toov5.api.controller;

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

@RestController
public class IndexController {
   @RequestMapping("/index")
   public String index() {
       return "ok";
   }
}

config (读取 properties文件 并且注入到spring 容器中的)

package com.toov5.job.executor.core.config;

import com.xxl.job.core.executor.XxlJobExecutor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

/**
 * xxl-job config
 *
 * @author xuxueli 2017-04-28
 */
@Configuration
@ComponentScan(basePackages = "com.xxl.job.executor.service.jobhandler")
public class XxlJobConfig {
    private Logger logger = LoggerFactory.getLogger(XxlJobConfig.class);

    @Value("${xxl.job.admin.addresses}")
    private String adminAddresses;

    @Value("${xxl.job.executor.appname}")
    private String appName;

    @Value("${xxl.job.executor.ip}")
    private String ip;

    @Value("${xxl.job.executor.port}")
    private int port;

    @Value("${xxl.job.accessToken}")
    private String accessToken;

    @Value("${xxl.job.executor.logpath}")
    private String logPath;

    @Value("${xxl.job.executor.logretentiondays}")
    private int logRetentionDays;


    @Bean(initMethod = "start", destroyMethod = "destroy")
    public XxlJobExecutor xxlJobExecutor() {
        logger.info(">>>>>>>>>>> xxl-job config init.");
        XxlJobExecutor xxlJobExecutor = new XxlJobExecutor();
        xxlJobExecutor.setAdminAddresses(adminAddresses);
        xxlJobExecutor.setAppName(appName);
        xxlJobExecutor.setIp(ip);
        xxlJobExecutor.setPort(port);
        xxlJobExecutor.setAccessToken(accessToken);
        xxlJobExecutor.setLogPath(logPath);
        xxlJobExecutor.setLogRetentionDays(logRetentionDays);

        return xxlJobExecutor;
    }

}

 

 Job

package com.toov5.TestJob;

import org.springframework.stereotype.Component;

import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.IJobHandler;
import com.xxl.job.core.handler.annotation.JobHandler;


@JobHandler(value="myJob")
@Component
public class MyJob extends IJobHandler {
     @Override
    public ReturnT execute(String param) throws Exception {
        System.out.println("XXL-Job传递的参数是:"+param);
        return SUCCESS;
    }
}

 

 logback:




    logback
    

    
       
        
            %d{HH:mm:ss.SSS} %contextName [%thread] %-5level %logger{36} - %msg%n
        
    

    
        ${log.path}
        
            ${log.path}.%d{yyyy-MM-dd}.zip
        
        
            %date %level [%thread] %logger{36} [%file : %line] %msg%n
            
        
    

    
        
        
    

    


  

启动admin的server,然后启动job的spring boot

运行成功:

 Spring Boot2.0之 整合XXL-Job_第4张图片

 

 相关配置 入config 、 logback application.properties 参考github上的demo 修改就可以了

你可能感兴趣的:(XXL-Job)