Skip to content

Commit 398cd4b

Browse files
committedJun 4, 2022
CAMEL-18166: Added endpointdsl example
1 parent 6c3c0ca commit 398cd4b

File tree

7 files changed

+328
-0
lines changed

7 files changed

+328
-0
lines changed
 

‎endpointdsl/pom.xml

+117
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
4+
Licensed to the Apache Software Foundation (ASF) under one or more
5+
contributor license agreements. See the NOTICE file distributed with
6+
this work for additional information regarding copyright ownership.
7+
The ASF licenses this file to You under the Apache License, Version 2.0
8+
(the "License"); you may not use this file except in compliance with
9+
the License. You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
19+
-->
20+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
21+
22+
<modelVersion>4.0.0</modelVersion>
23+
24+
<parent>
25+
<groupId>org.apache.camel.springboot.example</groupId>
26+
<artifactId>examples</artifactId>
27+
<version>3.18.0-SNAPSHOT</version>
28+
</parent>
29+
30+
<artifactId>camel-example-endpointdsl</artifactId>
31+
<name>Camel SB Examples :: Endpoint DSL</name>
32+
<description>Using type-safe Endpoint DSL</description>
33+
34+
<properties>
35+
<category>Beginner</category>
36+
37+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
38+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
39+
</properties>
40+
41+
<dependencyManagement>
42+
<dependencies>
43+
<!-- Spring Boot BOM -->
44+
<dependency>
45+
<groupId>org.springframework.boot</groupId>
46+
<artifactId>spring-boot-dependencies</artifactId>
47+
<version>${spring-boot-version}</version>
48+
<type>pom</type>
49+
<scope>import</scope>
50+
</dependency>
51+
<!-- Camel BOM -->
52+
<dependency>
53+
<groupId>org.apache.camel.springboot</groupId>
54+
<artifactId>camel-spring-boot-bom</artifactId>
55+
<version>${camel-version}</version>
56+
<type>pom</type>
57+
<scope>import</scope>
58+
</dependency>
59+
</dependencies>
60+
</dependencyManagement>
61+
62+
<dependencies>
63+
64+
<!-- Spring Boot -->
65+
<dependency>
66+
<groupId>org.springframework.boot</groupId>
67+
<artifactId>spring-boot-starter-web</artifactId>
68+
</dependency>
69+
<dependency>
70+
<groupId>org.springframework.boot</groupId>
71+
<artifactId>spring-boot-starter-actuator</artifactId>
72+
</dependency>
73+
74+
<!-- Camel -->
75+
<dependency>
76+
<groupId>org.apache.camel.springboot</groupId>
77+
<artifactId>camel-spring-boot-starter</artifactId>
78+
</dependency>
79+
<dependency>
80+
<groupId>org.apache.camel.springboot</groupId>
81+
<artifactId>camel-endpointdsl-starter</artifactId>
82+
</dependency>
83+
<dependency>
84+
<groupId>org.apache.camel.springboot</groupId>
85+
<artifactId>camel-stream-starter</artifactId>
86+
</dependency>
87+
88+
<!-- test -->
89+
<dependency>
90+
<groupId>org.springframework.boot</groupId>
91+
<artifactId>spring-boot-starter-test</artifactId>
92+
<scope>test</scope>
93+
</dependency>
94+
<dependency>
95+
<groupId>org.apache.camel</groupId>
96+
<artifactId>camel-test-spring-junit5</artifactId>
97+
<scope>test</scope>
98+
</dependency>
99+
</dependencies>
100+
101+
<build>
102+
<plugins>
103+
<plugin>
104+
<groupId>org.springframework.boot</groupId>
105+
<artifactId>spring-boot-maven-plugin</artifactId>
106+
<version>${spring-boot-version}</version>
107+
<executions>
108+
<execution>
109+
<goals>
110+
<goal>repackage</goal>
111+
</goals>
112+
</execution>
113+
</executions>
114+
</plugin>
115+
</plugins>
116+
</build>
117+
</project>

‎endpointdsl/readme.adoc

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
== Camel Example Endpoint DSL
2+
3+
This example shows how to work with the type-safe Endpoint DSL to build Camel routes
4+
in Java code where endpoints are configured using a chain of Java method calls,
5+
instead of using the Camel endpoint URI style.
6+
7+
The example generates messages using timer trigger, writes them to standard output.
8+
9+
=== Camel routes
10+
11+
The Camel route is embedded directly in the main class `MyCamelApplication` as a Spring Boot `@Bean` method.
12+
The route starts from a timer, that triggers every 2nd second and calls a Spring Bean `MyBean`
13+
which returns a message, that is routed to a stream endpoint which writes to standard output.
14+
15+
=== How to run
16+
17+
You can run this example using
18+
19+
[source,bash]
20+
----
21+
mvn spring-boot:run
22+
----
23+
24+
=== To get health check
25+
26+
To show a summary of spring boot health check
27+
28+
[source,bash]
29+
----
30+
curl -XGET -s http://localhost:8080/actuator/health
31+
----
32+
33+
And you can see some info details as well
34+
35+
[source,bash]
36+
----
37+
curl -XGET -s http://localhost:8080/actuator/info
38+
----
39+
40+
See the `application.properties` to control what information to present in actuator.
41+
42+
=== Help and contributions
43+
44+
If you hit any problem using Camel or have some feedback, then please
45+
https://camel.apache.org/support.html[let us know].
46+
47+
We also love contributors, so
48+
https://camel.apache.org/contributing.html[get involved] :-)
49+
50+
The Camel riders!
51+
52+
53+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package sample.camel;
18+
19+
import org.springframework.beans.factory.annotation.Value;
20+
import org.springframework.stereotype.Component;
21+
22+
/**
23+
* A bean that returns a message when you call the {@link #saySomething()} method.
24+
* <p/>
25+
* Uses <tt>@Component("myBean")</tt> to register this bean with the name <tt>myBean</tt>
26+
* that we use in the Camel route to lookup this bean.
27+
*/
28+
@Component("myBean")
29+
public class MyBean {
30+
31+
private int counter;
32+
33+
@Value("${greeting}")
34+
private String say;
35+
36+
public String saySomething(String body) {
37+
return String.format("%s I am invoked %d times", say, ++counter);
38+
}
39+
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package sample.camel;
18+
19+
import org.apache.camel.builder.endpoint.LambdaEndpointRouteBuilder;
20+
import org.springframework.boot.SpringApplication;
21+
import org.springframework.boot.autoconfigure.SpringBootApplication;
22+
import org.springframework.context.annotation.Bean;
23+
24+
//CHECKSTYLE:OFF
25+
/**
26+
* A sample Spring Boot application that starts the Camel routes.
27+
*/
28+
@SpringBootApplication
29+
public class MyCamelApplication {
30+
31+
/**
32+
* A main method to start this application.
33+
*/
34+
public static void main(String[] args) {
35+
SpringApplication.run(MyCamelApplication.class, args);
36+
}
37+
38+
// embed route directly using type-safe endpoint-dsl using java lambda style
39+
@Bean
40+
public LambdaEndpointRouteBuilder myRoute() {
41+
return rb -> rb
42+
.from(rb.timer("timer").period(2000))
43+
.to(rb.bean("myBean").method("saySomething"))
44+
.log("${body}");
45+
}
46+
47+
}
48+
//CHECKSTYLE:ON
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
## ---------------------------------------------------------------------------
2+
## Licensed to the Apache Software Foundation (ASF) under one or more
3+
## contributor license agreements. See the NOTICE file distributed with
4+
## this work for additional information regarding copyright ownership.
5+
## The ASF licenses this file to You under the Apache License, Version 2.0
6+
## (the "License"); you may not use this file except in compliance with
7+
## the License. You may obtain a copy of the License at
8+
##
9+
## http://www.apache.org/licenses/LICENSE-2.0
10+
##
11+
## Unless required by applicable law or agreed to in writing, software
12+
## distributed under the License is distributed on an "AS IS" BASIS,
13+
## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
## See the License for the specific language governing permissions and
15+
## limitations under the License.
16+
## ---------------------------------------------------------------------------
17+
18+
# the name of Camel
19+
camel.springboot.name = MyCamel
20+
21+
# what to say
22+
greeting = Hello World
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package sample.camel;
18+
19+
import java.util.concurrent.TimeUnit;
20+
21+
import org.apache.camel.CamelContext;
22+
import org.apache.camel.builder.NotifyBuilder;
23+
import org.apache.camel.test.spring.junit5.CamelSpringBootTest;
24+
import org.junit.jupiter.api.Test;
25+
import org.springframework.beans.factory.annotation.Autowired;
26+
import org.springframework.boot.test.context.SpringBootTest;
27+
28+
29+
import static org.junit.jupiter.api.Assertions.assertTrue;
30+
31+
@CamelSpringBootTest
32+
@SpringBootTest(classes = MyCamelApplication.class)
33+
public class MyCamelApplicationJUnit5Test {
34+
35+
@Autowired
36+
private CamelContext camelContext;
37+
38+
@Test
39+
public void shouldProduceMessages() throws Exception {
40+
// we expect that one or more messages is automatic done by the Camel
41+
// route as it uses a timer to trigger
42+
NotifyBuilder notify = new NotifyBuilder(camelContext).whenDone(1).create();
43+
44+
assertTrue(notify.matches(10, TimeUnit.SECONDS));
45+
}
46+
47+
}

‎pom.xml

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
<module>aws-secrets-manager</module>
4646
<module>aws2-s3</module>
4747
<module>dynamic-router-eip</module>
48+
<module>endpointdsl</module>
4849
<module>fhir</module>
4950
<module>fhir-auth-tx</module>
5051
<module>geocoder</module>

0 commit comments

Comments
 (0)
Please sign in to comment.