正在尝试从springboot调用camel/javaee

yqyhoc1h  于 2022-11-07  发布在  Apache
关注(0)|答案(1)|浏览(170)

我尝试从spring-boot调用camel/java-ee/class(这是很长的程序,所以没有时间转换),所以现在的解决方案是从spring-boot开始,调用主类。
这是springboot的主要应用程序,它将调用javaee/class,我以前尝试过简单的bean调用,所以这是我最新的测试,因为我认为这是关于bean的正确调用,但不幸的是,即使是bean的正确调用也会出现与bean的简单调用相同的错误,

@SpringBootApplication
public class App {
    public static void main(String[] args) throws Exception {
        ApplicationContext applicationContext = SpringApplication.run(App.class, args);
        BeanAppService service = applicationContext.getBean(BeanAppService.class);
        service.callInitCamelJavaEE();
    }
}

服务接口、

public interface BeanAppService {

    void callInitCamelJavaEE() throws Exception;

}

配置、

@Configuration
public class BeanAppConfig {

    @Bean
    public BeanAppService beanAppConfig() {
        return new BeanApp();
    }

}

BeanApp(这是java/ee中带有参数的原始主类),

public class BeanApp implements BeanAppService {
    public BeanApp() {}

    protected <I extends SpongeRequest, O extends SpongeResponse> void createOperation(RestDefinition restDefinition, String operation,
    String operationDescription, Class<I> requestClass, String requestDescription, Class<O> responseClass,
    String responseDescription, BiFunction<I, Exchange, O> operationHandler) {
  restDefinition.post(operation).description(operationDescription).type(requestClass).outType(responseClass).param().name("body")
      .type(body).description(requestDescription).endParam().responseMessage().code(200).message(responseDescription)
      .endResponseMessage().route().id("sponge-" + operation).process(exchange -> {
        String requestBody = exchange.getIn().getBody(String.class);
        if (logger.isDebugEnabled()) {
          logger.debug("REST API {} request: {}", operation, RestApiUtils.obfuscatePassword(requestBody));
        }
        try {
          setupResponse(operation, exchange,
              operationHandler.apply(getObjectMapper().readValue(requestBody, requestClass), exchange));
        } catch (Throwable processingException) {
          logger.info("REST API error", processingException);
          try {
            setupResponse(operation, exchange, apiService.createGenericErrorResponse(processingException, exchange));
          } catch (Throwable e) {
            logger.error("REST API send error response failure", e);
            throw e;
          }
        }
      }).endRest();
    }

    @Override
    public void callInitCamelJavaEE() throws Exception {
        try (CamelContext camelContext = new DefaultCamelContext()) {
            camelContext.addRoutes(new RouteBuilder() {
                @Override
                public void configure() throws Exception {
                    restConfiguration().enableCORS(true).setPort("8080");
                }
            });
            camelContext.start();
            Thread.sleep(6000000);
            camelContext.stop();
        }   

    }

错误、

StreamCaching is not in use. If using streams then it's recommended to enable stream caching. See more details at http://camel.apache.org/stream-caching.html
Error starting CamelContext (camel-2) due to exception thrown: Failed to start route route1 because of null
org.apache.camel.FailedToStartRouteException: Failed to start route route1 because of null
    at org.apache.camel.impl.engine.RouteService.warmUp(RouteService.java:122) ~[camel-base-engine-3.7.0.jar:3.7.0]
    at org.apache.camel.impl.engine.InternalRouteStartupManager.doWarmUpRoutes(InternalRouteStartupManager.java:270) ~[camel-base-engine-3.7.0.jar:3.7.0]
    at org.apache.camel.impl.engine.InternalRouteStartupManager.safelyStartRouteServices(InternalRouteStartupManager.java:157) ~[camel-base-engine-3.7.0.jar:3.7.0]
    at org.apache.camel.impl.engine.InternalRouteStartupManager.doStartOrResumeRoutes(InternalRouteStartupManager.java:115) ~[camel-base-engine-3.7.0.jar:3.7.0]
    at org.apache.camel.impl.engine.AbstractCamelContext.doStartCamel(AbstractCamelContext.java:2889) ~[camel-base-engine-3.7.0.jar:3.7.0]
    at org.apache.camel.impl.engine.AbstractCamelContext.doStartContext(AbstractCamelContext.java:2702) ~[camel-base-engine-3.7.0.jar:3.7.0]
    at org.apache.camel.impl.engine.AbstractCamelContext.doStart(AbstractCamelContext.java:2665) ~[camel-base-engine-3.7.0.jar:3.7.0]
    at org.apache.camel.support.service.BaseService.start(BaseService.java:115) ~[camel-api-3.7.0.jar:3.7.0]
    at org.apache.camel.impl.engine.AbstractCamelContext.start(AbstractCamelContext.java:2431) ~[camel-base-engine-3.7.0.jar:3.7.0]
    at ca.toronto.eis.dwork2.BeanApp.BeanAppApp.callMain(BeanAppApp.java:531) ~[main/:?]
    at ca.toronto.eis.dwork2.Dwork2Application.main(Dwork2Application.java:29) ~[main/:?]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_202]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_202]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_202]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_202]
    at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) ~[spring-boot-devtools-2.7.0.jar:2.7.0]
Caused by: java.lang.IllegalStateException: Cannot find RestConsumerFactory in Registry or as a Component to use
    at org.apache.camel.component.rest.RestEndpoint.createConsumer(RestEndpoint.java:602) ~[camel-rest-3.7.0.jar:3.7.0]
    at org.apache.camel.impl.engine.DefaultRoute.addServices(DefaultRoute.java:575) ~[camel-base-engine-3.7.0.jar:3.7.0]
    at org.apache.camel.impl.engine.DefaultRoute.onStartingServices(DefaultRoute.java:160) ~[camel-base-engine-3.7.0.jar:3.7.0]
    at org.apache.camel.impl.engine.RouteService.doWarmUp(RouteService.java:150) ~[camel-base-engine-3.7.0.jar:3.7.0]
    at org.apache.camel.impl.engine.RouteService.warmUp(RouteService.java:120) ~[camel-base-engine-3.7.0.jar:3.7.0]
    ... 15 more
Apache Camel 3.7.0 (camel-2) is shutting down
Apache Camel 3.7.0 (camel-2) uptime 28ms
Apache Camel 3.7.0 (camel-2) is shutdown in 10ms

有没有人遇到过这种情况?

kmbjn2e3

kmbjn2e31#

似乎在路由生成器中您尚未配置任何路由。

@Override
public void callInitCamelJavaEE() throws Exception {
    try (CamelContext camelContext = new DefaultCamelContext()) {
        camelContext.addRoutes(new RouteBuilder() {
            @Override
            public void configure() throws Exception {
                restConfiguration().enableCORS(true).setPort("8080");
            }
        });
        camelContext.start();
        Thread.sleep(6000000);
        camelContext.stop();
    }   

}

假设您使用的是Rest here是如何使用REST DSL在RouteBuilder中配置路由

相关问题