当前位置: 首页 > 知识库问答 >
问题:

Apache Camel-DirectConsumerNotAvailableException:endpoint上没有可用的使用者。交换[]

丁经国
2023-03-14

有人能帮我解决这个错误并解释我怎么了吗?

我正在开发一个简单的驼峰路径来处理信息,然后将其存储在文件中。

我正在使用的技术:

  • JDK 11
  • Apache Camel 3.3.0
  • Spring靴2.3.0

当我想测试骆驼路线时,我得到了以下错误。

org.apache.camel.CamelExecutionException: Exception occurred during execution on the exchange: Exchange[]

    at org.apache.camel.CamelExecutionException.wrapCamelExecutionException(CamelExecutionException.java:47)
    at org.apache.camel.support.ExchangeHelper.extractResultBody(ExchangeHelper.java:687)
    at org.apache.camel.impl.engine.DefaultProducerTemplate.extractResultBody(DefaultProducerTemplate.java:568)
    at org.apache.camel.impl.engine.DefaultProducerTemplate.extractResultBody(DefaultProducerTemplate.java:564)
    at org.apache.camel.impl.engine.DefaultProducerTemplate.sendBody(DefaultProducerTemplate.java:189)
    at org.apache.camel.impl.engine.DefaultProducerTemplate.sendBody(DefaultProducerTemplate.java:430)
    at com.facturacionx.ms.invoice.camel.CamelTest.testMethod(CamelTest.java:40)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.springframework.test.context.junit4.statements.RunBeforeTestExecutionCallbacks.evaluate(RunBeforeTestExecutionCallbacks.java:74)
    at org.springframework.test.context.junit4.statements.RunAfterTestExecutionCallbacks.evaluate(RunAfterTestExecutionCallbacks.java:84)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
    at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
    at org.junit.rules.TestWatcher$1.evaluate(TestWatcher.java:61)
    at org.junit.rules.TestWatcher$1.evaluate(TestWatcher.java:61)
    at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:366)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:251)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)
    at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329)
    at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
    at org.junit.rules.ExternalResource$1.evaluate(ExternalResource.java:54)
    at org.junit.rules.RunRules.evaluate(RunRules.java:20)
    at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:413)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
    at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
    at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:220)
    at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:53)
Caused by: org.apache.camel.component.direct.DirectConsumerNotAvailableException: No consumers available on endpoint: direct://my-route. Exchange[]
    at org.apache.camel.component.direct.DirectProducer.process(DirectProducer.java:59)
    at org.apache.camel.processor.SharedCamelInternalProcessor.process(SharedCamelInternalProcessor.java:212)
    at org.apache.camel.processor.SharedCamelInternalProcessor$1.process(SharedCamelInternalProcessor.java:110)
    at org.apache.camel.impl.engine.DefaultAsyncProcessorAwaitManager.process(DefaultAsyncProcessorAwaitManager.java:83)
    at org.apache.camel.processor.SharedCamelInternalProcessor.process(SharedCamelInternalProcessor.java:107)
    at org.apache.camel.impl.engine.DefaultProducerCache.send(DefaultProducerCache.java:185)
    at org.apache.camel.impl.engine.DefaultProducerTemplate.send(DefaultProducerTemplate.java:176)
    at org.apache.camel.impl.engine.DefaultProducerTemplate.send(DefaultProducerTemplate.java:172)
    at org.apache.camel.impl.engine.DefaultProducerTemplate.send(DefaultProducerTemplate.java:153)
    at org.apache.camel.impl.engine.DefaultProducerTemplate.sendBody(DefaultProducerTemplate.java:187)
    ... 39 more

这是我的路线

@Component
public class XmlRoute extends RouteBuilder {

    @Autowired
    private MyProcessor myProcessor;

    private JaxbDataFormat jaxb = new JaxbDataFormat(true);

    @Override
    public void configure() throws Exception {

        from("direct:my-route")
                .process(myProcessor)
                .marshal(jaxb)
                .log(LoggingLevel.INFO, "${body}")
                .setHeader(Exchange.CONTENT_TYPE, constant(MediaType.TEXT_XML_VALUE))
                .setHeader(Exchange.FILE_NAME, constant("output.xml"))
            .to("file:outputFolder");
    }
}

这是我的测试用例

@RunWith(CamelSpringBootRunner.class)
@ContextConfiguration
public class CamelTest extends CamelTestSupport {

    @Produce("direct:my-route")
    protected ProducerTemplate template;

    @EndpointInject("mock:file:outputFolder")
    protected MockEndpoint mock;

    @DirtiesContext
    @Test
    public void testMethod() throws InterruptedException, NullPointerException, JsonProcessingException {
        // Arrange
        ObjectMapper objectMapper = new ObjectMapper();

        mock.expectedBodyReceived();
        Invoice invoice = InvoiceTestDataBuilder.anInvoice().build();

        // Act
        template.sendBody(objectMapper.writeValueAsString(invoice));

        // Assert
        mock.assertIsSatisfied();
    }
}

这是我的gradle档案

/*
 * This file was generated by the Gradle 'init' task.
 */

plugins {
    id 'org.springframework.boot' version '2.3.0.RELEASE'
    id 'io.spring.dependency-management' version '1.0.9.RELEASE'
    id 'java'
    id 'org.unbroken-dome.xjc' version '1.4.3'
}

repositories {
    mavenCentral()
}

xjcGenerate {
    source = fileTree('src/main/resources/XSD/maindoc') { include '*.xsd' }
}


dependencies {
    implementation('org.springframework.boot:spring-boot-starter-web') {
        exclude group: 'org.springframework.boot', module: 'spring-boot-starter-tomcat'
    }
    implementation 'org.springframework.boot:spring-boot-starter-undertow'
    implementation 'org.springframework.boot:spring-boot-starter-actuator'

    // Camel
    implementation 'org.apache.camel.springboot:camel-spring-boot-starter:3.3.0'
    implementation 'org.apache.camel.springboot:camel-jaxb-starter:3.3.0'
    implementation 'org.apache.camel.springboot:camel-zipfile-starter:3.3.0'
    implementation 'org.apache.camel.springboot:camel-direct:3.3.0'

    implementation 'io.springfox:springfox-swagger2:2.9.2'
    implementation 'io.springfox:springfox-swagger-ui:2.9.2'

    implementation 'com.github.joschi.jackson:jackson-datatype-threetenbp:2.6.4'
    implementation 'javax.validation:validation-api:1.1.0.Final'
    testImplementation 'org.springframework.boot:spring-boot-starter-test:1.5.22.RELEASE'
    testImplementation 'org.apache.camel:camel-test-spring:3.3.0'

}

group = 'com.ejemplo.ms'
version = '1.0.0'
description = 'swagger-spring'
sourceCompatibility = '11'
targetCompatibility = '11'


tasks.withType(JavaCompile) {
    options.encoding = 'UTF-8'
}

共有2个答案

茹轩昂
2023-03-14

我找到了解决办法,

我添加了两个注释类:@SpringBootTest和@DirtiesContext

我还添加了一个属性:@Autowired CamelContext;我还添加了另一个属性,路由:@InjectMocks XmlRoute XmlRoute;

@SpringBootTest
@RunWith(CamelSpringBootRunner.class)
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
public class CamelTest {

    @Autowired
    private CamelContext camelContext;

    @Produce(value = "direct:my-route")
    protected ProducerTemplate template;

    @EndpointInject("mock:file:outputFolder")
    protected MockEndpoint mock;

    @InjectMocks
    protected XmlRoute xmlRoute;

    @Autowired
    private ObjectMapper objectMapper;

    @Test
    public void testMethod() throws InterruptedException, NullPointerException, JsonProcessingException {
        // Arrange

        mock.expectedBodyReceived();
        Invoice invoice = InvoiceTestDataBuilder.anInvoice().build();

        // Act
        template.sendBody(invoice);

        // Assert
        mock.assertIsNotSatisfied();
    }
}

有了这些变化,测试工作正常。

谢谢大家的帮助。

应和光
2023-03-14

您需要在POM文件中直接导入camel。自camel 3以来,camel core中不再包含direct。

 类似资料:
  • 我是阿帕奇骆驼队的新手。我试图将一个交换从java方法发送到一个路由,但它给了我“由:org.apache.camel.component.direct.DirectConsumerNotAvailableException:endpoint上没有可用的消费者”错误。我想知道这个错误到底是什么,我们什么时候得到这个?

  • 我是的新手,正在尝试使用组件。 我在这里简化了代码,但基本上我有一个参与者,它从endpoint使用。 因此,我尝试这种“直接”方法,但在endpoint:endpoint[direct://mycamelendpoint]上没有可用的消费者。Exchange[message:My test message]异常。 我在这里缺少什么配置?

  • 我用的是骆驼2.13.1,有一个奇怪的行为。如果我想测试我的路由,我已经用XML编写了。 我总是在一条路由上得到没有消费者可用的信息。如果我把它部署在Tomcat上,一切都正常工作。即使在启动时,我也可以看到日志,它清楚地说明路由已被消耗。 我的路由如下所示: 我这样开始测试: 我基本上只是开始我的Spring-Context。spring-module.xml加载多个camel-context.

  • 我需要在SE环境中使用没有CDI容器的Jersey 2.28(带Jetty)。我的所有设置都在web.xml中: 以下是我使用的依赖项: 我得到的是: 我知道Jersey可以与不同的DI容器一起使用,例如Weld、HK2等,但是否可以不使用DI容器?如果是,那又是怎样做的呢?

  • 我想使用Camel从ActiveMQ获取一条消息,然后根据消息内容(protobuf)向Twitter发送一条或多条消息。我编写了一个从路由内调用的bean,它使用注入将多条消息发送到“direct:xyz”endpoint。 这个豆子看起来像:- 我在其他路线上也遇到过这个问题(这肯定与Twitter功能无关),但刚刚解决了这个问题。然而,这一次,我想真正理解问题是什么!如有任何帮助,不胜感激,