1

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

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

路线示例:

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

问题来了,因为之后to,我得到了null

我从camel-http4to 移动到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$1.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 板上发布了一个问题/错误。

更新 3:

我添加了一个我的测试类的例子,被测试的路由看起来像上面的那个。

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

@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));
    }

}

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

@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);
        }
    }
}
4

1 回答 1

0

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

解决方案是:

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

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

于 2021-03-19T16:55:45.773 回答