maven Camel Spring不出发

emeijp43  于 2023-10-17  发布在  Maven
关注(0)|答案(3)|浏览(160)

我可以使用maven中的camel-maven-plugin(mvn camel:run)运行我的Camel应用程序。读取camel-context.xml文件,路由正确启动。
当我在Spring尝试执行这些 Camel 路线时,我的问题就出现了。当Spring开始时,我没有看到任何来自Camel的日志,就像我直接运行camel插件时一样。我也没有任何证据表明任何与 Camel 有关的事情已经开始。要成功运行应用程序,我的配置缺少什么?我目前正在尝试通过一个嵌入式tomcat示例来运行它(参见下面的mvn配置文件)。我会想象我需要做一些独特的事情,才能让Spring找到 Camel 的背景。
感谢任何和所有的帮助!
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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
    <groupId>---</groupId>
    <artifactId>---</artifactId>
    <version>1.0.6-SNAPSHOT</version>
</parent>

<artifactId>---</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>${packaging.type}</packaging>

<properties>
    <jacoco.minimum.code.coverage>0.8</jacoco.minimum.code.coverage>
    <packaging.type>war</packaging.type>
    <failOnMissingWebXml>false</failOnMissingWebXml>
    <org.apache.camel.version>2.16.0</org.apache.camel.version>
</properties>

<dependencies>
    <dependency>
        <groupId>com.fasterxml.jackson.dataformat</groupId>
        <artifactId>jackson-dataformat-csv</artifactId>
    </dependency>

    <dependency>
        <groupId>org.apache.camel</groupId>
        <artifactId>camel-core</artifactId>
        <version>2.19.2</version>
    </dependency>
    <dependency>
        <groupId>org.apache.camel</groupId>
        <artifactId>camel-aws</artifactId>
        <version>2.19.2</version>
    </dependency>
    <dependency>
        <groupId>org.apache.camel</groupId>
        <artifactId>camel-spring</artifactId>
        <version>2.19.2</version>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-maven-plugin</artifactId>
            <version>2.19.2</version>
        </plugin>
    </plugins>
</build>

<profiles>
    <!-- Default build profile for generating war -->
    <profile>
        <id>war</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <properties>
            <packaging.type>war</packaging.type>
            <log.dir>${catalina.base}/logs</log.dir>
            <!-- updates bootstrap.properties -->
            <config.override.path>file:${catalina.base}/conf</config.override.path>
        </properties>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-tomcat</artifactId>
                <scope>provided</scope>
            </dependency>
        </dependencies>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-assembly-plugin</artifactId>
                    <version>2.6</version>
                    <configuration>
                        <descriptor>/src/main/resources/deployablecontent.xml</descriptor>
                        <tarLongFileMode>posix</tarLongFileMode>
                    </configuration>
                    <executions>
                        <execution>
                            <phase>package</phase>
                            <goals>
                                <goal>single</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>

            </plugins>
        </build>
    </profile>

    <!-- Build profile for stand-alone java application with embedded Tomcat 
        Container -->
    <profile>
        <id>embedded</id>
        <activation>
            <activeByDefault>false</activeByDefault>
        </activation>
        <properties>
            <packaging.type>jar</packaging.type>
            <log.dir>logs</log.dir>
            <!-- updates bootstrap.properties -->
            <config.override.path>./conf</config.override.path>
        </properties>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.camel</groupId>
                    <artifactId>camel-maven-plugin</artifactId>
                    <version>2.19.2</version>
                </plugin>
            </plugins>
        </build>
    </profile>
</profiles>

我的Routebuilder类:

public class PriorityCodeSourcesUpdaterRouteBuilder extends RouteBuilder {

private Endpoint incomingEndpoint;
private Endpoint outgoingEndpoint;

@Override
public void configure() throws Exception {
    from(incomingEndpoint)
        .process((exchange) -> {
            System.out.println("new file received");
        })
        .to(outgoingEndpoint);
}

/**
 * Set the incoming endpoint from the spring config file.
 * @param incomingEndpoint incoming endpoint
 */
public void setIncomingEndpoint(final Endpoint incomingEndpoint) {
    this.incomingEndpoint = incomingEndpoint;
}

/**
 * Set the outgoing endpoint from the spring config file.
 * @param outgoingEndpoint outgoing endpoint
 */
public void setOutgoingEndpoint(final Endpoint outgoingEndpoint) {
    this.outgoingEndpoint = outgoingEndpoint;
}
}

我的camel-context.xml位于resources/META-INF/spring/中:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:util="http://www.springframework.org/schema/util"
   xmlns:aop="http://www.springframework.org/schema/aop"
   xmlns:camel="http://camel.apache.org/schema/spring" xmlns:context="http://www.springframework.org/schema/context"
   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
                    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
                    http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd
                    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
                    ">

<camel:camelContext id="camel">

    <camel:routeBuilder ref="PriorityCodeSourcesUpdaterRouteBuilder"/>

    <camel:endpoint id="incomingEndpoint" uri="">
        <camel:property key="accessKey" value=""/>
        <camel:property key="secretKey" value="RAW()"/>
        <camel:property key="region" value=""/>
        <camel:property key="deleteAfterRead" value="false"/>
    </camel:endpoint>
    <camel:endpoint id="outgoingEndpoint" uri="file://#{systemProperties['java.io.tmpdir']}">
        <camel:property key="fileName" value="deadBeefName"/>
        <camel:property key="readLock" value="markerFile "/>
        <!-- We need a customer idempotentKey because all files sent to this endpoint have the same fileName. -->
        <!-- This will prevent camel from thinking that it has already consumed the file. -->
        <!--<camel:property key="idempotentKey" value="3"/>-->
    </camel:endpoint>

</camel:camelContext>

<bean id="PriorityCodeSourcesUpdaterRouteBuilder" class=".....PriorityCodeSourcesUpdaterRouteBuilder">
    <property name="incomingEndpoint" ref="incomingEndpoint" />
    <property name="outgoingEndpoint" ref="outgoingEndpoint" />
</bean>
xriantvc

xriantvc1#

TL;DR:

尝试将camel-spring-boot-starter依赖项添加到POM文件中,使用@Component注解标记路由,并添加@SpringBootApplication类以启动与Camel绑定的Spring Context。
通过阅读您的文件,我猜您正在使用Sping Boot ,对吗?
如果是这样的话,在POM中有以下依赖关系是很好的:

<dependencyManagement>
    <dependencies>
        <!-- Spring Boot BOM -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>${spring.boot-version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
        <!-- Camel BOM -->
        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-spring-boot-dependencies</artifactId>
            <version>${camel.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

这是来自Sping Boot 和Camel的BOM(物料清单)。这样,所有需要的依赖项都将得到很好的解决,并且避免了一直描述camel组件版本的需要。
必须要有camel-context.xml文件吗?如果不是你,可以在你的RouteBuilder类上定义一切,并在你的类路径上放置一个@SpringBootApplication注解类。
关于docs
Sping Boot 组件为Apache Camel提供自动配置。我们对Camel上下文的自定义自动配置自动检测Spring上下文中可用的Camel路由,并将关键的Camel实用程序(如生产者模板,消费者模板和类型转换器)注册为bean。
camel-spring- Boot jar随spring.factories文件一起提供,因此只要您将该依赖项添加到类路径中,Sping Boot 就会自动为您自动配置Camel。
我认为您面临的问题是,在您的Spring Context和Camel Context(camel-context.xml文件)之间没有“粘合剂”。将@Component注解添加到RouteBuilder和以下依赖项:

<dependency>
    <groupId>org.apache.camel</groupId>
    <artifactId>camel-spring-boot-starter</artifactId>
</dependency>

然后这些路由将自动启动。要保持主线程阻塞,以便Camel保持运行,请包含spring-boot-starter-web依赖项,或将camel.springboot.main-run-controller=true添加到application.properties或application.yml文件中。
docs中有更多的信息和例子。
干杯!干杯!

bvn4nwqk

bvn4nwqk2#

根据要求,下面是我们在Camel项目中使用的Camel-Spring设置的简化版本。使用Sping Boot (它提供的好处太好了,不能忽视,IMO),我们还使用web启动器。既然你正在使用WAR打包,这对你来说不应该是一个问题。
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/xsd/maven-4.0.0.xsd">
    <artifactId>stackoverflow</artifactId>
    <groupId>sandbox</groupId>
    <version>1.0-SNAPSHOT</version>
    <modelVersion>4.0.0</modelVersion>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>1.5.3.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-spring-boot-starter</artifactId>
            <version>2.19.2</version>
        </dependency>
    </dependencies>
</project>

src/main/java/stackoverflow/CamelRoute.java(路由定义,在启动时自动发现,由于被放置在@SpringBootApplication类的包路径中-下面的TestApp类):

package stackoverflow;

import org.apache.camel.builder.RouteBuilder;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class CamelRoute extends RouteBuilder {
    @Value("${message}")
    private String message;
    // you can also have dependencies @Autowired here

    @Override
    public void configure() {
        from("direct:test").process(exchange -> {
            exchange.getIn().setBody(message);
        });
    }
}

src/main/resources/application.properties(说明如何将配置值传递到路由定义):

message=Test

src/main/resources/log4j.properties(主要是为了让你在日志中看到你的路由开始):

log4j.rootLogger=INFO, stdout

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%-5p %c:%L - %m%n

src/main/java/stackoverflow/TestApp.java:

package stackoverflow;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class TestApp {
    public static void main(String[] args) {
        SpringApplication.run(TestApp.class, args);
    }
}

TestApp类启动应用程序,并一直运行直到停止(它启动一个嵌入式Tomcat服务器)。路由在应用程序启动时被发现并启动。
这个设置更喜欢Java Configuration而不是XML,但是如果你仍然喜欢XML,你可以在TestApp上使用@ImportResource注解导入你的配置。您还可以将XML配置的类自动连接到路由定义中。
如果您对此设置有任何疑问,请告诉我。

syqv5f0l

syqv5f0l3#

请确保Sping Boot 的版本与Apache Camel兼容。Camel将从Apache Camel 4.x支持Sping Boot 3.x。你可以在this线程中阅读更多。

相关问题