Camel 错误:端点上没有可用的使用者:direct://myRoute.交换[]

enyaitl3  于 2023-10-18  发布在  Apache
关注(0)|答案(3)|浏览(119)

我开始学习camel,我正试图用java spring Boot 做一个简单的代码。下面是我代码:

package camel;

import org.apache.camel.CamelContext;
import org.apache.camel.Configuration;
import org.apache.camel.Consume;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.builder.RouteBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;

@Configuration
public class CamelConfig extends RouteBuilder {

     @Autowired
        private CamelContext camelContext;

        @Override
        public void configure() throws Exception {
            from("direct:myRoute")
//              from("timer:myTimer?period=5000")
                .setBody().constant("Hello, world!")
                .to("log:output");
            
        }
                

        @Bean
        public ProducerTemplate producerTemplate() {
            return camelContext.createProducerTemplate();
        }

    
}

控制器:

@Autowired
        private ProducerTemplate producerTemplate;

        @GetMapping("/hello")
        public String sayHello() {
            producerTemplate.start();
            producerTemplate.sendBody("direct:myRoute", "Hello from REST API!");
            return "Message sent to Camel route";
        }

pom.xml

<dependency>
        <groupId>org.apache.camel.springboot</groupId>
        <artifactId>camel-servlet-starter</artifactId>
        <version>3.21.0</version>
    </dependency>
    <dependency>
        <groupId>org.apache.camel.springboot</groupId>
        <artifactId>camel-jackson-starter</artifactId>
        <version>3.21.0</version>
    </dependency>
    <dependency>
        <groupId>org.apache.camel.springboot</groupId>
        <artifactId>camel-swagger-java-starter</artifactId>
        <version>3.21.0</version>
    </dependency>
    <dependency>
        <groupId>org.apache.camel.springboot</groupId>
        <artifactId>camel-spring-boot-starter</artifactId>
        <version>3.21.0</version>
    </dependency>

当我尝试发送get请求时,我收到状态为500的失败,并出现以下错误:终结点上没有可用的使用者:direct://myRoute.#21453;的交流,有人知道为什么吗?

u7up0aaq

u7up0aaq1#

问题可能是您混合了Camel和Spring注解。尝试使用来自spring import org.springframework.context.annotation.Configuration@Configuration,而不是import org.apache.camel.Configuration
一般来说,我建议将Spring配置与Camel路由配置分开。如果你使用的是springboot和camel-spring-boot-starter,那么你可以跳过

@Bean
        public ProducerTemplate producerTemplate() {
            return camelContext.createProducerTemplate();
        }

Spring会为你做的。让你的类像这样:

@Component  // from org.springframework.stereotype.Component;
public class CamelConfig extends RouteBuilder {

           @Override
        public void configure() throws Exception {
            from("direct:myRoute")
                .setBody().constant("Hello, world!")
                .to("log:output");
            }
      
}

还要仔细查看您正在使用的软件包版本

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.14</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>camel</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>camel</name>
    <properties>
        <java.version>11</java.version>
        <camel.version>3.21.0</camel.version>
        <spring.boot.version>2.7.14</spring.boot.version>
    </properties>
    <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.springboot</groupId>
                <artifactId>camel-spring-boot-bom</artifactId>
                <version>${camel.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.apache.camel.springboot</groupId>
            <artifactId>camel-servlet-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.camel.springboot</groupId>
            <artifactId>camel-spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

对于camel 3(3.20)-使用springboot 2(2.7.14)。如果你想换到camel 4.0.0,可以切换到springboot 3和Java 17

6l7fqoea

6l7fqoea2#

我又补充了一句:
@Configuration @ComponentScan("camel")
控制器,现在它的工作!tnx!

a9wyjsp7

a9wyjsp73#

我把它改成:

package camel;

import org.apache.camel.builder.RouteBuilder;
import org.springframework.stereotype.Component;

    @Component 
    public class CamelConfig extends RouteBuilder {
    
               @Override
            public void configure() throws Exception {
                from("direct:myRoute")
                    .setBody().constant("Hello, world!")
                    .to("log:output");
                }
 but still the same error :(

相关问题