Skip to content

Commit 34d6060

Browse files
authored
refactor: move MyRouteBuilder to standalone class in java-lambda example (#158)
* refactor: move MyRouteBuilder to standalone class in java-lambda example Fixes IllegalAccessException during 'mvn camel:run' by ensuring the RouteBuilder is public and accessible for reflection. * docs: add license header and fix indentation --------- Co-authored-by: jomin mathew <>
1 parent 6b26519 commit 34d6060

3 files changed

Lines changed: 66 additions & 48 deletions

File tree

java-lambda/src/main/java/org/apache/camel/example/java8/MyApplication.java

Lines changed: 0 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,9 @@
1616
*/
1717
package org.apache.camel.example.java8;
1818

19-
import java.util.Date;
20-
import java.util.Objects;
21-
22-
import org.apache.camel.Exchange;
23-
import org.apache.camel.Message;
24-
import org.apache.camel.builder.RouteBuilder;
2519
import org.apache.camel.main.Main;
26-
import org.slf4j.Logger;
27-
import org.slf4j.LoggerFactory;
2820

2921
public final class MyApplication {
30-
private static final Logger LOGGER = LoggerFactory.getLogger(MyApplication.class);
3122

3223
private MyApplication() {
3324
}
@@ -36,42 +27,4 @@ public static void main(String[] args) throws Exception {
3627
Main main = new Main(MyApplication.class);
3728
main.run(args);
3829
}
39-
40-
static class MyRouteBuilder extends RouteBuilder {
41-
42-
@Override
43-
public void configure() throws Exception {
44-
from("timer:simple?includeMetadata=true&period=503")
45-
.id("simple-route")
46-
.transform()
47-
.exchange(this::dateToTime)
48-
.process()
49-
.message(this::log)
50-
.process()
51-
.body(this::log)
52-
.choice()
53-
.when()
54-
.body(Integer.class, b -> (b & 1) == 0)
55-
.log("Received even number")
56-
.when()
57-
.body(Integer.class, b -> (b & 1) != 0)
58-
.log("Received odd number")
59-
.when()
60-
.body(Objects::isNull)
61-
.log("Received null body")
62-
.end();
63-
}
64-
65-
private Long dateToTime(Exchange e) {
66-
return e.getProperty(Exchange.TIMER_FIRED_TIME, Date.class).getTime();
67-
}
68-
69-
private void log(Object b) {
70-
LOGGER.info("body is: {}", b);
71-
}
72-
73-
private void log(Message m) {
74-
LOGGER.info("message is: {}", m);
75-
}
76-
}
7730
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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 org.apache.camel.example.java8;
18+
19+
import org.apache.camel.Exchange;
20+
import org.apache.camel.Message;
21+
import org.apache.camel.builder.RouteBuilder;
22+
import org.slf4j.Logger;
23+
import org.slf4j.LoggerFactory;
24+
25+
import java.util.Date;
26+
import java.util.Objects;
27+
28+
public class MyRouteBuilder extends RouteBuilder {
29+
private static final Logger LOGGER = LoggerFactory.getLogger(MyApplication.class);
30+
31+
@Override
32+
public void configure() throws Exception {
33+
from("timer:simple?includeMetadata=true&period=503")
34+
.id("simple-route")
35+
.transform()
36+
.exchange(this::dateToTime)
37+
.process()
38+
.message(this::log)
39+
.process()
40+
.body(this::log)
41+
.choice()
42+
.when()
43+
.body(Integer.class, b -> (b & 1) == 0)
44+
.log("Received even number")
45+
.when()
46+
.body(Integer.class, b -> (b & 1) != 0)
47+
.log("Received odd number")
48+
.when()
49+
.body(Objects::isNull)
50+
.log("Received null body")
51+
.end();
52+
}
53+
54+
private Long dateToTime(Exchange e) {
55+
return e.getProperty(Exchange.TIMER_FIRED_TIME, Date.class).getTime();
56+
}
57+
58+
private void log(Object b) {
59+
LOGGER.info("body is: {}", b);
60+
}
61+
62+
private void log(Message m) {
63+
LOGGER.info("message is: {}", m);
64+
}
65+
}

java-lambda/src/test/java/org/apache/camel/example/java8/Java8Test.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,6 @@ void should_be_evaluated() {
4141

4242
@Override
4343
protected void configure(MainConfigurationProperties configuration) {
44-
configuration.addRoutesBuilder(new MyApplication.MyRouteBuilder());
44+
configuration.addRoutesBuilder(new MyRouteBuilder());
4545
}
4646
}

0 commit comments

Comments
 (0)