简体   繁体   English

Spring Boot Camel 应用程序的问题

[英]Problem with Spring Boot Camel application

I am still very new to using Apache Camel and am trying to adapt an example provided by someone ( https://www.javainuse.com/camel/camel-consume-rest ) to essentially do the same thing the original application does, except by using a combination of Spring Boot and Camel with XML route configuration.我对使用 Apache Camel 仍然很陌生,并且正在尝试改编某人( https://www.javainuse.com/camel/camel-consume-rest )提供的示例,以基本上做与原始应用程序相同的事情,除了通过使用带有 XML 路由配置的 Spring Boot 和 Camel 的组合。

I downloaded the original application code (both the Camel consumer and the HTTP server) and got those to run and execute properly very easily.我下载了原始应用程序代码(Camel 消费者和 HTTP 服务器),并让它们非常轻松地正确运行和执行。

Now, I want to modify the application a little so that Spring Boot is used to do all the behind-the-scenes configuration, and use the XML DSL to configure the Camel routes.现在,我想稍微修改一下应用程序,以便使用 Spring Boot 来完成所有的幕后配置,并使用 XML DSL 来配置 Camel 路由。 The original Camel consumer application does not use Spring at all.最初的 Camel 消费者应用程序根本不使用 Spring。

I made the changes that seemed to make sense, but cannot get the application to run.我进行了看似有意义的更改,但无法运行应用程序。 I get the following exception:我收到以下异常:

Exception in thread "main" java.lang.IllegalAccessError: tried to access method org.springframework.core.io.support.SpringFactoriesLoader.loadFactoryNames(Ljava/lang/Class;Ljava/lang/ClassLoader;)Ljava/util/List; from class org.springframework.boot.SpringApplication
    at org.springframework.boot.SpringApplication.getSpringFactoriesInstances(SpringApplication.java:418)
    at org.springframework.boot.SpringApplication.getSpringFactoriesInstances(SpringApplication.java:412)
    at org.springframework.boot.SpringApplication.<init>(SpringApplication.java:268)
    at org.springframework.boot.SpringApplication.<init>(SpringApplication.java:249)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1213)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1202)
    at org.test.ApplicationMain.main(ApplicationMain.java:12)

I do not understand what it's complaining about.我不明白它在抱怨什么。 I'm including resources below so that people can look at what I'm doing.我在下面包含了资源,以便人们可以查看我在做什么。 I apologize in advance for the amount of code.对于代码量,我提前道歉。

ApplicationMain.java:应用程序主.java:

@SpringBootApplication
@ImportResource("classpath:camel-context.xml")
public class ApplicationMain {
  public static void main(String[] args) {
    SpringApplication.run(ApplicationMain.class, args);
  }
}

Configurer.java:配置器.java:

@Configuration
@ComponentScan("org.test")
public class Configurer {
  @Bean
  public MyProcessor myProcessor() {
    return new MyProcessor();
  }
}

application.yml:应用程序.yml:

---
camel:
  springboot:
    name: CamelConsumer
    main-run-controller: true

camel-context.xml:骆驼-context.xml:

<?xml version="1.0" encoding="UTF-8"?>
<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">

  <import resource="classpath:routes/routes.xml" />

  <camelContext id="camel-context" xmlns="http://camel.apache.org/schema/spring">
    <routeContextRef ref="routes" />
  </camelContext>
</beans>

routes/routes.xml:路线/routes.xml:

Note : I do not know whether I'm using the correct syntax for the elements in the route definition below.注意:我不知道我是否对下面路由定义中的元素使用了正确的语法。 I'm trying to adapt it from the original, which was done in Java DSL.我正在尝试从原始版本中改编它,这是在 Java DSL 中完成的。 I'm also not sure whether or not I need the processor bean definition, since I also have the bean defined in the Configurer above.我也不确定是否需要处理器 bean 定义,因为我还在上面的配置器中定义了 bean。

<?xml version="1.0" encoding="UTF-8"?>
<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">

  <bean id="myProcessor" class="org.test.processor.MyProcessor" />

  <routeContext id="routes" xmlns="http://camel.apache.org/schema/spring">
    <route id="httpTest">
      <from uri="file:C:/inboxREST?noop=true" />
      <setHeader headerName="HTTP_METHOD">
        <simple>GET</simple>
      </setHeader>
      <to uri="http://localhost:8080/employee?id=5" />
      <process ref="myProcessor" />
    </route>
  </routeContext>
</beans>

MyProcessor.java:我的处理器.java:

public class MyProcessor implements Processor {
  public MyProcessor() {
  }
  public void process(Exchange exchange) throws Exception {
    System.out.println(exchange.getIn().getBody(String.class));
  }
}

pom.xml: pom.xml:

Note : You may notice a mix of Spring Boot Camel starters as well as Camel dependencies for Camel components (like HTTP).注意:您可能会注意到 Spring Boot Camel starters 以及 Camel 组件(如 HTTP)的 Camel 依赖项的混合。 It's not clear to me which of these I actually need.我不清楚我真正需要哪些。 Do the Spring Boot starters remove the need to include Camel component dependencies? Spring Boot 启动器是否不需要包含 Camel 组件依赖项?

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

  <groupId>com.javainuse</groupId>
  <artifactId>apache-camel-consume</artifactId>
  <version>0.0.1-SNAPSHOT</version>

  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.6.RELEASE</version>
    <relativePath />
  </parent>

  <properties>
    <java.version>1.8</java.version>

    <apache-camel-springboot.version>3.1.0</apache-camel-springboot.version>
    <apache-camel.version>3.1.0</apache-camel.version>
  </properties>

  <dependencies>

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

    <!-- Camel Starter -->
    <dependency>
      <groupId>org.apache.camel.springboot</groupId>
      <artifactId>camel-spring-boot-starter</artifactId>
      <version>${apache-camel-springboot.version}</version>
    </dependency>

    <dependency>
      <groupId>org.apache.camel.springboot</groupId>
      <artifactId>camel-spring-boot</artifactId>
      <version>${apache-camel-springboot.version}</version>
    </dependency>

    <!-- Camel HTTP -->
    <dependency>
      <groupId>org.apache.camel</groupId>
      <artifactId>camel-http</artifactId>
      <version>${apache-camel.version}</version>
    </dependency>

    <dependency>
      <groupId>org.apache.camel.springboot</groupId>
      <artifactId>camel-http-starter</artifactId>
      <version>${apache-camel-springboot.version}</version>
    </dependency>

    <!-- Camel Jackson -->
    <dependency>
      <groupId>org.apache.camel.springboot</groupId>
      <artifactId>camel-jackson-starter</artifactId>
      <version>${apache-camel-springboot.version}</version>
    </dependency>

    <!-- Spring Boot -->

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter</artifactId>
      <exclusions>
        <exclusion>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-logging</artifactId>
        </exclusion>
      </exclusions>
    </dependency>

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
      <exclusions>
        <exclusion>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-tomcat</artifactId>
        </exclusion>
        <exclusion>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-logging</artifactId>
        </exclusion>
      </exclusions>
    </dependency>

  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.1</version>
      </plugin>

      <plugin>
        <groupId>org.apache.felix</groupId>
        <artifactId>maven-bundle-plugin</artifactId>
        <extensions>true</extensions>
        <version>2.4.0</version>
      </plugin>
    </plugins>
  </build>

</project>

Argh.啊。 Very tricky and hard to find problem.非常棘手且难以发现问题。

It turns out that there were dependencies in my maven repo (.m2/repository) that I needed to clean out.事实证明,我的 Maven 存储库 (.m2/repository) 中存在我需要清除的依赖项。 I think they were brought in by the original project indicated at the start of the post.我认为它们是由帖子开头指出的原始项目带来的。

Then, I needed to edit the eclipse project properties to remove the old (and completely unneeded) jars.然后,我需要编辑 eclipse 项目属性以删除旧的(完全不需要的)jar。 Once this was done and a clean build completed, the application started as it should.完成此操作并完成干净的构建后,应用程序将按原样启动。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM