1
votes

UseCase: In my app have one REST Controller, developed with the help of the Spring boot, my requirement is, I have to pass the request data from controller to the route, from route again need to pass data to MQ

Here how can I pass the inputReq data from controller to the route? could anyone please help

@Controller
public class RequestController {

    @PostMapping("/request")
    public String requestMapping(@RequestBody String inputReq) {
        new ProduceRouter();  // instance of the apache camel route
        return null;

    }
}

Below one the apache camel route:

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

        

    }
}
1

1 Answers

1
votes

In your Controller, autowire an instance of the CamelContext and ProducerTemplate.

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

You then need to create an exchange request with the ExchangeBuilder and add your request body .

Exchange exchangeRequest = ExchangeBuilder.anExchange(camelContext)
.withBody(inputReq).build();

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).

Then in your route file, you can consume from direct:startRoute