在这个 Camel 教程中,我们将学习如何使用它的 Maven 原型设置一个基本的 Camel 项目,该原型可以生成基于 Java DSL 和 Spring XML 的项目。
有几种不同的方法来配置组件和端点。
context.addRoutes(new RouteBuilder() {
public void configure() {
from("test-jms:queue:test.queue").to("file://test");
}
});
使用基于 Spring 的 Camel 项目的优势在于,您可以以声明方式构建复杂的 Camel 路由,即只需填写名为 camel-context.xml 的 Camel Spring 配置文件
我们将在这里展示如何使用 Java DSL 或 Spring XML 和 Maven 构建示例项目。
您可以从命令行创建您的 Camel 项目,如下所示:
mvn archetype:generate \ -DarchetypeGroupId=org.apache.camel.archetypes \ -DarchetypeArtifactId=camel-archetype-java \ -DarchetypeVersion=3.0.0 \ -DgroupId=com.mastertheboss.camel \ -DartifactId=camel-helloworld \ -Dversion=1.0-SNAPSHOT
将创建项目 camel-helloworld。现在将其导入您最喜欢的 IDE。在我们的例子中,我们将其导入 IntelliJ Idea Community 版本。
我们将只编辑 MainApp 类以删除不推荐使用的代码,并改用 Camel Main 类。它应该是这样的:
package com.mastertheboss.camel;
import org.apache.camel.main.Main;
public class MainApp {
public static void main(String...args) throws Exception {
// use Camels Main class
Main main = new Main();
main.addRouteBuilder(MyRouteBuilder.class);
main.run(args);
}
}
为您创建的 Class MyRouteBuilder 是一个用于路由消息的 Java DSL 类。
package com.mastertheboss.camel;
import org.apache.camel.builder.RouteBuilder;
public class MyRouteBuilder extends RouteBuilder {
public void configure() {
from("file:src/data?noop=true")
.choice()
.when(xpath("/person/city = 'London'"))
.log("UK message")
.to("file:target/messages/uk")
.otherwise()
.log("Other message")
.to("file:target/messages/others");
}
}
正如您所看到的,这个类所做的只是处理输入文件(将它们留在原处,如“noop”标志所配置的那样),然后使用 XPath 表达式执行基于内容的路由。
XML 文件是从项目中包含的文件夹“data”中获取的。
最后,在 log4j2.properties 文件中,您可以为 Logger 设置自定义定义:
appender.out.type = Console appender.out.name = out appender.out.layout.type = PatternLayout appender.out.layout.pattern = [%30.30t] %-30.30c{1} %-5p %m%n rootLogger.level = INFO rootLogger.appenderRef.out.ref = out
项目文件 (pom.xml) 中包含以下依赖项:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.mastertheboss.camel</groupId> <artifactId>camel-helloworld</artifactId> <packaging>jar</packaging> <version>1.0-SNAPSHOT</version> <name>A Camel Route</name> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> </properties> <dependencyManagement> <dependencies> <!-- Camel BOM --> <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-parent</artifactId> <version>3.0.0</version> <scope>import</scope> <type>pom</type> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-core</artifactId> </dependency> <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-main</artifactId> </dependency> <!-- logging --> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-api</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-slf4j-impl</artifactId> <scope>runtime</scope> </dependency> <!-- testing --> <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <defaultGoal>install</defaultGoal> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.1</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <version>3.1.0</version> <configuration> <encoding>UTF-8</encoding> </configuration> </plugin> <!-- Allows the example to be run via 'mvn compile exec:java' --> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>exec-maven-plugin</artifactId> <version>1.6.0</version> <configuration> <mainClass>com.mastertheboss.camel.MainApp</mainClass> <includePluginDependencies>false</includePluginDependencies> </configuration> </plugin> </plugins> </build> </project>
运行 Main 类并从日志中检查文件是否已路由:
[1) thread #2 - file://src/data] route1 INFO UK message [1) thread #2 - file://src/data] route1 INFO Other message
恭喜!你刚刚使用 Maven 运行了你的第一个 Camel 项目
Camel Hello world 项目的源代码:https://github.com/fmarchioni/masteringintegration/tree/master/camel-helloworld
如果要创建相同示例的 Spring 版本,可以使用 maven 原型:
mvn archetype:generate \ -DarchetypeGroupId=org.apache.camel.archetypes \ -DarchetypeArtifactId=camel-archetype-spring \ -DarchetypeVersion=3.0.0 \ -DgroupId=com.mastertheboss.camel \ -DartifactId=camel-helloworld-spring \ -Dversion=1.0-SNAPSHOT
主要区别在于该项目将不包含 Java 资源,而是包含以下 camel-context.xml 文件,其中包含 Camel 上下文和路由的定义:
<?xml version="1.0" encoding="UTF-8"?><!-- Configures the Camel Context--><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd">
<camelContext xmlns="http://camel.apache.org/schema/spring">
<!-- here is a sample which processes the input files (leaving them in place - see the 'noop' flag) then performs content based routing on the message using XPath -->
<route>
<from uri="file:src/data?noop=true"/>
<choice>
<when>
<xpath>/person/city = 'London'</xpath>
<log message="UK message"/>
<to uri="file:target/messages/uk"/>
</when>
<otherwise>
<log message="Other message"/>
<to uri="file:target/messages/others"/>
</otherwise>
</choice>
</route>
</camelContext>
</beans>
这是项目树:
src ├── data │ ├── message1.xml │ └── message2.xml ├── main │ ├── java │ │ └── com │ │ └── mastertheboss │ │ └── camel │ └── resources │ ├── log4j2.properties │ └── META-INF │ └── spring │ └── camel-context.xml └── test ├── java │ └── com │ └── mastertheboss │ └── camel └── resources
您可以使用以下命令运行项目:
mvn install camel:run
Camel Hello World Spring 的源代码:https://github.com/fmarchioni/masteringintegration/tree/master/camel-helloworld-spring
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : http://www.masterspringboot.com/various/camel/using-maven-to-set-up-a-camel-project
内容来源于网络,如有侵权,请联系作者删除!