如何解决org.apache.camel.FailedToCreateProducerException CaughtExceptionMessage: Failed to create Producer for endpoint?

How to solve org.apache.camel.FailedToCreateProducerException CaughtExceptionMessage: Failed to create Producer for endpoint?

我将 Camel 组件和代码从 2.X.X 升级到 3.7.3

此外,我在 Java 11 中重新编译了源代码,然后才在 Java 8 中工作。

路线示例:

from(direct:xyz)
  .to(URL_A)
  .transform()
  ...

问题出现是因为在 to 之后,我得到 null

我从 camel-http4 移动到 camel-http,检查了库的属性,但仍然无法找出问题所在?

我得到的错误:

CaughtExceptionType: org.apache.camel.FailedToCreateProducerException  CaughtExceptionMessage: Failed to create Producer for endpoint: direct:xyz1616073923650. Reason: java.lang.NullPointerException  StackTrace: org.apache.camel.FailedToCreateProducerException: Failed to create Producer for endpoint: direct:xyz1616073923650. Reason: java.lang.NullPointerException
    at org.apache.camel.support.cache.DefaultProducerCache.acquireProducer(DefaultProducerCache.java:154)
    at org.apache.camel.processor.RecipientListProcessor.createProcessorExchangePairs(RecipientListProcessor.java:219)
    at org.apache.camel.processor.MulticastProcessor.process(MulticastProcessor.java:276)
    at org.apache.camel.processor.RecipientList.sendToRecipientList(RecipientList.java:222)
    at org.apache.camel.processor.RecipientList.process(RecipientList.java:185)
    at org.apache.camel.processor.Pipeline$PipelineTask.run(Pipeline.java:90)
    at org.apache.camel.impl.engine.DefaultReactiveExecutor$Worker.schedule(DefaultReactiveExecutor.java:148)
    at org.apache.camel.impl.engine.DefaultReactiveExecutor.scheduleMain(DefaultReactiveExecutor.java:60)
    at org.apache.camel.processor.Pipeline.process(Pipeline.java:147)
    at org.apache.camel.impl.engine.CamelInternalProcessor.process(CamelInternalProcessor.java:312)
    at org.apache.camel.component.direct.DirectProducer.process(DirectProducer.java:84)
    at org.apache.camel.impl.engine.SharedCamelInternalProcessor.process(SharedCamelInternalProcessor.java:218)
    at org.apache.camel.impl.engine.SharedCamelInternalProcessor.process(SharedCamelInternalProcessor.java:112)
    at org.apache.camel.impl.engine.DefaultAsyncProcessorAwaitManager.process(DefaultAsyncProcessorAwaitManager.java:83)
    at org.apache.camel.impl.engine.SharedCamelInternalProcessor.process(SharedCamelInternalProcessor.java:109)
    at org.apache.camel.support.cache.DefaultProducerCache.send(DefaultProducerCache.java:189)
    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)
    at org.apache.camel.impl.engine.DefaultProducerTemplate.sendBody(DefaultProducerTemplate.java:457)

评论:我什至找不到更远的出路,所以欢迎任何建议。

更新:

进一步调试后,我了解到错误在路由中。

from(direct:xyz)
  .to(URL_A)
  .transform()
  ...
  .recipientList(exchangeProperty("event"))

更新 2:

我在 Camel Jira 版块中发布了 question/bug

更新 3:

我添加一个我测试的例子class是的,测试的路由和上面的一样。

Spring启动路由的启动应用程序,上下文:

@SpringBootApplication
@PropertySource("classpath:application-test.properties")
class TestApp {

    public static void main(String[] args) {
        SpringApplication.run(TestApp.class, args);
    }
}

自测:

@RunWith(SpringRunner.class)
@TestPropertySource(locations = "application-test.properties")
@SpringBootTest(classes = TestApp.class, webEnvironment = DEFINED_PORT)
public class FunctionalTest {

    @Autowired
    private EventConsumeCreator eventConsumerCreator;

    private List<Endpoint> consumers;

    private static final MockComponent MOCK_COMPONENT = new MockComponent();
    
    @Before
    public void before() {
        // Setting Wiremock and LogAsserts

        consumers = eventConsumerCreator.getConsumers();
      
        consumers.clear();
        consumers.add(new MockEndpoint("direct:xyz", MOCK_COMPONENT));

    }

    @Test
    public checkNumEvents() {
      // Make requests to endpoints
      // Verify logs
      // Check wiremock asserts

      assertThat(numEvents(consumers), is(1));
    }

}

用于创建消费者的实用程序 class:

@Component
public class EventConsumerCreator {

    private List<Endpoint> consumers = new LinkedList<>();

    private CamelContext camelContext;

    @Autowired
    public EventConsumerCreator(CamelContext camelContext) {
        this.camelContext = camelContext;
    }

    public List<Endpoint> getConsumers() {
        return consumers;
    }

    public Endpoint createConsumer(String identifier) {
        Endpoint endpoint = createEndpoint(identifier);
        consumers.add(endpoint);
        return endpoint;
    }

    private Endpoint createEndpoint(String identifier) {
        DirectComponent component = camelContext.getComponent("direct", DirectComponent.class);
        try {
            return component.createEndpoint("direct://" + identifier);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}

感谢@Claus Ibsen 在 Jira 中的回复,我解决了这个问题。

解决方案是:

  1. FunctionalTest
  2. 中添加@Autowired private CamelContext camelContext
  3. @Before 中遍历所有端点并执行
camelContext.addEndpoint(endpoint.getEndpointUri(), endpoint);
endpoint.start();
  1. @After语句中,停止运行个端点
endpoint.stop();
camelContext.removeEndpoint(endpoint);

评论: 如果遇到这种情况,请考虑是否可以依赖 Spring/Camel 自动配置。如果没有,请尝试手动重新创建类似 Camel 的步骤。