#176 Test run, app shutdown, minor refactoring

This commit is contained in:
adkm 2017-10-13 14:02:03 +02:00
parent 60f07d1940
commit 0453ba1315
3 changed files with 27 additions and 4 deletions

View File

@ -1,7 +1,10 @@
package com.iluwatar.eip.wiretap; package com.iluwatar.eip.wiretap;
import org.apache.camel.CamelContext;
import org.apache.camel.builder.RouteBuilder;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
/** /**
* In most integration cases there is a need to monitor the messages flowing through the system. It is usually achieved * In most integration cases there is a need to monitor the messages flowing through the system. It is usually achieved
@ -22,7 +25,27 @@ public class App {
* *
* @param args command line args * @param args command line args
*/ */
public static void main(String[] args) { public static void main(String[] args) throws Exception{
SpringApplication.run(App.class, args); // Run Spring Boot application and obtain ApplicationContext
ConfigurableApplicationContext context = SpringApplication.run(App.class, args);
// Get CamelContext from ApplicationContext
CamelContext camelContext = (CamelContext) context.getBean("camelContext");
// Add a new routes that will handle endpoints form WireTapRoute class.
camelContext.addRoutes(new RouteBuilder() {
@Override
public void configure() throws Exception {
from("{{endpoint}}").log("ENDPOINT: ${body}");
from("{{wireTapEndpoint}}").log("WIRETAPPED ENDPOINT: ${body}");
}
});
// Add producer that will send test message to an entry point in WireTapRoute
camelContext.createProducerTemplate().sendBody("{{entry}}", "Test message");
SpringApplication.exit(context);
} }
} }

View File

@ -27,6 +27,6 @@ public class WireTapRoute extends RouteBuilder {
from("{{entry}}").wireTap("direct:wireTap").to("{{endpoint}}"); from("{{entry}}").wireTap("direct:wireTap").to("{{endpoint}}");
// Wire tap route // Wire tap route
from("direct:wireTap").to("{{wireTapEndpoint}}"); from("direct:wireTap").log("Message: ${body}").to("{{wireTapEndpoint}}");
} }
} }

View File

@ -8,7 +8,7 @@ import org.junit.Test;
public class AppTest { public class AppTest {
@Test @Test
public void testMain() { public void testMain() throws Exception {
String[] args = {}; String[] args = {};
App.main(args); App.main(args);
} }