Skip to content

Commit 27b119a

Browse files
Merge remote-tracking branch 'origin/master' into bael-965-collection-util
2 parents 7a77016 + db2bb25 commit 27b119a

66 files changed

Lines changed: 2300 additions & 0 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎camel-api/README.md‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
Example for the Article on Camel API with SpringBoot
2+
3+
to start up, run:
4+
mvn spring-boot:run
5+
6+
them, do a POST http request to:
7+
http://localhost:8080/camel/api/bean
8+
9+
with the HEADER: Content-Type: application/json,
10+
11+
and a BODY Payload like {"id": 1,"name": "World"}
12+
13+
and we will get a return code of 201 and the response: Hello, World - if the transform() method from Application class is uncommented and the process() method is commented
14+
15+
or return code of 201 and the response: {"id": 10,"name": "Hello, World"} - if the transform() method from Application class is commented and the process() method is uncommented

‎camel-api/pom.xml‎

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>com.example</groupId>
8+
<artifactId>spring-boot-camel</artifactId>
9+
<version>0.0.1-SNAPSHOT</version>
10+
11+
<name>Spring-Boot - Camel API</name>
12+
13+
<properties>
14+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15+
<maven-compiler-plugin.version>3.6.0</maven-compiler-plugin.version>
16+
<maven-surefire-plugin.version>2.19.1</maven-surefire-plugin.version>
17+
<camel.version>2.19.1</camel.version>
18+
<spring-boot-starter.version>1.5.4.RELEASE</spring-boot-starter.version>
19+
</properties>
20+
21+
<dependencies>
22+
<dependency>
23+
<groupId>org.apache.camel</groupId>
24+
<artifactId>camel-servlet-starter</artifactId>
25+
<version>${camel.version}</version>
26+
</dependency>
27+
<dependency>
28+
<groupId>org.apache.camel</groupId>
29+
<artifactId>camel-jackson-starter</artifactId>
30+
<version>${camel.version}</version>
31+
</dependency>
32+
<dependency>
33+
<groupId>org.apache.camel</groupId>
34+
<artifactId>camel-swagger-java-starter</artifactId>
35+
<version>${camel.version}</version>
36+
</dependency>
37+
<dependency>
38+
<groupId>org.apache.camel</groupId>
39+
<artifactId>camel-spring-boot-starter</artifactId>
40+
<version>${camel.version}</version>
41+
</dependency>
42+
<dependency>
43+
<groupId>org.springframework.boot</groupId>
44+
<artifactId>spring-boot-starter-web</artifactId>
45+
<version>${spring-boot-starter.version}</version>
46+
</dependency>
47+
</dependencies>
48+
49+
<build>
50+
<defaultGoal>spring-boot:run</defaultGoal>
51+
52+
<plugins>
53+
<plugin>
54+
<artifactId>maven-compiler-plugin</artifactId>
55+
<version>${maven-compiler-plugin.version}</version>
56+
<configuration>
57+
<source>1.8</source>
58+
<target>1.8</target>
59+
</configuration>
60+
</plugin>
61+
<plugin>
62+
<groupId>org.apache.maven.plugins</groupId>
63+
<artifactId>maven-surefire-plugin</artifactId>
64+
<version>${maven-surefire-plugin.version}</version>
65+
</plugin>
66+
<plugin>
67+
<groupId>org.springframework.boot</groupId>
68+
<artifactId>spring-boot-maven-plugin</artifactId>
69+
<version>${spring-boot-starter.version}</version>
70+
<executions>
71+
<execution>
72+
<goals>
73+
<goal>repackage</goal>
74+
</goals>
75+
</execution>
76+
</executions>
77+
</plugin>
78+
</plugins>
79+
</build>
80+
</project>
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
package com.baeldung.camel;
2+
3+
import javax.ws.rs.core.MediaType;
4+
5+
import org.apache.camel.CamelContext;
6+
import org.apache.camel.Exchange;
7+
import org.apache.camel.Processor;
8+
import org.apache.camel.builder.RouteBuilder;
9+
import org.apache.camel.component.servlet.CamelHttpTransportServlet;
10+
import org.apache.camel.impl.DefaultCamelContext;
11+
import org.apache.camel.model.rest.RestBindingMode;
12+
import org.springframework.beans.factory.annotation.Value;
13+
import org.springframework.boot.SpringApplication;
14+
import org.springframework.boot.autoconfigure.SpringBootApplication;
15+
import org.springframework.boot.web.servlet.ServletRegistrationBean;
16+
import org.springframework.boot.web.support.SpringBootServletInitializer;
17+
import org.springframework.context.annotation.Bean;
18+
import org.springframework.context.annotation.ComponentScan;
19+
import org.springframework.stereotype.Component;
20+
21+
@SpringBootApplication
22+
@ComponentScan(basePackages="com.baeldung.camel")
23+
public class Application extends SpringBootServletInitializer {
24+
25+
@Value("${server.port}")
26+
String serverPort;
27+
28+
@Value("${baeldung.api.path}")
29+
String contextPath;
30+
31+
public static void main(String[] args) {
32+
SpringApplication.run(Application.class, args);
33+
}
34+
35+
@Bean
36+
ServletRegistrationBean servletRegistrationBean() {
37+
ServletRegistrationBean servlet = new ServletRegistrationBean(new CamelHttpTransportServlet(), contextPath+"/*");
38+
servlet.setName("CamelServlet");
39+
return servlet;
40+
}
41+
42+
43+
@Component
44+
class RestApi extends RouteBuilder {
45+
46+
@Override
47+
public void configure() {
48+
49+
CamelContext context = new DefaultCamelContext();
50+
51+
52+
// http://localhost:8080/camel/api-doc
53+
restConfiguration().contextPath(contextPath) //
54+
.port(serverPort)
55+
.enableCORS(true)
56+
.apiContextPath("/api-doc")
57+
.apiProperty("api.title", "Test REST API")
58+
.apiProperty("api.version", "v1")
59+
.apiProperty("cors", "true") // cross-site
60+
.apiContextRouteId("doc-api")
61+
.component("servlet")
62+
.bindingMode(RestBindingMode.json)
63+
.dataFormatProperty("prettyPrint", "true");
64+
/**
65+
The Rest DSL supports automatic binding json/xml contents to/from POJOs using Camels Data Format.
66+
By default the binding mode is off, meaning there is no automatic binding happening for incoming and outgoing messages.
67+
You may want to use binding if you develop POJOs that maps to your REST services request and response types.
68+
This allows you, as a developer, to work with the POJOs in Java code.
69+
*/
70+
71+
rest("/api/").description("Teste REST Service")
72+
.id("api-route")
73+
.post("/bean")
74+
.produces(MediaType.APPLICATION_JSON)
75+
.consumes(MediaType.APPLICATION_JSON)
76+
// .get("/hello/{place}")
77+
.bindingMode(RestBindingMode.auto)
78+
.type(MyBean.class)
79+
.enableCORS(true)
80+
// .outType(OutBean.class)
81+
82+
.to("direct:remoteService");
83+
84+
85+
from("direct:remoteService")
86+
.routeId("direct-route")
87+
.tracing()
88+
.log(">>> ${body.id}")
89+
.log(">>> ${body.name}")
90+
// .transform().simple("blue ${in.body.name}")
91+
.process(new Processor() {
92+
@Override
93+
public void process(Exchange exchange) throws Exception {
94+
MyBean bodyIn = (MyBean) exchange.getIn().getBody();
95+
96+
ExampleServices.example(bodyIn);
97+
98+
exchange.getIn().setBody(bodyIn);
99+
}
100+
})
101+
.setHeader(Exchange.HTTP_RESPONSE_CODE, constant(201));
102+
}
103+
}
104+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.baeldung.camel;
2+
3+
/**
4+
* a Mock class to show how some other layer
5+
* (a persistence layer, for instance)
6+
* could be used insida a Camel
7+
*
8+
*/
9+
public class ExampleServices {
10+
11+
public static void example(MyBean bodyIn) {
12+
bodyIn.setName( "Hello, " + bodyIn.getName() );
13+
bodyIn.setId(bodyIn.getId()*10);
14+
}
15+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.baeldung.camel;
2+
3+
public class MyBean {
4+
private Integer id;
5+
private String name;
6+
public Integer getId() {
7+
return id;
8+
}
9+
public void setId(Integer id) {
10+
this.id = id;
11+
}
12+
public String getName() {
13+
return name;
14+
}
15+
public void setName(String name) {
16+
this.name = name;
17+
}
18+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
logging.config=classpath:logback.xml
2+
3+
# the options from org.apache.camel.spring.boot.CamelConfigurationProperties can be configured here
4+
camel.springboot.name=MyCamel
5+
6+
# lets listen on all ports to ensure we can be invoked from the pod IP
7+
server.address=0.0.0.0
8+
management.address=0.0.0.0
9+
10+
# lets use a different management port in case you need to listen to HTTP requests on 8080
11+
management.port=8081
12+
13+
# disable all management enpoints except health
14+
endpoints.enabled = true
15+
endpoints.health.enabled = true
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
server:
2+
port: 8080
3+
4+
# for example purposes of Camel version 2.18 and below
5+
baeldung:
6+
api:
7+
path: '/camel'
8+
9+
camel:
10+
springboot:
11+
# The Camel context name
12+
name: ServicesRest
13+
14+
# Binding health checks to a different port
15+
management:
16+
port: 8081
17+
18+
# disable all management enpoints except health
19+
endpoints:
20+
enabled: false
21+
health:
22+
enabled: true
23+
24+
# The application configuration properties
25+
quickstart:
26+
generateOrderPeriod: 10s
27+
processOrderPeriod: 30s
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE xml>
3+
<configuration>
4+
5+
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
6+
<!-- encoders are assigned the type
7+
ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
8+
<encoder>
9+
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
10+
</encoder>
11+
</appender>
12+
13+
<root level="info">
14+
<appender-ref ref="STDOUT" />
15+
</root>
16+
17+
</configuration>

‎core-java/pom.xml‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,22 @@
327327
</executions>
328328
</plugin>
329329

330+
<plugin>
331+
<groupId>org.codehaus.mojo</groupId>
332+
<artifactId>exec-maven-plugin</artifactId>
333+
<version>1.6.0</version>
334+
<configuration>
335+
<executable>java</executable>
336+
<mainClass>com.baeldung.outofmemoryerror.OutOfMemoryGCLimitExceed</mainClass>
337+
<arguments>
338+
<argument>-Xmx300m</argument>
339+
<argument>-XX:+UseParallelGC</argument>
340+
<argument>-classpath</argument>
341+
<classpath/>
342+
<argument>com.baeldung.outofmemoryerror.OutOfMemoryGCLimitExceed</argument>
343+
</arguments>
344+
</configuration>
345+
</plugin>
330346
</plugins>
331347

332348
</build>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.baeldung.concurrent.semaphores;
2+
3+
import java.util.concurrent.Semaphore;
4+
5+
class CounterUsingMutex {
6+
7+
private final Semaphore mutex;
8+
private int count;
9+
10+
CounterUsingMutex() {
11+
mutex = new Semaphore(1);
12+
count = 0;
13+
}
14+
15+
void increase() throws InterruptedException {
16+
mutex.acquire();
17+
this.count = this.count + 1;
18+
Thread.sleep(1000);
19+
mutex.release();
20+
21+
}
22+
23+
int getCount() {
24+
return this.count;
25+
}
26+
27+
boolean hasQueuedThreads() {
28+
return mutex.hasQueuedThreads();
29+
}
30+
31+
}

0 commit comments

Comments
 (0)