Repository: camel Updated Branches: refs/heads/master 977604bd7 -> 560844c2c
CAMEL-10057: add optional JobRegistry Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/697ff58c Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/697ff58c Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/697ff58c Branch: refs/heads/master Commit: 697ff58ced3f38f75119e0fa60314087df5818c9 Parents: 977604b Author: asegarra <angelsega...@outlook.com> Authored: Fri Jul 29 14:27:26 2016 -0400 Committer: asegarra <angelsega...@outlook.com> Committed: Fri Jul 29 15:02:37 2016 -0400 ---------------------------------------------------------------------- components/camel-spring-batch/pom.xml | 5 + .../src/main/docs/spring-batch.adoc | 9 +- .../spring/batch/SpringBatchComponent.java | 16 +- .../spring/batch/SpringBatchEndpoint.java | 33 +++- .../spring/batch/SpringBatchProducer.java | 12 +- .../SpringBatchComponentAutoConfiguration.java | 70 ++++---- .../SpringBatchComponentConfiguration.java | 52 +++--- .../spring/batch/SpringBatchEndpointTest.java | 96 ++++++++++- .../batch/SpringBatchJobRegistryTest.java | 170 +++++++++++++++++++ 9 files changed, 390 insertions(+), 73 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/697ff58c/components/camel-spring-batch/pom.xml ---------------------------------------------------------------------- diff --git a/components/camel-spring-batch/pom.xml b/components/camel-spring-batch/pom.xml index 7daa107..dbfe869 100644 --- a/components/camel-spring-batch/pom.xml +++ b/components/camel-spring-batch/pom.xml @@ -78,6 +78,11 @@ <artifactId>camel-spring</artifactId> <scope>test</scope> </dependency> + <dependency> + <groupId>org.apache.camel</groupId> + <artifactId>camel-spring-javaconfig</artifactId> + <scope>test</scope> + </dependency> </dependencies> </project> http://git-wip-us.apache.org/repos/asf/camel/blob/697ff58c/components/camel-spring-batch/src/main/docs/spring-batch.adoc ---------------------------------------------------------------------- diff --git a/components/camel-spring-batch/src/main/docs/spring-batch.adoc b/components/camel-spring-batch/src/main/docs/spring-batch.adoc index bf82d27..fd5f999 100644 --- a/components/camel-spring-batch/src/main/docs/spring-batch.adoc +++ b/components/camel-spring-batch/src/main/docs/spring-batch.adoc @@ -29,7 +29,8 @@ spring-batch:jobName[?options] ------------------------------ Where *jobName* represents the name of the Spring Batch job located in -the Camel registry. +the Camel registry. Alternatively if a JobRegistry is provided it will be used +to locate the job instead. WARNING:This component can only be used to define producer endpoints, which means that you cannot use the Spring Batch component in a `from()` @@ -50,6 +51,7 @@ The Spring Batch component supports 1 options which are listed below. |======================================================================= | Name | Java Type | Description | jobLauncher | JobLauncher | Explicitly specifies a JobLauncher to be used. +| jobRegistry | JobRegistry | Explicitly specifies a JobRegistry to be used. |======================================================================= {% endraw %} // component options: END @@ -57,6 +59,7 @@ The Spring Batch component supports 1 options which are listed below. + // endpoint options: START The Spring Batch component supports 5 endpoint options which are listed below: @@ -65,8 +68,9 @@ The Spring Batch component supports 5 endpoint options which are listed below: |======================================================================= | Name | Group | Default | Java Type | Description | jobName | producer | | String | *Required* The name of the Spring Batch job located in the registry. -| jobFromHeader | producer | false | boolean | Explicitly defines if the jobName shouls be taken from the headers instead of the URI. +| jobFromHeader | producer | | Boolean | Explicitly defines if the jobName shouls be taken from the headers instead of the URI. | jobLauncher | producer | | JobLauncher | Explicitly specifies a JobLauncher to be used. +| jobRegistry | producer | | JobRegistry | Explicitly specifies a JobRegistry to be used. If set it it will be used to locate jobs. | exchangePattern | advanced | InOnly | ExchangePattern | Sets the default exchange pattern when creating an exchange | synchronous | advanced | false | boolean | Sets whether synchronous processing should be strictly used or Camel is allowed to use asynchronous processing (if supported). |======================================================================= @@ -75,6 +79,7 @@ The Spring Batch component supports 5 endpoint options which are listed below: + [[SpringBatch-Usage]] Usage ^^^^^ http://git-wip-us.apache.org/repos/asf/camel/blob/697ff58c/components/camel-spring-batch/src/main/java/org/apache/camel/component/spring/batch/SpringBatchComponent.java ---------------------------------------------------------------------- diff --git a/components/camel-spring-batch/src/main/java/org/apache/camel/component/spring/batch/SpringBatchComponent.java b/components/camel-spring-batch/src/main/java/org/apache/camel/component/spring/batch/SpringBatchComponent.java index 9f7d10c..4a11110 100644 --- a/components/camel-spring-batch/src/main/java/org/apache/camel/component/spring/batch/SpringBatchComponent.java +++ b/components/camel-spring-batch/src/main/java/org/apache/camel/component/spring/batch/SpringBatchComponent.java @@ -20,6 +20,7 @@ import java.util.Map; import org.apache.camel.Endpoint; import org.apache.camel.impl.UriEndpointComponent; +import org.springframework.batch.core.configuration.JobRegistry; import org.springframework.batch.core.launch.JobLauncher; public class SpringBatchComponent extends UriEndpointComponent { @@ -29,6 +30,7 @@ public class SpringBatchComponent extends UriEndpointComponent { private JobLauncher jobLauncher; private JobLauncher defaultResolvedJobLauncher; private Map<String, JobLauncher> allResolvedJobLaunchers; + private JobRegistry jobRegistry; public SpringBatchComponent() { super(SpringBatchEndpoint.class); @@ -36,7 +38,8 @@ public class SpringBatchComponent extends UriEndpointComponent { @Override protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters) throws Exception { - SpringBatchEndpoint endpoint = new SpringBatchEndpoint(uri, this, jobLauncher, defaultResolvedJobLauncher, allResolvedJobLaunchers, remaining); + SpringBatchEndpoint endpoint = new SpringBatchEndpoint(uri, this, jobLauncher, defaultResolvedJobLauncher, + allResolvedJobLaunchers, remaining, jobRegistry); setProperties(endpoint, parameters); return endpoint; } @@ -57,4 +60,15 @@ public class SpringBatchComponent extends UriEndpointComponent { public void setJobLauncher(JobLauncher jobLauncher) { this.jobLauncher = jobLauncher; } + + public JobRegistry getJobRegistry() { + return jobRegistry; + } + + /** + * Explicitly specifies a JobRegistry to be used. + */ + public void setJobRegistry(JobRegistry jobRegistry) { + this.jobRegistry = jobRegistry; + } } \ No newline at end of file http://git-wip-us.apache.org/repos/asf/camel/blob/697ff58c/components/camel-spring-batch/src/main/java/org/apache/camel/component/spring/batch/SpringBatchEndpoint.java ---------------------------------------------------------------------- diff --git a/components/camel-spring-batch/src/main/java/org/apache/camel/component/spring/batch/SpringBatchEndpoint.java b/components/camel-spring-batch/src/main/java/org/apache/camel/component/spring/batch/SpringBatchEndpoint.java index 9581ff9..4f4fcd1 100644 --- a/components/camel-spring-batch/src/main/java/org/apache/camel/component/spring/batch/SpringBatchEndpoint.java +++ b/components/camel-spring-batch/src/main/java/org/apache/camel/component/spring/batch/SpringBatchEndpoint.java @@ -30,6 +30,7 @@ import org.apache.camel.spi.UriParam; import org.apache.camel.spi.UriPath; import org.apache.camel.util.CamelContextHelper; import org.springframework.batch.core.Job; +import org.springframework.batch.core.configuration.JobRegistry; import org.springframework.batch.core.launch.JobLauncher; /** @@ -58,20 +59,26 @@ public class SpringBatchEndpoint extends DefaultEndpoint { private JobLauncher defaultResolvedJobLauncher; private Map<String, JobLauncher> allResolvedJobLaunchers; private Job job; + + @UriParam + private JobRegistry jobRegistry; public SpringBatchEndpoint(String endpointUri, Component component, JobLauncher jobLauncher, JobLauncher defaultResolvedJobLauncher, - Map<String, JobLauncher> allResolvedJobLaunchers, String jobName) { + Map<String, JobLauncher> allResolvedJobLaunchers, String jobName, + JobRegistry jobRegistry) { super(endpointUri, component); this.jobLauncher = jobLauncher; this.defaultResolvedJobLauncher = defaultResolvedJobLauncher; this.allResolvedJobLaunchers = allResolvedJobLaunchers; this.jobName = jobName; + this.jobRegistry = jobRegistry; + } @Override public Producer createProducer() throws Exception { - return new SpringBatchProducer(this, jobLauncher, job); + return new SpringBatchProducer(this, jobLauncher, job, jobRegistry); } @Override @@ -88,9 +95,13 @@ public class SpringBatchEndpoint extends DefaultEndpoint { protected void doStart() throws Exception { if (jobLauncher == null) { jobLauncher = resolveJobLauncher(); - } + } if (job == null && jobName != null && !jobFromHeader) { - job = CamelContextHelper.mandatoryLookup(getCamelContext(), jobName, Job.class); + if(jobRegistry != null) { + job = jobRegistry.getJob(jobName); + } else { + job = CamelContextHelper.mandatoryLookup(getCamelContext(), jobName, Job.class); + } } } @@ -152,7 +163,7 @@ public class SpringBatchEndpoint extends DefaultEndpoint { } /** - * Explicitly defines if the jobName shouls be taken from the headers instead of the URI. + * Explicitly defines if the jobName should be taken from the headers instead of the URI. */ public void setJobFromHeader(boolean jobFromHeader) { this.jobFromHeader = jobFromHeader; @@ -162,4 +173,16 @@ public class SpringBatchEndpoint extends DefaultEndpoint { return jobFromHeader; } + public JobRegistry getJobRegistry() { + return jobRegistry; + } + + /** + * Explicitly specifies a JobRegistry to be used. + */ + public void setJobRegistry(JobRegistry jobRegistry) { + this.jobRegistry = jobRegistry; + } + + } http://git-wip-us.apache.org/repos/asf/camel/blob/697ff58c/components/camel-spring-batch/src/main/java/org/apache/camel/component/spring/batch/SpringBatchProducer.java ---------------------------------------------------------------------- diff --git a/components/camel-spring-batch/src/main/java/org/apache/camel/component/spring/batch/SpringBatchProducer.java b/components/camel-spring-batch/src/main/java/org/apache/camel/component/spring/batch/SpringBatchProducer.java index 6869b52..ddcb028 100644 --- a/components/camel-spring-batch/src/main/java/org/apache/camel/component/spring/batch/SpringBatchProducer.java +++ b/components/camel-spring-batch/src/main/java/org/apache/camel/component/spring/batch/SpringBatchProducer.java @@ -27,6 +27,7 @@ import org.springframework.batch.core.Job; import org.springframework.batch.core.JobExecution; import org.springframework.batch.core.JobParameters; import org.springframework.batch.core.JobParametersBuilder; +import org.springframework.batch.core.configuration.JobRegistry; import org.springframework.batch.core.launch.JobLauncher; /** @@ -37,11 +38,14 @@ public class SpringBatchProducer extends DefaultProducer { private final JobLauncher jobLauncher; private final Job job; + + private final JobRegistry jobRegistry; - public SpringBatchProducer(SpringBatchEndpoint endpoint, JobLauncher jobLauncher, Job job) { + public SpringBatchProducer(SpringBatchEndpoint endpoint, JobLauncher jobLauncher, Job job, JobRegistry jobRegistry) { super(endpoint); this.job = job; this.jobLauncher = jobLauncher; + this.jobRegistry = jobRegistry; } @Override @@ -53,7 +57,11 @@ public class SpringBatchProducer extends DefaultProducer { Job job2run = this.job; if (messageJobName != null) { - job2run = CamelContextHelper.mandatoryLookup(getEndpoint().getCamelContext(), messageJobName, Job.class); + if(jobRegistry != null) { + job2run = jobRegistry.getJob(messageJobName); + } else { + job2run = CamelContextHelper.mandatoryLookup(getEndpoint().getCamelContext(), messageJobName, Job.class); + } } if (job2run == null) { http://git-wip-us.apache.org/repos/asf/camel/blob/697ff58c/components/camel-spring-batch/src/main/java/org/apache/camel/component/spring/batch/springboot/SpringBatchComponentAutoConfiguration.java ---------------------------------------------------------------------- diff --git a/components/camel-spring-batch/src/main/java/org/apache/camel/component/spring/batch/springboot/SpringBatchComponentAutoConfiguration.java b/components/camel-spring-batch/src/main/java/org/apache/camel/component/spring/batch/springboot/SpringBatchComponentAutoConfiguration.java index 3ffd8c6..bb184ca 100644 --- a/components/camel-spring-batch/src/main/java/org/apache/camel/component/spring/batch/springboot/SpringBatchComponentAutoConfiguration.java +++ b/components/camel-spring-batch/src/main/java/org/apache/camel/component/spring/batch/springboot/SpringBatchComponentAutoConfiguration.java @@ -14,39 +14,39 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.apache.camel.component.spring.batch.springboot; - -import java.util.HashMap; -import java.util.Map; -import org.apache.camel.CamelContext; -import org.apache.camel.component.spring.batch.SpringBatchComponent; -import org.apache.camel.util.IntrospectionSupport; -import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; -import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; -import org.springframework.boot.context.properties.EnableConfigurationProperties; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; - -/** - * Generated by camel-package-maven-plugin - do not edit this file! - */ -@Configuration -@EnableConfigurationProperties(SpringBatchComponentConfiguration.class) -public class SpringBatchComponentAutoConfiguration { - - @Bean(name = "spring-batch-component") - @ConditionalOnClass(CamelContext.class) - @ConditionalOnMissingBean(SpringBatchComponent.class) - public SpringBatchComponent configureSpringBatchComponent( - CamelContext camelContext, - SpringBatchComponentConfiguration configuration) throws Exception { - SpringBatchComponent component = new SpringBatchComponent(); - component.setCamelContext(camelContext); - Map<String, Object> parameters = new HashMap<>(); - IntrospectionSupport.getProperties(configuration, parameters, null, - false); - IntrospectionSupport.setProperties(camelContext, - camelContext.getTypeConverter(), component, parameters); - return component; - } +package org.apache.camel.component.spring.batch.springboot; + +import java.util.HashMap; +import java.util.Map; +import org.apache.camel.CamelContext; +import org.apache.camel.component.spring.batch.SpringBatchComponent; +import org.apache.camel.util.IntrospectionSupport; +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +/** + * Generated by camel-package-maven-plugin - do not edit this file! + */ +@Configuration +@EnableConfigurationProperties(SpringBatchComponentConfiguration.class) +public class SpringBatchComponentAutoConfiguration { + + @Bean + @ConditionalOnClass(CamelContext.class) + @ConditionalOnMissingBean(SpringBatchComponent.class) + public SpringBatchComponent configureSpringBatchComponent( + CamelContext camelContext, + SpringBatchComponentConfiguration configuration) throws Exception { + SpringBatchComponent component = new SpringBatchComponent(); + component.setCamelContext(camelContext); + Map<String, Object> parameters = new HashMap<>(); + IntrospectionSupport.getProperties(configuration, parameters, null, + false); + IntrospectionSupport.setProperties(camelContext, + camelContext.getTypeConverter(), component, parameters); + return component; + } } \ No newline at end of file http://git-wip-us.apache.org/repos/asf/camel/blob/697ff58c/components/camel-spring-batch/src/main/java/org/apache/camel/component/spring/batch/springboot/SpringBatchComponentConfiguration.java ---------------------------------------------------------------------- diff --git a/components/camel-spring-batch/src/main/java/org/apache/camel/component/spring/batch/springboot/SpringBatchComponentConfiguration.java b/components/camel-spring-batch/src/main/java/org/apache/camel/component/spring/batch/springboot/SpringBatchComponentConfiguration.java index a1d5b8e..d00cd58 100644 --- a/components/camel-spring-batch/src/main/java/org/apache/camel/component/spring/batch/springboot/SpringBatchComponentConfiguration.java +++ b/components/camel-spring-batch/src/main/java/org/apache/camel/component/spring/batch/springboot/SpringBatchComponentConfiguration.java @@ -14,30 +14,30 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.apache.camel.component.spring.batch.springboot; - -import org.springframework.batch.core.launch.JobLauncher; -import org.springframework.boot.context.properties.ConfigurationProperties; - -/** - * The spring-batch component allows to send messages to Spring Batch for - * further processing. - * - * Generated by camel-package-maven-plugin - do not edit this file! - */ -@ConfigurationProperties(prefix = "camel.component.spring-batch") -public class SpringBatchComponentConfiguration { - - /** - * Explicitly specifies a JobLauncher to be used. - */ - private JobLauncher jobLauncher; - - public JobLauncher getJobLauncher() { - return jobLauncher; - } - - public void setJobLauncher(JobLauncher jobLauncher) { - this.jobLauncher = jobLauncher; - } +package org.apache.camel.component.spring.batch.springboot; + +import org.springframework.batch.core.launch.JobLauncher; +import org.springframework.boot.context.properties.ConfigurationProperties; + +/** + * The spring-batch component allows to send messages to Spring Batch for + * further processing. + * + * Generated by camel-package-maven-plugin - do not edit this file! + */ +@ConfigurationProperties(prefix = "camel.component.spring-batch") +public class SpringBatchComponentConfiguration { + + /** + * Explicitly specifies a JobLauncher to be used. + */ + private JobLauncher jobLauncher; + + public JobLauncher getJobLauncher() { + return jobLauncher; + } + + public void setJobLauncher(JobLauncher jobLauncher) { + this.jobLauncher = jobLauncher; + } } \ No newline at end of file http://git-wip-us.apache.org/repos/asf/camel/blob/697ff58c/components/camel-spring-batch/src/test/java/org/apache/camel/component/spring/batch/SpringBatchEndpointTest.java ---------------------------------------------------------------------- diff --git a/components/camel-spring-batch/src/test/java/org/apache/camel/component/spring/batch/SpringBatchEndpointTest.java b/components/camel-spring-batch/src/test/java/org/apache/camel/component/spring/batch/SpringBatchEndpointTest.java index c6259b3..45d79c2 100644 --- a/components/camel-spring-batch/src/test/java/org/apache/camel/component/spring/batch/SpringBatchEndpointTest.java +++ b/components/camel-spring-batch/src/test/java/org/apache/camel/component/spring/batch/SpringBatchEndpointTest.java @@ -31,6 +31,7 @@ import org.apache.camel.impl.JndiRegistry; import org.apache.camel.impl.SimpleRegistry; import org.apache.camel.test.junit4.CamelTestSupport; import org.apache.commons.lang.reflect.FieldUtils; +import static org.junit.Assert.assertSame; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.ArgumentCaptor; @@ -43,6 +44,7 @@ import org.springframework.batch.core.launch.JobLauncher; import static org.mockito.BDDMockito.*; +import org.springframework.batch.core.configuration.JobRegistry; @RunWith(MockitoJUnitRunner.class) public class SpringBatchEndpointTest extends CamelTestSupport { @@ -53,6 +55,9 @@ public class SpringBatchEndpointTest extends CamelTestSupport { @Mock JobLauncher alternativeJobLauncher; + + @Mock + JobRegistry jobRegistry; @Mock Job job; @@ -78,6 +83,10 @@ public class SpringBatchEndpointTest extends CamelTestSupport { to("spring-batch:fake?jobFromHeader=true"). errorHandler(deadLetterChannel("mock:error")). to("mock:test"); + from("direct:dynamicWithJobRegistry"). + to("spring-batch:fake?jobFromHeader=true&jobRegistry=#jobRegistry"). + errorHandler(deadLetterChannel("mock:error")). + to("mock:test"); } }; } @@ -89,6 +98,7 @@ public class SpringBatchEndpointTest extends CamelTestSupport { registry.bind("alternativeJobLauncher", alternativeJobLauncher); registry.bind("mockJob", job); registry.bind("dynamicMockjob", dynamicMockjob); + registry.bind("jobRegistry", jobRegistry); return registry; } @@ -134,14 +144,33 @@ public class SpringBatchEndpointTest extends CamelTestSupport { mockEndpoint.assertIsSatisfied(); errorEndpoint.assertIsSatisfied(); } + + @Test + public void dynamicJobWorksIfHeaderPresentWithValidJobLocatedInJobRegistry() throws Exception { + + mockEndpoint.expectedMessageCount(1); + errorEndpoint.expectedMessageCount(0); + + Job mockJob = mock(Job.class); + when(jobRegistry.getJob(eq("dyanmicMockJobFromJobRegistry"))).thenReturn(mockJob); + final Map<String, Object> headers = new HashMap<>(); + headers.put(SpringBatchConstants.JOB_NAME, "dyanmicMockJobFromJobRegistry"); + headers.put("jobRegistry", "#jobRegistry"); + + sendBody("direct:dynamicWithJobRegistry", "Start the job, please.", headers); + + mockEndpoint.assertIsSatisfied(); + errorEndpoint.assertIsSatisfied(); + } + @Test public void shouldInjectJobToEndpoint() throws IllegalAccessException { SpringBatchEndpoint batchEndpoint = getMandatoryEndpoint("spring-batch:mockJob", SpringBatchEndpoint.class); Job batchEndpointJob = (Job) FieldUtils.readField(batchEndpoint, "job", true); assertSame(job, batchEndpointJob); } - + @Test public void shouldRunJob() throws Exception { // When @@ -332,7 +361,7 @@ public class SpringBatchEndpointTest extends CamelTestSupport { JobLauncher batchEndpointJobLauncher = (JobLauncher) FieldUtils.readField(batchEndpoint, "jobLauncher", true); assertSame(jobLauncher, batchEndpointJobLauncher); } - + @Test public void shouldUseJobLauncherFromComponent() throws Exception { // Given @@ -354,4 +383,67 @@ public class SpringBatchEndpointTest extends CamelTestSupport { assertSame(alternativeJobLauncher, batchEndpointJobLauncher); } + @Test + public void shouldInjectJobRegistryByReferenceName() throws Exception { + // Given + Job mockJob = mock(Job.class); + when(jobRegistry.getJob(eq("mockJob"))).thenReturn(mockJob); + + context().addRoutes(new RouteBuilder() { + @Override + public void configure() throws Exception { + from("direct:jobRegistryRefTest").to("spring-batch:mockJob?jobRegistry=#jobRegistry"); + } + }); + + // When + template.sendBody("direct:jobRegistryRefTest", "Start the job, please."); + + // Then + SpringBatchEndpoint batchEndpoint = context().getEndpoint("spring-batch:mockJob?jobRegistry=#jobRegistry", SpringBatchEndpoint.class); + JobRegistry batchEndpointJobRegistry = (JobRegistry) FieldUtils.readField(batchEndpoint, "jobRegistry", true); + assertSame(jobRegistry, batchEndpointJobRegistry); + } + + @Test + public void shouldUseJobRegistryFromComponent() throws Exception { + // Given + SpringBatchComponent batchComponent = new SpringBatchComponent(); + batchComponent.setJobRegistry(jobRegistry); + batchComponent.setJobLauncher(jobLauncher); + context.addComponent("customBatchComponent", batchComponent); + + // When + context().addRoutes(new RouteBuilder() { + @Override + public void configure() throws Exception { + from("direct:startCustom").to("customBatchComponent:mockJob"); + } + }); + + // Then + SpringBatchEndpoint batchEndpoint = context().getEndpoint("customBatchComponent:mockJob", SpringBatchEndpoint.class); + JobRegistry batchEndpointJobRegistry = (JobRegistry) FieldUtils.readField(batchEndpoint, "jobRegistry", true); + assertSame(jobRegistry, batchEndpointJobRegistry); + } + + @Test + public void shouldGetJobFromJobRegistry() throws Exception { + // Given + Job mockJobFromJobRegistry = mock(Job.class); + when(jobRegistry.getJob(eq("mockJobFromJobRegistry"))).thenReturn(mockJobFromJobRegistry); + + // When + context().addRoutes(new RouteBuilder() { + @Override + public void configure() throws Exception { + from("direct:jobRegistryTest").to("spring-batch:mockJobFromJobRegistry?jobRegistry=#jobRegistry"); + } + }); + + // Then + SpringBatchEndpoint batchEndpoint = context().getEndpoint("spring-batch:mockJobFromJobRegistry?jobRegistry=#jobRegistry", SpringBatchEndpoint.class); + Job batchEndpointJob = (Job) FieldUtils.readField(batchEndpoint, "job", true); + assertSame(mockJobFromJobRegistry, batchEndpointJob); + } } http://git-wip-us.apache.org/repos/asf/camel/blob/697ff58c/components/camel-spring-batch/src/test/java/org/apache/camel/component/spring/batch/SpringBatchJobRegistryTest.java ---------------------------------------------------------------------- diff --git a/components/camel-spring-batch/src/test/java/org/apache/camel/component/spring/batch/SpringBatchJobRegistryTest.java b/components/camel-spring-batch/src/test/java/org/apache/camel/component/spring/batch/SpringBatchJobRegistryTest.java new file mode 100755 index 0000000..cd56e78 --- /dev/null +++ b/components/camel-spring-batch/src/test/java/org/apache/camel/component/spring/batch/SpringBatchJobRegistryTest.java @@ -0,0 +1,170 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.spring.batch; + +import org.apache.camel.ConsumerTemplate; +import org.apache.camel.EndpointInject; +import org.apache.camel.ExchangePattern; +import org.apache.camel.ProducerTemplate; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.mock.MockEndpoint; +import org.apache.camel.component.spring.batch.support.CamelItemProcessor; +import org.apache.camel.component.spring.batch.support.CamelItemReader; +import org.apache.camel.component.spring.batch.support.CamelItemWriter; +import org.apache.camel.component.spring.batch.support.CamelJobExecutionListener; +import org.apache.camel.spring.javaconfig.SingleRouteCamelConfiguration; +import org.apache.camel.test.spring.CamelSpringDelegatingTestContextLoader; +import org.apache.camel.test.spring.CamelSpringRunner; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.batch.core.Job; +import org.springframework.batch.core.JobExecutionListener; +import org.springframework.batch.core.Step; +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.core.configuration.support.ApplicationContextFactory; +import org.springframework.batch.core.configuration.support.GenericApplicationContextFactory; +import org.springframework.batch.item.ItemProcessor; +import org.springframework.batch.item.ItemReader; +import org.springframework.batch.item.ItemWriter; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Import; +import org.springframework.test.annotation.DirtiesContext; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests; + +@RunWith(CamelSpringRunner.class) +@ContextConfiguration(classes = SpringBatchJobRegistryTest.ContextConfig.class, loader = CamelSpringDelegatingTestContextLoader.class) +public class SpringBatchJobRegistryTest extends AbstractJUnit4SpringContextTests { + + @EndpointInject(uri = "mock:output") + MockEndpoint outputEndpoint; + + @EndpointInject(uri = "mock:jobExecutionEventsQueue") + MockEndpoint jobExecutionEventsQueueEndpoint; + + @Autowired + protected ProducerTemplate template; + + @Autowired + protected ConsumerTemplate consumer; + + String[] inputMessages = new String[]{"foo", "bar", "baz", null}; + + @Before + public void setUp() throws Exception { + + for (String message : inputMessages) { + template.sendBody("seda:inputQueue", message); + } + } + + + @DirtiesContext + @Test + public void testJobRegistry() throws InterruptedException { + outputEndpoint.expectedBodiesReceived("Echo foo", "Echo bar", "Echo baz"); + + template.sendBody("direct:start", "Start batch!"); + + outputEndpoint.assertIsSatisfied(); + } + + + @Configuration + @Import(value = BatchConfig.class) + public static class ContextConfig extends SingleRouteCamelConfiguration { + @Override + public RouteBuilder route() + { + return new RouteBuilder() { + @Override + public void configure() { + from("direct:start").to("spring-batch:echoJob?jobRegistry=#jobRegistry"); + from("direct:processor").setExchangePattern(ExchangePattern.InOut).setBody(simple("Echo ${body}")); + } + }; + } + } + + @EnableAutoConfiguration + @EnableBatchProcessing(modular = true) + public static class BatchConfig { + + @Bean + public ApplicationContextFactory testJobs () { + return new GenericApplicationContextFactory(ChildBatchConfig.class); + } + } + + @Configuration + public static class ChildBatchConfig { + + @Autowired + private JobBuilderFactory jobs; + + @Autowired + private StepBuilderFactory steps; + + @Autowired + ConsumerTemplate consumerTemplate; + + @Autowired + ProducerTemplate producerTemplate; + + @Bean + protected ItemReader reader() throws Exception { + return new CamelItemReader(consumerTemplate, "seda:inputQueue"); + } + + @Bean + protected ItemWriter writer() throws Exception { + return new CamelItemWriter(producerTemplate, "mock:output"); + } + + @Bean + protected ItemProcessor processor() throws Exception { + return new CamelItemProcessor(producerTemplate, "direct:processor"); + } + + @Bean + protected JobExecutionListener jobExecutionListener() throws Exception { + return new CamelJobExecutionListener(producerTemplate, "mock:jobExecutionEventsQueue"); + } + + @Bean + public Job echoJob() throws Exception { + return this.jobs.get("echoJob").start(echoStep()).build(); + } + + @Bean + protected Step echoStep() throws Exception { + return this.steps.get("echoStep") + .chunk(3) + .reader(reader()) + .processor(processor()) + .writer(writer()) + .build(); + } + + } +}