骆驼不是春天开始的

问题描述 投票:0回答:2

我可以使用maven中的camel-maven-plugin运行我的Camel应用程序(mvn camel:run)。读取camel-context.xml文件,我的路由正确启动。

当我尝试在春天执行这些骆驼路线时,我的问题就出现了。当spring开始时,我没有看到Camel的任何日志,就像我直接运行camel插件时所做的那样。我也没有任何证据表明任何与骆驼有关的事情已经开始。成功运行应用程序缺少什么配置?我目前正在尝试通过嵌入式tomcat实例运行它(请参阅下面的mvn配置文件)。我想,为了让春天找到骆驼的背景,我需要做一些独特的事情。

感谢您的帮助!

pom.hml

<?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>

spring maven spring-mvc tomcat apache-camel
2个回答
1
投票

TL; DR:

尝试将camel-spring-boot-starter依赖项添加到您的POM文件中,使用@Component注释标记您的路由并添加一个@SpringBootApplication类以启动与Camel绑定的Spring Context。


阅读你的文件我猜你正在使用Spring 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>

这是Spring Boot和Camel的BOM(材料清单)。这样就可以很好地解决所有必需的依赖关系,并且无需一直描述驼峰组件的版本。

camel-context.xml文件的要求?如果不是你,可以在你的RouteBuilder类上定义所有内容,并在你的类路径上放置一个@SpringBootApplication注释类。

来自docs

Spring Boot组件为Apache Camel提供自动配置。我们认为Camel上下文的自动配置会自动检测Spring上下文中可用的Camel路由,并将关键的Camel实用程序(如生产者模板,使用者模板和类型转换器)注册为bean。

camel-spring-boot jar附带了spring.factories文件,因此只要将该依赖项添加到类路径中,Spring 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中有更多信息和示例。

干杯!


1
投票

根据要求,下面是我们在Camel项目中使用的Camel-Spring设置的简化版本。使用Spring Boot(它提供的好东西太好了,不容忽视,IMO)我们也使用web启动器。既然你正在使用WAR包装,这对你来说应该不是问题。

pom.hml:

<?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 over XML,但如果您仍然喜欢XML,则可以在TestApp上使用@ImportResource批注导入配置。您还可以将XML配置的类自动装配到路径定义中。

如果您对此设置有任何疑问,请与我们联系。

© www.soinside.com 2019 - 2024. All rights reserved.