如何将请求数据从 spring 引导控制器传递到 apache camel 路由
how to pass the request data from spring boot controller to the apache camel route
用例: 在我的应用程序中有一个 REST 控制器,是在 Spring 启动的帮助下开发的,我的要求是,我必须从controller 到 route,从 route 又需要传数据给 MQ
这里如何将 inputReq 数据从控制器传递到路由?谁能帮忙
@Controller
public class RequestController {
@PostMapping("/request")
public String requestMapping(@RequestBody String inputReq) {
new ProduceRouter(); // instance of the apache camel route
return null;
}
}
下面是 apache 骆驼路线:
@Component
public class ProduceRouter extends RouteBuilder {
@Override
public void configure() throws Exception {
.from("jms:RequestQueue?disableReplyTo=true")
.log("Received Body is ${body} and header info is ${headers} ");
}
}
在您的控制器中,自动装配 CamelContext 和 ProducerTemplate 的实例。
@Autowired
private CamelContext camelContext;
@Autowired
private ProducerTemplate producer;
然后您需要使用 ExchangeBuilder 创建一个交换请求并添加您的请求正文。
Exchange exchangeRequest = ExchangeBuilder.anExchange(camelContext)
.withBody(inputReq).build();
然后您可以调用生产者对象上的发送方法来接入您的路由并捕获响应。
Exchange exchangeResponse = producer.send("direct:startRoute", exchangeRequest).
然后在你的路由文件中,你可以从direct:startRoute
消费
用例: 在我的应用程序中有一个 REST 控制器,是在 Spring 启动的帮助下开发的,我的要求是,我必须从controller 到 route,从 route 又需要传数据给 MQ
这里如何将 inputReq 数据从控制器传递到路由?谁能帮忙
@Controller
public class RequestController {
@PostMapping("/request")
public String requestMapping(@RequestBody String inputReq) {
new ProduceRouter(); // instance of the apache camel route
return null;
}
}
下面是 apache 骆驼路线:
@Component
public class ProduceRouter extends RouteBuilder {
@Override
public void configure() throws Exception {
.from("jms:RequestQueue?disableReplyTo=true")
.log("Received Body is ${body} and header info is ${headers} ");
}
}
在您的控制器中,自动装配 CamelContext 和 ProducerTemplate 的实例。
@Autowired
private CamelContext camelContext;
@Autowired
private ProducerTemplate producer;
然后您需要使用 ExchangeBuilder 创建一个交换请求并添加您的请求正文。
Exchange exchangeRequest = ExchangeBuilder.anExchange(camelContext)
.withBody(inputReq).build();
然后您可以调用生产者对象上的发送方法来接入您的路由并捕获响应。
Exchange exchangeResponse = producer.send("direct:startRoute", exchangeRequest).
然后在你的路由文件中,你可以从direct:startRoute