target/city/London中复制这些文件。同样,在XML文件中,如果person.city==孟买
,那么我需要复制target/city/mumbai的文件。
编码编译和执行文件:
a. mvn clean package
b. mvn camel:run -Dcamel.main.durationMaxMessages=2
我正在尝试编写此逻辑,但有时它不会被编译,有时会出现错误,而我无法前进。
我的pom文件内容:
<?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>org.apache.camel.learn</groupId>
<artifactId>first-camel-integration</artifactId>
<packaging>jar</packaging>
<version>1.0.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>
<log4j2-version>2.13.3</log4j2-version>
</properties>
<dependencyManagement>
<dependencies>
<!-- Camel BOM -->
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-bom</artifactId>
<version>3.18.4</version>
<scope>import</scope>
<type>pom</type>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-core</artifactId>
<version>3.20.0</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-main</artifactId>
<version>3.20.0</version>
</dependency>
<!-- logging -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<scope>runtime</scope>
<version>${log4j2-version}</version>
</dependency>
<!-- testing -->
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-jsonpath</artifactId>
<version>3.20.0</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-jackson</artifactId>
<version>3.20.0</version>
</dependency>
</dependencies>
<build>
<defaultGoal>install</defaultGoal>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.10.1</version>
<configuration>
<release>11</release>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<!-- Allows the example to be run via 'mvn camel:run' -->
<plugin>
<groupId>org.apache.camel</groupId>
<artifactId>camel-maven-plugin</artifactId>
<version>3.20.0</version>
<configuration>
<logClasspath>true</logClasspath>
<mainClass>org.apache.camel.learn.MainApp</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>
package org.apache.camel.learn;
import org.apache.camel.main.Main;
public class MainApp {
/**
* A main() so we can easily run these routing rules in our IDE
*/
public static void main(String... args) throws Exception {
Main main = new Main();
main.configure().addRoutesBuilder(new MyRouteBuilder());
main.run(args);
}
}
Myroutebuilder.java
package org.apache.camel.learn;
import org.apache.camel.builder.RouteBuilder;
public class MyRouteBuilder extends RouteBuilder {
/**
* Let's configure the Camel routing rules using Java code...
*/
public void configure() {
// 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
from("file:src/data?noop=true")
.choice()
.when(simple("${file:ext} == 'xml'"))
.choice()
.when(xpath("/person/city = 'London'"))
.log("UK message")
.to("file:target/messages/uk")
.when(xpath("/person/city = 'Mumbai'"))
.log("India message")
.to("file:target/messages/india")
.otherwise()
.log("Other message")
.to("file:target/messages/others")
.end()
.when(simple("${file:ext} == 'json'"))
.unmarshal().json(JsonLibrary.Jackson) // Convert JSON to Java Object
.choice()
.when().jsonpath("$.person.city == 'London'")
.log("UK message")
.to("file:target/messages/uk")
.when().jsonpath("$.person.city == 'Mumbai'")
.log("India message")
.to("file:target/messages/india")
.otherwise()
.log("Other message")
.to("file:target/messages/others")
.end()
.otherwise()
.log("Unsupported file type")
.to("file:target/messages/others")
.end();
}
}
我得到:
[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] /Users/DEEPAK.PARASNATH/Projects/java/first-camel-integration/first-camel-integration/src/main/java/org/apache/camel/learn/MyRouteBuilder.java:[34,39] cannot find symbol
symbol: variable JsonLibrary
location: class org.apache.camel.learn.MyRouteBuilder
[ERROR] /Users/DEEPAK.PARASNATH/Projects/java/first-camel-integration/first-camel-integration/src/main/java/org/apache/camel/learn/MyRouteBuilder.java:[33,17] cannot find symbol
symbol: method when(org.apache.camel.builder.ValueBuilder)
location: class org.apache.camel.model.ProcessorDefinition<capture#1 of ?>
[INFO] 2 errors
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.999 s
[INFO] Finished at: 2025-02-06T13:57:42+05:30
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.10.1:compile (default-compile) on project first-camel-integration: Compilation failure: Compilation failure:
[ERROR] /Users/DEEPAK.PARASNATH/Projects/java/first-camel-integration/first-camel-integration/src/main/java/org/apache/camel/learn/MyRouteBuilder.java:[34,39] cannot find symbol
[ERROR] symbol: variable JsonLibrary
[ERROR] location: class org.apache.camel.learn.MyRouteBuilder
[ERROR] /Users/DEEPAK.PARASNATH/Projects/java/first-camel-integration/first-camel-integration/src/main/java/org/apache/camel/learn/MyRouteBuilder.java:[33,17] cannot find symbol
[ERROR] symbol: method when(org.apache.camel.builder.ValueBuilder)
[ERROR] location: class org.apache.camel.model.ProcessorDefinition<capture#1 of ?>
[ERROR] -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException
对于第一个错误(找不到符号... jsonlibrary),您缺少导入:
import org.apache.camel.model.dataformat.JsonLibrary;
第二个错误可能是第一个错误的结果,或者您正在一起使用错误的libs。