是否可以在自定义组件中使用骆驼组件? [英] Is it possible to use camel components within a custom component?

查看:30
本文介绍了是否可以在自定义组件中使用骆驼组件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近开始使用 Apache Camel,我们正在研究创建自定义组件来抽象大量逻辑并简化路由,但其中一些逻辑涉及 http 请求和其他具有我们想要利用的现有骆驼组件的部分.

I have recently started with Apache Camel, and we are looking into creating custom components to abstract a lot of logic and simplify routes, but some of this logic involves http requests and other portions that have an existing camel component we want to utilize.

是否可以从我们的自定义组件的生产者中调用其他组件(例如 http 组件)?

Is it possible to call other components (e.g. the http component) from within our custom component's producer?

我确实看到了这个问题(可以自定义 Camel 组件在内部使用路由和其他组件?)提到使用骆驼上下文,但是如何在 RouteBuilder 之外复制路由调用?

I did see this question (Can a custom Camel component use routes and other components internally?) that mentions using the camel context, but how to you replicate the route call outside of a RouteBuilder?

推荐答案

需要导入 CamelContext、Exchange、ProducerTemplate 和 ExchangeBuilder.

You need to import CamelContext, Exchange, ProducerTemplate and ExchangeBuilder.

import org.apache.camel.CamelContext;
import org.apache.camel.Exchange;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.builder.ExchangeBuilder;

然后您需要创建生产者模板和骆驼上下文的实例.我正在使用 spring boot,所以我可以注入依赖项.

You then need to create instances of the producer template and camel context. I am using spring boot, so I can just inject the dependencies.

@Autowired
private ProducerTemplate producer;
@Autowired
private CamelContext camelContext;

在您的方法定义中,您需要使用 ExchangeBuilder 创建一个交换请求.您可以在此处创建正文并为交换消息添加标题.

In your method definition, you need to create an exchange request with the ExchangeBuilder. You can create a body and add headers to you exchange message here.

Exchange exchangeRequest = ExchangeBuilder.anExchange(camelContext)
.withBody("Hello World!")
.withHeader("username", "jdoe")
.withHeader("password", "pass")
.build();

然后您可以调用生产者对象上的 send 方法来访问您的路由并捕获响应.

You can then call the send method on the producer object to tap into your route and capture the response.

Exchange exchangeResponse = producer.send("direct:startRoute", exchangeRequest)

这篇关于是否可以在自定义组件中使用骆驼组件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆