apache / shardingsphere-elasticjob

Distributed scheduled job
Apache License 2.0
8.15k stars 3.29k forks source link

elasticjob-1.1.1如何与springboot整合 #128

Closed yygymmmmsee closed 8 years ago

yygymmmmsee commented 8 years ago

elasticjob-1.1.1如何与springboot整合?测试了半天,发现启动不报错,但是,zookeeper里面没有任务信息,配置如下:

@Configuration
public class JobEnvConfig {

    @Bean
    public SpringJobScheduler testSpringbootElasticJob(
            @Qualifier("registryCenter") CoordinatorRegistryCenter registryCenter) {
        SimpleJobConfigurationBuilder simpleJobConfigurationBuilder = JobConfigurationFactory.createSimpleJobConfigurationBuilder(
                "testSpringbootElasticJob", TestElasticJob.class, 2, "0 */2 * * * ?").description("test springboot and elasticjob!")
                .failover(true).overwrite(true).disabled(false).shardingItemParameters("").monitorExecution(false);
        JobConfiguration jobConfiguration = simpleJobConfigurationBuilder.build();

        AbstractJobConfigurationDto jobConfigurationDto = new AbstractJobConfigurationDto<JobConfiguration, TestElasticJob, AbstractJobConfigurationBuilder>("testSpringbootElasticJob", JobType.SIMPLE ,TestElasticJob.class, 2, "0 */2 * * * ?") {

            @Override
            protected AbstractJobConfigurationBuilder createCustomizedBuilder() {
                return simpleJobConfigurationBuilder;
            }

            @Override
            protected AbstractJobConfigurationBuilder buildCustomizedProperties(
                    AbstractJobConfigurationBuilder builder) {
                return builder;
            }

            @Override
            public JobConfiguration toJobConfiguration() {
                return jobConfiguration;
            }
        };
        SpringJobScheduler springJobScheduler = new SpringJobScheduler(registryCenter, jobConfigurationDto);

        System.out.println("----------------------------init job-------------------");
        return springJobScheduler;

    }

    //@Bean
    public SimpleJobConfiguration<TestElasticJob> TestElasticJob(){
        SimpleJobConfigurationBuilder simpleJobConfigurationBuilder = JobConfigurationFactory.createSimpleJobConfigurationBuilder(
                "testSpringbootElasticJob", TestElasticJob.class, 2, "0 */2 * * * ?").description("test springboot and elasticjob!")
                .failover(true).overwrite(true).disabled(false);
        SimpleJobConfiguration<TestElasticJob> jobConfiguration = (SimpleJobConfiguration)simpleJobConfigurationBuilder.build();
        System.out.println("----------------------------init job2-------------------");
        return jobConfiguration;
    }

    @Bean
    public ZookeeperRegistryCenter registryCenter(
            @Qualifier("zookeeperConfiguration") ZookeeperConfiguration zookeeperConfiguration) {
        System.out.println("----------------------------init registryCenter-------------------");
        return new ZookeeperRegistryCenter(zookeeperConfiguration);
    }

    @Bean
    public ZookeeperConfiguration zookeeperConfiguration() {
        ZookeeperConfiguration zookeeperConfiguration = new ZookeeperConfiguration("127.0.0.1:2182",
                "job/test", 1000, 3000, 3);

        System.out.println("----------------------------init zookeeperConfiguration-------------------");
        return zookeeperConfiguration;
    }

}

pom文件

<dependency>
            <groupId>com.dangdang</groupId>
            <artifactId>elastic-job-core</artifactId>
            <version>1.1.1</version>
        </dependency>

        <!-- import other module if need -->
        <dependency>
            <groupId>com.dangdang</groupId>
            <artifactId>elastic-job-spring</artifactId>
            <version>1.1.1</version>
        </dependency>

启动信息如下:

2016-09-03 17:13:06 [INFO] com.wsp.example.DemoApplication - No active profile set, falling back to default profiles: default
2016-09-03 17:13:07 [INFO] o.s.b.c.e.AnnotationConfigEmbeddedWebApplicationContext - Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@1283bb96: startup date [Sat Sep 03 17:13:07 CST 2016]; root of context hierarchy
2016-09-03 17:13:10 [INFO] o.s.c.s.PostProcessorRegistrationDelegate$BeanPostProcessorChecker - Bean 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration' of type [class org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration$$EnhancerBySpringCGLIB$$2cdf0cd2] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2016-09-03 17:13:11 [INFO] o.s.b.c.e.t.TomcatEmbeddedServletContainer - Tomcat initialized with port(s): 8082 (http)
2016-09-03 17:13:11 [INFO] org.apache.catalina.core.StandardService - Starting service Tomcat
2016-09-03 17:13:11 [INFO] org.apache.catalina.core.StandardEngine - Starting Servlet Engine: Apache Tomcat/8.5.4
2016-09-03 17:13:11 [INFO] o.a.c.c.C.[Tomcat].[localhost].[/] - Initializing Spring embedded WebApplicationContext
2016-09-03 17:13:11 [INFO] o.s.web.context.ContextLoader - Root WebApplicationContext: initialization completed in 4003 ms
2016-09-03 17:13:11 [INFO] o.s.b.w.servlet.ServletRegistrationBean - Mapping servlet: 'dispatcherServlet' to [/]
2016-09-03 17:13:11 [INFO] o.s.b.web.servlet.FilterRegistrationBean - Mapping filter: 'characterEncodingFilter' to: [/*]
2016-09-03 17:13:11 [INFO] o.s.b.web.servlet.FilterRegistrationBean - Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2016-09-03 17:13:11 [INFO] o.s.b.web.servlet.FilterRegistrationBean - Mapping filter: 'httpPutFormContentFilter' to: [/*]
2016-09-03 17:13:11 [INFO] o.s.b.web.servlet.FilterRegistrationBean - Mapping filter: 'requestContextFilter' to: [/*]
----------------------------init zookeeperConfiguration-------------------
----------------------------init registryCenter-------------------
----------------------------init job-------------------
2016-09-03 17:13:12 [INFO] o.s.w.s.m.m.a.RequestMappingHandlerAdapter - Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@1283bb96: startup date [Sat Sep 03 17:13:07 CST 2016]; root of context hierarchy
2016-09-03 17:13:12 [INFO] o.s.w.s.m.m.a.RequestMappingHandlerMapping - Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> com.wsp.example.controller.AppErrorController.error(javax.servlet.http.HttpServletRequest)
2016-09-03 17:13:12 [INFO] o.s.w.s.m.m.a.RequestMappingHandlerMapping - Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView com.wsp.example.controller.AppErrorController.errorHtml(javax.servlet.http.HttpServletRequest)
2016-09-03 17:13:12 [INFO] o.s.w.s.m.m.a.RequestMappingHandlerMapping - Mapped "{[/index],methods=[GET]}" onto public java.lang.String com.wsp.example.controller.IndexController.index()
2016-09-03 17:13:12 [INFO] o.s.w.s.handler.SimpleUrlHandlerMapping - Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2016-09-03 17:13:12 [INFO] o.s.w.s.handler.SimpleUrlHandlerMapping - Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2016-09-03 17:13:12 [INFO] o.s.w.s.handler.SimpleUrlHandlerMapping - Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2016-09-03 17:13:13 [INFO] o.s.j.e.a.AnnotationMBeanExporter - Registering beans for JMX exposure on startup
2016-09-03 17:13:13 [INFO] o.s.b.c.e.t.TomcatEmbeddedServletContainer - Tomcat started on port(s): 8082 (http)
2016-09-03 17:13:13 [INFO] com.wsp.example.DemoApplication - Started DemoApplication in 7.75 seconds (JVM running for 8.96)
terrymanu commented 8 years ago

有很多同学已经与spring boot整合成功。此功能并非esjob范畴。