In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces how Java developers get started with Apache Camel, the content is very detailed, interested friends can refer to, I hope it can be helpful to you.
Apache Camel is a very useful rule engine library that can be used to handle events and information from different sources. You can deliver messages using different protocols such as VM,HTTP,FTP,JMS or even file systems, and keep your operational logic and delivery logic separate, which allows you to focus more on the content of the message.
In this article, I will provide a demonstration of getting started with Apache Camel in the Java language (not Groovy).
First create a pom.xml for the Maven project.
4.0.0 camel-spring-demo camel-spring-demo 1.0-SNAPSHOT jar UTF-8 2.11.1 org.apache.camel camel-core ${camel.version} org.slf4j slf4j-simple 1.7.5
We only use the camel-core.jar package here, which actually provides a lot of utility components that you might use. For logging purposes, I used slf4j-simple as the logging implementation so that we could see the output from the console.
Next we just need to construct a routing class. Routing is like an instruction definition of how to pass a message from one end to the other in Camel. We will create a src/main/java / camelcoredemo/TimerRouteBuilder.java file, send a message to the processor every other second, and simply print it out.
Package camelcoredemo; import org.slf4j.*; import org.apache.camel.*; import org.apache.camel.builder.*; public class TimerRouteBuilder extends RouteBuilder {static Logger LOG = LoggerFactory.getLogger (TimerRouteBuilder.class); public void configure () {from ("timer://timer1?period=1000") .process (new Processor () {public void process (Exchange msg) {LOG.info ("Processing {}", msg);});}}
That's all you need for this example, which is now compiled and run.
Bash > mvn compile bash > mvn exec:java-Dexec.mainClass=org.apache.camel.main.Main-Dexec.args='-r camelcoredemo.TimerRouteBuilder'
Note that we don't write the main entry for the Java class here, we simply pass the class name of RouteBuilder as a parameter to org.apache.camel.main.Main, and it will automatically load the route.
Control CamelContext
When Camel is started, it creates a CamelContext object that has a lot of information about how to run Camel, as well as a definition of the Route we created. Now if you want to gain more control through CamelContext, you need to write your own main class code. Let me give you a simple example.
Package camelcoredemo; import org.slf4j.*; import org.apache.camel.*; import org.apache.camel.impl.*; import org.apache.camel.builder.*; public class TimerMain {static Logger LOG = LoggerFactory.getLogger (TimerMain.class); public static void main (String [] args) throws Exception {new TimerMain (). Run ();} void run () throws Exception {final CamelContext camelContext = new DefaultCamelContext (); camelContext.addRoutes (createRouteBuilder ()); camelContext.setTracing (true) CamelContext.start (); Runtime.getRuntime (). AddShutdownHook (new Thread () {public void run () {try {camelContext.stop ();} catch (Exception e) {throw new RuntimeException (e);}); waitForStop ();} RouteBuilder createRouteBuilder () {return new TimerRouteBuilder ();} void waitForStop () {while (true) {try {Thread.sleep (Long.MAX_VALUE) } catch (InterruptedException e) {break;}
As you can see, we reuse the existing TimerRouteBuilder class in the createRouteBuilder () method. Now our main class has complete control over when to create, start, and stop CamelContext. The context (camelContext) object allows you to have global control over how Camel is configured, not at the Route level. Its JavaDoc link gives all the setter methods, and you can study what it can do.
Note that we also need to provide a small amount of setup code in our main class. First of all, we need to deal with elegant closure, so we added a Java close callback function to call context's stop () method. Second, after context has started, we need to add a thread blocking. If you don't block your main thread after startup, it will simply exit after startup, which is useless. You will run Camel as a service (like a server) until you press the CTRL+C key to terminate the process.
Improve the main class that starts CamelContext
If you don't want to deal too much with the main class setup code as in the example above, you can simply inherit the org.apache.camel.main.Main class provided by camel-core instead. By using this class, you can not only set your context automatically, but also get all the additional command-line features, such as controlling how long the process is running, enabling tracing, loading custom route classes, and so on.
The next example is refactored with the following code:
Package camelcoredemo; import org.slf4j.*; import org.apache.camel.builder.*; import org.apache.camel.main.Main; public class TimerMain2 extends Main {static Logger LOG = LoggerFactory.getLogger (TimerMain2.class); public static void main (String [] args) throws Exception {TimerMain2 main = new TimerMain2 (); main.enableHangupSupport (); main.addRouteBuilder (createRouteBuilder ()); main.run (args);} static RouteBuilder createRouteBuilder () {return new TimerRouteBuilder ();}}
Now that the TimerMain2 class has less code than before, you can try it, it should be the same as before.
Bash > mvn compile bash > mvn exec:java-Dexec.mainClass=camelcoredemo.TimerMain2-Dexec.args='-t'
Notice that when we give the-t option, the Route trace is dumped. Use-h to see all the options available.
Use Camel's registration mechanism to add bean
In the previous TimerRouteBuilder example, we have created an anonymous Processor in the code. Now if you want to put several different Processor together, then using Camel's registration mechanism to add bean will better reduce code clutter. Camel allows you to inject processing into its registry space as bean, and then you just call them as bean components. Here is my refactoring code:
Package camelcoredemo; import org.slf4j.*; import org.apache.camel.*; import org.apache.camel.builder.*; import org.apache.camel.main.Main; public class TimerBeansMain extends Main {static Logger LOG = LoggerFactory.getLogger (TimerBeansMain.class); public static void main (String [] args) throws Exception {TimerBeansMain main = new TimerBeansMain (); main.enableHangupSupport (); main.bind ("processByBean1", new Bean1 ()); main.bind ("processAgainByBean2", new Bean2 ()) Main.addRouteBuilder (createRouteBuilder ()); main.run (args);} static RouteBuilder createRouteBuilder () {return new RouteBuilder () {public void configure () {from ("timer://timer1?period=1000") .to ("bean:processByBean1") .to ("bean:processAgainByBean2");}};} / Processor beans static class Bean1 implements Processor {public void process (Exchange msg) {LOG.info ("First process {}", msg) }} static class Bean2 implements Processor {public void process (Exchange msg) {LOG.info ("Second process {}", msg);}
The Route class is now more concise, and the processing code has been refactored into separate classes. When you need to write complex Route to implement business logic, this approach can help you better organize and test your code. It allows you to build reusable POJO bean like Lego bricks. Camel's registry space can also be used for many other purposes, such as customizing many endpoint components with additional functions, registering some information, or replacing things within the thread pool implementation policy.
The above Route example is made up of the so-called Java DSL, which is highly readable, and you can use the support provided by IDE to see all the methods available for Route.
I hope this article will help you skip the exploratory phase of Camel. In addition to the event components already mentioned, camel provides the following components:
Bean component
Browse component
Dataset component
Direct component
File component
Log component
Mock component
Properties component
Seda component
Test component
Timer component
Stub component
Validator component
Vm component
Xslt component
Have fun!
So much for sharing about how Java developers get started with Apache Camel. I hope the above content can be helpful to you and learn more. If you think the article is good, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.