Srikanth Janapati
Srikanth Janapati

Reputation: 443

How can i receive the response from Apache camel route to the Spring boot controller

From the route I'm sending data to endpoint direct: receive and I'm receiving same in the controller, by using consumertemplate but getting below error

[Camel (camel-1) thread #1 - JmsConsumer[MQT.EI.PRD2X4_PRODUCER_UT.REPLY.Q1]] ERROR o.a.c.processor.DefaultErrorHandler.log - Failed delivery for (MessageId: ID:c3e2d840d4d8e3f14040404040404040d85c2573b4cf7342 on ExchangeId: ID-APINP-ELPT60235-1597255599355-0-5). Exhausted after delivery attempt: 1 caught: org.apache.camel.component.direct.DirectConsumerNotAvailableException: No consumers available on endpoint: direct://recive. Exchange[ID-APINP-ELPT60235-1597255599355-0-5]

Can anyone please give the suggestion on how can i get the response data from the route to the controller?

Controller code:

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

    @PostMapping("/request-reply")
    public String requestReplyMapping(@RequestBody String inputReq) {

        Exchange exchangeRequest = ExchangeBuilder.anExchange(camelContext).withBody(inputReq).build();
        Exchange exchangeResponse = producer.send("direct:request-reply", exchangeRequest);

        Exchange receive = consumer.receive("direct:receive"); //receiving data from this endpoint
        return null;

    }

Route Code:

@Component
public class RequestReplyRouter extends RouteBuilder {

    @Override
    public void configure() throws Exception {

        from("direct:request-reply").
        to("jms:RequestQueue?ReplyTo=ResponseQueue&exchangePattern=InOut")
        .log("Request-reply Body is  ${body}   and header info is   ${headers}  ");

        from("jms:ResponseQueue")
        .log("Received Body is  ${body}   and header info is   ${headers}  ")
        .to("direct:receive"); // sending data to the endpoint
    }

}

Upvotes: 0

Views: 3197

Answers (1)

Sneharghya Pathak
Sneharghya Pathak

Reputation: 1060

You don't need a consumerTemplate for what you are attempting to do. Direct component by default has a exchange pattern of InOut. So you can use the exchangeResponse variable to get the exchange after your camel routes have been processed.

Camel route:

from("direct:start")
.log("${body} in stat")
.transform().simple("text")
.to("direct:nextRoute");

from("direct:nextRoute")
.log("${body} in nextRoute");

Rest Controller:

public class RestController{

    final static String CAMEL_START_URI ="direct:start";

    @Autowired
    CamelContext camelContext;

    @Autowired
    @EndpointInject(uri=CAMEL_START_URI)
    ProducerTemplate producer;

    @PostMapping(value = "/request")
    public String requestMapping(@RequestBody String inputReq) {
        
        Exchange sendExchange = ExchangeBuilder.anExchange(camelContext).withBody(inputReq).build();
        Exchange outExchange = producer.send(sendExchange);
        String outString = outExchange.getMessage().getBody(String.class);
        System.out.println(outString); //this will have a value of "text" since I am setting the body as "text" in the second route
        return outString; 
    }

}

Upvotes: 2

Related Questions