This is an automated email from the ASF dual-hosted git repository. nferraro pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/camel.git
commit b91d58609a4b94ffb87908c105429b26981d2469 Author: Nicola Ferraro <ni.ferr...@gmail.com> AuthorDate: Mon Jan 13 10:11:10 2020 +0100 CAMEL-14385: support cron component in camel-spring --- .../camel-cron/src/main/docs/cron-component.adoc | 3 +- components/camel-spring/pom.xml | 5 ++ components/camel-spring/src/main/docs/spring.adoc | 26 +++++++++ .../component/cron/CamelSpringCronService.java | 62 ++++++++++++++++++++++ .../camel/component/cron/SpringCronConsumer.java | 36 +++++++++++++ .../camel/component/cron/SpringCronEndpoint.java | 42 +++++++++++++++ ...pache.camel.component.cron.api.CamelCronService | 18 +++++++ .../camel/component/cron/SpringCronRouteTest.java | 41 ++++++++++++++ .../org/apache/camel/component/cron/cron.xml | 36 +++++++++++++ 9 files changed, 268 insertions(+), 1 deletion(-) diff --git a/components/camel-cron/src/main/docs/cron-component.adoc b/components/camel-cron/src/main/docs/cron-component.adoc index aa991e2..b4d2288 100644 --- a/components/camel-cron/src/main/docs/cron-component.adoc +++ b/components/camel-cron/src/main/docs/cron-component.adoc @@ -15,7 +15,8 @@ the implementation of their choice. The following standard Camel components support the Cron endpoints: -- Quartz +- Camel-quartz +- Camel-spring The Cron component is also supported in **Camel K**, which can use the Kubernetes scheduler to trigger the routes when required by the cron expression. Camel K does not require additional libraries to be plugged when using cron expressions compatible with Kubernetes cron syntax. diff --git a/components/camel-spring/pom.xml b/components/camel-spring/pom.xml index 75a83b3..a061848 100644 --- a/components/camel-spring/pom.xml +++ b/components/camel-spring/pom.xml @@ -54,6 +54,11 @@ <artifactId>camel-core-xml</artifactId> </dependency> <dependency> + <groupId>org.apache.camel</groupId> + <artifactId>camel-cron</artifactId> + <optional>true</optional> + </dependency> + <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> </dependency> diff --git a/components/camel-spring/src/main/docs/spring.adoc b/components/camel-spring/src/main/docs/spring.adoc index b3c9a37..cb9dbc9 100644 --- a/components/camel-spring/src/main/docs/spring.adoc +++ b/components/camel-spring/src/main/docs/spring.adoc @@ -379,3 +379,29 @@ Integration] for further injections. To avoid a hung route when testing using Spring Transactions see the note about Spring Integration Testing under Transactional Client. +== Cron Component Support + +Camel-spring can be used as implementation of the Camel Cron component. + + +Maven users will need to add the following additional dependency to their `pom.xml`: + +[source,xml] +------------------------------------------------------------ +<dependency> + <groupId>org.apache.camel</groupId> + <artifactId>camel-cron</artifactId> + <version>x.x.x</version> + <!-- use the same version as your Camel core version --> +</dependency> +------------------------------------------------------------ + +Users can then use the cron component inside routes of their Spring or Spring-boot application: + +[source,xml] +---- +<route> + <from uri="cron:tab?schedule=0/1+*+*+*+*+?"/> + <to uri="log:info"/> +</route> +---- diff --git a/components/camel-spring/src/main/java/org/apache/camel/component/cron/CamelSpringCronService.java b/components/camel-spring/src/main/java/org/apache/camel/component/cron/CamelSpringCronService.java new file mode 100644 index 0000000..723fa1e --- /dev/null +++ b/components/camel-spring/src/main/java/org/apache/camel/component/cron/CamelSpringCronService.java @@ -0,0 +1,62 @@ +/* + * 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.cron; + +import java.util.HashMap; +import java.util.Map; + +import org.apache.camel.CamelContext; +import org.apache.camel.CamelContextAware; +import org.apache.camel.Endpoint; +import org.apache.camel.component.cron.api.CamelCronConfiguration; +import org.apache.camel.component.cron.api.CamelCronService; + +/** + * Allows camel-spring to be used as implementation for camel-cron endpoints. + */ +public class CamelSpringCronService implements CamelCronService, CamelContextAware { + + private CamelContext context; + + @Override + public Endpoint createEndpoint(CamelCronConfiguration configuration) throws Exception { + CronComponent cronComponent = context.getComponent("cron", CronComponent.class); + String uri = "cron:" + configuration.getName(); + SpringCronEndpoint cronEndpoint = new SpringCronEndpoint(uri, cronComponent); + Map<String, Object> options = new HashMap<>(); + options.put("scheduler", "spring"); + options.put("scheduler.cron", configuration.getSchedule()); + cronEndpoint.configureProperties(options); + return cronEndpoint; + } + + @Override + public String getId() { + return "spring"; + } + + @Override + public void setCamelContext(CamelContext camelContext) { + this.context = camelContext; + } + + @Override + public CamelContext getCamelContext() { + return this.context; + } + +} diff --git a/components/camel-spring/src/main/java/org/apache/camel/component/cron/SpringCronConsumer.java b/components/camel-spring/src/main/java/org/apache/camel/component/cron/SpringCronConsumer.java new file mode 100644 index 0000000..217d9e7 --- /dev/null +++ b/components/camel-spring/src/main/java/org/apache/camel/component/cron/SpringCronConsumer.java @@ -0,0 +1,36 @@ +/* + * 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.cron; + +import org.apache.camel.Endpoint; +import org.apache.camel.Exchange; +import org.apache.camel.Processor; +import org.apache.camel.support.ScheduledPollConsumer; + +public class SpringCronConsumer extends ScheduledPollConsumer { + + public SpringCronConsumer(Endpoint endpoint, Processor processor) { + super(endpoint, processor); + } + + @Override + protected int poll() throws Exception { + Exchange exchange = getEndpoint().createExchange(); + getProcessor().process(exchange); + return 1; + } +} diff --git a/components/camel-spring/src/main/java/org/apache/camel/component/cron/SpringCronEndpoint.java b/components/camel-spring/src/main/java/org/apache/camel/component/cron/SpringCronEndpoint.java new file mode 100644 index 0000000..5a92687 --- /dev/null +++ b/components/camel-spring/src/main/java/org/apache/camel/component/cron/SpringCronEndpoint.java @@ -0,0 +1,42 @@ +/* + * 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.cron; + +import org.apache.camel.Component; +import org.apache.camel.Consumer; +import org.apache.camel.Processor; +import org.apache.camel.Producer; +import org.apache.camel.support.ScheduledPollEndpoint; + +public class SpringCronEndpoint extends ScheduledPollEndpoint { + + public SpringCronEndpoint(String endpointUri, Component component) { + super(endpointUri, component); + } + + @Override + public Producer createProducer() throws Exception { + throw new UnsupportedOperationException(); + } + + @Override + public Consumer createConsumer(Processor processor) throws Exception { + SpringCronConsumer consumer = new SpringCronConsumer(this, processor); + configureConsumer(consumer); + return consumer; + } +} diff --git a/components/camel-spring/src/main/resources/META-INF/services/org.apache.camel.component.cron.api.CamelCronService b/components/camel-spring/src/main/resources/META-INF/services/org.apache.camel.component.cron.api.CamelCronService new file mode 100644 index 0000000..b65cde4 --- /dev/null +++ b/components/camel-spring/src/main/resources/META-INF/services/org.apache.camel.component.cron.api.CamelCronService @@ -0,0 +1,18 @@ +# +# 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. +# + +org.apache.camel.component.cron.CamelSpringCronService diff --git a/components/camel-spring/src/test/java/org/apache/camel/component/cron/SpringCronRouteTest.java b/components/camel-spring/src/test/java/org/apache/camel/component/cron/SpringCronRouteTest.java new file mode 100644 index 0000000..2452fcd --- /dev/null +++ b/components/camel-spring/src/test/java/org/apache/camel/component/cron/SpringCronRouteTest.java @@ -0,0 +1,41 @@ +/* + * 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.cron; + +import org.apache.camel.component.mock.MockEndpoint; +import org.apache.camel.spring.SpringTestSupport; +import org.junit.Test; +import org.springframework.context.support.AbstractXmlApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; + +public class SpringCronRouteTest extends SpringTestSupport { + protected MockEndpoint resultEndpoint; + + @Test + public void testCronSpringRoute() throws Exception { + resultEndpoint = getMockEndpoint("mock:result"); + resultEndpoint.expectedMinimumMessageCount(1); + + // lets test the receive worked + resultEndpoint.assertIsSatisfied(); + } + + @Override + protected AbstractXmlApplicationContext createApplicationContext() { + return new ClassPathXmlApplicationContext("org/apache/camel/component/cron/cron.xml"); + } +} diff --git a/components/camel-spring/src/test/resources/org/apache/camel/component/cron/cron.xml b/components/camel-spring/src/test/resources/org/apache/camel/component/cron/cron.xml new file mode 100644 index 0000000..37fd5f2 --- /dev/null +++ b/components/camel-spring/src/test/resources/org/apache/camel/component/cron/cron.xml @@ -0,0 +1,36 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + + 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. + +--> +<beans xmlns="http://www.springframework.org/schema/beans" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation=" + http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd + http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd + "> + + <camelContext xmlns="http://camel.apache.org/schema/spring"> + + <route> + <from uri="cron:tab?schedule=0/1+*+*+*+*+?"/> + <to uri="mock:result"/> + </route> + + </camelContext> + +</beans>