簡體   English   中英

Spring Boot Camel 應用程序的問題

[英]Problem with Spring Boot Camel application

我對使用 Apache Camel 仍然很陌生,並且正在嘗試改編某人( https://www.javainuse.com/camel/camel-consume-rest )提供的示例,以基本上做與原始應用程序相同的事情,除了通過使用帶有 XML 路由配置的 Spring Boot 和 Camel 的組合。

我下載了原始應用程序代碼(Camel 消費者和 HTTP 服務器),並讓它們非常輕松地正確運行和執行。

現在,我想稍微修改一下應用程序,以便使用 Spring Boot 來完成所有的幕后配置,並使用 XML DSL 來配置 Camel 路由。 最初的 Camel 消費者應用程序根本不使用 Spring。

我進行了看似有意義的更改,但無法運行應用程序。 我收到以下異常:

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)

我不明白它在抱怨什么。 我在下面包含了資源,以便人們可以查看我在做什么。 對於代碼量,我提前道歉。

應用程序主.java:

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

配置器.java:

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

應用程序.yml:

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

駱駝-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.xml:

注意:我不知道我是否對下面路由定義中的元素使用了正確的語法。 我正在嘗試從原始版本中改編它,這是在 Java DSL 中完成的。 我也不確定是否需要處理器 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>

我的處理器.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:

注意:您可能會注意到 Spring Boot Camel starters 以及 Camel 組件(如 HTTP)的 Camel 依賴項的混合。 我不清楚我真正需要哪些。 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>

啊。 非常棘手且難以發現問題。

事實證明,我的 Maven 存儲庫 (.m2/repository) 中存在我需要清除的依賴項。 我認為它們是由帖子開頭指出的原始項目帶來的。

然后,我需要編輯 eclipse 項目屬性以刪除舊的(完全不需要的)jar。 完成此操作並完成干凈的構建后,應用程序將按原樣啟動。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM