Apache Camel - 20 - Using Camel in a Java application(在Java应用中使用Camel)

版权声明:本文为博主原创文章,可以转载,但请注明出处。 https://blog.csdn.net/Simba_cheng/article/details/81318721

在Java应用中使用Camel

Apache Camel相关代码已经上传GitHub,需要的自取:GitHub - Apache Camel 完整Demo
如果觉得还行,麻烦点个Star

这个栗子是书中提供的,我并没有实际运行过,不过你可以根据《Apache Camel 使用教程》专栏中组件章节的代码,写一个小Demo,体验一下,因为他们都是相通的。

这里假定你会使用Eclipse或IDEA 编译器,以及Maven。

如果不会使用,抱歉,这里也不会讲,环境搭建没什么好说的,就像你平时往pom.xml中添加一个jar配置一样简单。

只需要一个核心jar就可以了:camel-core

版本随便你,不要太低就行。

我这里使用的是:

<!-- https://mvnrepository.com/artifact/org.apache.camel/camel-core -->
<dependency>
    <groupId>org.apache.camel</groupId>
    <artifactId>camel-core</artifactId>
    <version>2.22.0</version>
</dependency>

我这里直接用书中的例子,如果你想尝试一下其他的组件,移步Apache Camel 使用教程

1. 首先使用Camel Java DSL定义路由,自定义路由需要继承org.apache.camel.builder.RouteBuilder抽象类

public class LogMessageOnTimerEventRouteBuilder extends RouteBuilder {
	@Override
	public void configure() throws Exception {
		from("timer:logMessageTimer?period=1s").log("Event triggered by ${property.CamelTimerName}"+ " at ${header.CamelTimerFiredTime}");
	}
};

2. 接着,在main()方法中创建CamelContext。

CamelContext context = new DefaultCamelContext();

3. 然后,将上面创建的路由添加到CamelContext中。

context.addRoutes(new LogMessageOnTimerEventRouteBuilder());

4. 接下来,启动CamelContext(Camel上下文),CamelContext会加载上面添加的路由定义,并在后台处理
注意:CamelContext.start()方法是非阻塞的。它会启动内部线程上的关联组件,并返回给调用者。

context.start();

5. 当不需要Camel继续运行的时候,运行stop方法:

context.stop();

Camel 是怎么工作的?

The CamelContextinterfaceis the heart of the Camel framework. It isresponsible for processing messages along routes.
CamelContext接口是Camel框架的核心。 它负责处理路由中的消息。

The from(...)statement at the start of a route defines an endpoint, or a technology-specific location, that the Camel routing engine uses to fetch messages. Endpoints are defined using URIs, such as in the preceding example, timer:logMessageTimer. The first part of the URI specifies the component that is being used to consume the message, and the remaining is a set of instructions for that specific component. See the Using Camel componentsrecipe in  this chapter for more details.
路由开头的from(...)语句定义了Camel路由引擎用于获取消息的端点或技术特定位置。 端点是使用URI定义的,例如在前面的示例中,timer:logMessageTimer。 URI的第一部分指定用于使用消息的组件,其余部分是该特定组件的一组指令。有关详细信息,请参阅本章中的使用Camel componentsrecipe。

The Camel routing engine consumes exchanges from these endpoints and processes them sequentially through each of the steps defined in the route. The engine is responsible  for threading, transactions, error handling, copying messages where required, and many other details。
Camel路由引擎消耗来自这些端点的交换,并按顺序通过路由中定义的每个步骤处理它们。 引擎负责线程,事务,错误处理,在需要时复制消息以及许多其他细节

The Camel context is a long-running object; it is intended to live for as long as the application does, and therefore its initialization and shutdown is usually tied to the lifecycle of the application. Typical deployments of Camel define the context within:
Camel上下文是一个长期运行的对象; 它只要应用程序就可以存活,因此它的初始化和关闭通常与应用程序的生命周期有关。 Camel的典型部署定义了以下内容

   • The main() method of a standalone command-line application; here it waits indefinitely until the user terminates the process。
    独立命令行应用程序的main()方法; 在这里,它会无限期地等待,直到用户终止进程。

   • As an instance variable within a javax.servlet.ServletContextListenerin a web app, starting up and shutting down along with the application。
    作为Web应用程序中javax.servlet.ServletContextListener中的实例变量,与应用程序一起启动和关闭。

   • An object tied to an OSGi bundle's lifecycle。
    绑定到OSGi包的生命周期的对象。

   • An object within a Spring or OSGi Blueprint context that is itself tied to the application's lifecycle。
    Spring或OSGi Blueprint上下文中的对象本身与应用程序的生命周期相关联。

Routes, which are definitions of the steps that messages should be processed through, are typically added to the newly created context, though they can be added, removed, and modified at runtime. Route definitions can only be added to a context before the context is started, though they can be stopped and restarted while the context is running.
路由是处理消息步骤的定义,通常会添加到新创建的上下文中,尽管可以在运行时添加,删除和修改它们。 路径定义只能在上下文启动之前添加到上下文中,尽管可以在上下文运行时停止和重新启动它们

Extending the RouteBuilderabstract class gives access to Camel's Java route definition DSL, or simply the Java DSL. What this means in practice is that within themandatory configure() method, after typing the first from(...)statement that defines the start of a route, you get context-specific code completion of whichever integration patterns you might be using.
扩展RouteBuilderabstract类可以访问Camel的Java路由定义DSL,或简单地访问Java DSL。 这在实践中意味着在强制的configure()方法中,在键入定义路径起始的第一个from(...)语句之后,您可以获得可能使用的任何集成模式的特定于上下文的代码

A RouteBuilderimplementation may implement one or many routes. That is, within the configure()method, you can specify multiple from(...)statement that Camel will translate into multiple runtime route instances, one per from(...) statement。
RouteBuilderimplementation可以实现一个或多个路由。 也就是说,在configure()方法中,您可以指定Camel将转换为多个运行时路由实例的多个from(...)语句,每个来自(...)语句一个

参考来源:《Apache Camel Developer's Cookbook》

电子书我上传了,有需要的自行下载。《Apache Camel Developer's Cookbook》下载地址

猜你喜欢

转载自blog.csdn.net/Simba_cheng/article/details/81318721