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

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
}
}

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

Related

CircuitBreaker Fallback method not working

I have the below code in Billing service microservice:
#RestController
#RequestMapping("/billing")
public class WebController {
#Autowired
private BillingService service;
#GetMapping("/hi")
#CircuitBreaker(name="BillingServiceCapture", fallbackMethod = "hiFallback")
public String hi() {
return "Hello Khushboo!";
}
public String hiFallback() {
return "Hello Khushboo FallBack!";
}
Application.Properties file:
server.port=9191
spring.h2.console.enable=true
spring.application.name=billing-service
eureka.client.serviceurl.defaultzone=http://localhost:8761/eureka
eureka.instance.hostname=localhost
management.health.circuitbreakers.enabled=true
#actuator settings
management.endpoints.web.exposure.include=*
management.endpoint.health.show-details=always
resilience4j.circuitbreaker.instances.BillingServiceCapture.registerHealthIndicator=true
resilience4j.circuitbreaker.instances.BillingServiceCapture.eventConsumerBufferSize=10
resilience4j.circuitbreaker.instances.BillingServiceCapture.failureRateThreshold=20
resilience4j.circuitbreaker.instances.BillingServiceCapture.minimumNumberOfCalls=5
resilience4j.circuitbreaker.instances.BillingServiceCapture.automaticTransitionFromOpenToHalfOpenEnabled=true
resilience4j.circuitbreaker.instances.BillingServiceCapture.waitDurationInOpenState=5s
resilience4j.circuitbreaker.instances.BillingServiceCapture.permittedNumberOfCallsInHalfOpenState=3
resilience4j.circuitbreaker.instances.BillingServiceCapture.slidingWindowSize=10
resilience4j.circuitbreaker.instances.BillingServiceCapture.slidingWindowType=COUNT_BASED
However, if I send a Get Request: localhost:8765/billing/hi
I get Hello Khushboo message.
But when I stop the BillingService microservice and again send the same request, the circuit breaker method doesn't get invoked.
Also, while accessing the Actuator Health status, I do not see circuit breaker information in the status logs which I should see.
I even added the CircuitBreaker code in OrderService which actually calls the BillingService:
#CircuitBreaker(name="BillingServiceCapture", fallbackMethod = "getAllBillingDetails")
public TransactionResponse saveOrder(TransactionRequest request) {
Order order=request.getOrder();
Billing billing=request.getBilling();
billing.setOrderId(order.getId());
billing.setAmount(order.getPrice());
Order ordermade=orderRepo.save(order);
Billing billingresponse=billingproxy.getBillingDone(billing);
TransactionResponse response=null;
String responseStr= billingresponse.getPaymentStatus().equals("success")?"Payment processing successful":"Payment failed";
response=new TransactionResponse(order, billingresponse.getTransactionId(),billingresponse.getAmount(),responseStr);
return response;
}
public Billing getAllBillingDetails(Billing bill,Exception e) {
return new Billing(1000,"pass",101,102,1000);
}
When I call http://localhost:8765/order/bookorder - this throws a 500 internal server exception but CircuitBreaker is not called. The error is:
[503] during [GET] to [http://billing-service/billing/preparebill] [BillingProxy#getBillingDone(Billing)]: [Load balancer does not contain an instance for the service billing-service]
Please note for testing purpose I'm not starting BillingService so the instance is not available for OrderService Feign to call.
Any insights will be appreciated.
Thanks.
The fallback method should pass the Exception parameter and return the same type as the original method:
public String hiFallback(Exception e) {
return "Hello Khushboo FallBack!";
}

Spring Integration Mqtt : DestinationResolutionException: no output-channel or replyChannel header available

Please can someone help me to understand where is the probleme in this config:
Versions :
org.springframework.integration:spring-integration-mqtt:5.5.2
org.springframework.boot:spring-boot-starter:2.5.3
org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.2.5
#Configuration
public class MqttConfig {
#Bean
public MqttPahoClientFactory mqttClientFactory() {
DefaultMqttPahoClientFactory factory = new DefaultMqttPahoClientFactory();
MqttConnectOptions options = new MqttConnectOptions();
options.setServerURIs(new String[] { "tcp://localhost:1883" });
return factory;
}
#Bean
public MqttPahoMessageDrivenChannelAdapter inboundAdapter(MqttPahoClientFactory clientFactory) {
return new MqttPahoMessageDrivenChannelAdapter("MyApp", clientFactory, "ReplyTopic");
}
#Bean
IntegrationFlow inboundFlow(MqttPahoMessageDrivenChannelAdapter inboundAdapter) {
return IntegrationFlows.from(inboundAdapter)
.bridge()
.channel("replyChannel")
.get();
}
#Bean
public MessageChannel replyChannel() {
return MessageChannels.publishSubscribe().get();;
}
#Bean
public MqttPahoMessageHandler outboundAdapter(MqttPahoClientFactory clientFactory) {
return new MqttPahoMessageHandler("MyApp", clientFactory);
}
#Bean
public IntegrationFlow outboundFlow(MqttPahoMessageHandler outboundAdapter) {
return IntegrationFlows.from("requestChannel")
.handle(outboundAdapter).get()
}
#MessagingGateway
public interface MyGateway {
#Gateway(requestChannel = "requestChannel", replyChannel = "replyChannel")
String send(String request, #Header(MqttHeaders.TOPIC) String requestTopic);
}
}
Client code
#RestController
public class MyController {
#Autowired
private MyGateway myGateway;
#GetMapping("/sendRequest")
public String sendRequest() {
var response = myGateway.send("Hello", "MyTopic");
return response;
}
}
Usage:
curl http://localhost:8080/sendRequest
manual response from the mqtt broker (HiveMQ)
docker exec -it hivemq mqtt pub -t ReplyTopic -m "World" --debug
CLIENT mqttClient-MQTT_5_0-9ecded84-8416-4baa-a8f3-d593c692bc65: acknowledged PUBLISH: 'World' for PUBLISH to Topic: ReplyTopic
But I dont know why i have this message on the Spring application output
2022-10-25 18:04:33.171 ERROR 17069 --- [T Call: MyApp] .m.i.MqttPahoMessageDrivenChannelAdapter : Unhandled exception for GenericMessage [payload=World, headers={mqtt_receivedRetained=false, mqtt_id=0, mqtt_duplicate=false, id=9dbd5e14-66ed-5dc8-6cea-6d04ef19c6cc, mqtt_receivedTopic=ReplyTopic, mqtt_receivedQos=0, timestamp=1666713873170}]
org.springframework.messaging.MessageHandlingException: error occurred in message handler [org.springframework.integration.handler.BridgeHandler#6f63903c]; nested exception is org.springframework.messaging.core.DestinationResolutionException: no output-channel or replyChannel header available
Please can someone explain why i have this ?
no output-channel or replyChannel header available
I think the problem you are facing is not related to your bridge() configuration.
This comes from the MessagingGatewaySupport and its replyMessageCorrelator feature which is activated by your replyChannel = "replyChannel".
The real problem that you are trying to do what is not possible with MQTT v3. There is just no headers transferring over MQTT broker to carry on a required for gateway initiator a correlation key - the TemporaryReplyChannel. See more in docs about gateway: https://docs.spring.io/spring-integration/docs/current/reference/html/messaging-endpoints.html#gateway.
In other words: independently of the replyChannel configuration on gateway, the replyChannel header must be present in the reply message. This is the way how gateway correlates requests with replies.
You have to look into an aggregator to send the request message in parallel and to preserve the mentioned TemporaryReplyChannel header. Then when you receive a reply (inboundAdapter) you send it to this aggregator. You need to ensure some correlation key from a request and reply payload, so they can match and fulfill group for reply to be sent back to the gateway.
See more info in docs: https://docs.spring.io/spring-integration/docs/current/reference/html/message-routing.html#aggregator

Working fine with Spring JMS and getting problem with Apache camel route , IBM MQ Route

I'm Sending request to the Third party IBM MQ with space pattern, like below text (COBOL input file), but i'm getting reponse as a error in Reply queue like below but same input working fine spring jms, i'm facing this problem with apache camel
Sample Input:
GetProducerWithCommissionddRates
C26115
77104 99998
2010-01-01 011
Response:
printing data in controller:: EPPRD02007EAn invalid COBOL Date Format
was provided as input to the Producer Retrieval Service, Get Producer
With Commission Rates Operation: AsOfDate. Source:
(ProducerMediationModule)
ProducerMediationModule
Controller with camel:
#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);
return exchangeResponse.getIn().getBody(String.class);
}
Camel Route:
#Component
public class RequestReplyRouter extends RouteBuilder {
#Override
public void configure() throws Exception {
from("direct:request-reply").
to("jms:REQUEST.Q1?ReplyTo=REPLY.Q1&exchangePattern=InOut")
.log("Request-reply Body is ${body} and header info is ${headers} ");
from("jms:REPLY.Q1")
.log("Received Body is ${body} and header info is ${headers} ");
}
}
JMS Controller Code(working) :
#PostMapping(value="request-reply")
public ResponseEntity<String> sendRequestAndReceiveReply (#RequestBody String prd2x4Request) { System.out.println("received prd2x4 request: "+prd2x4Request);
String reply = prd2x4Wrapper.sendReqAndGetMessageId(prd2x4Request);
return new ResponseEntity<String>(reply, HttpStatus.OK);
}
JMS MQ Calling (working) :
private String sendReqAndGetMessage(String prd2x4Request) {
String messageId = "";
MQQueue requestQueue = new MQQueue(RequestQueue);
Session session = jmsTemplate.getConnectionFactory().createConnection().createSession(); MessageProducer mp = session.createProducer(requestQueue);
TextMessage message = session.createTextMessage(prd2x4Request);
message.setStringProperty("JMS_IBM_Character_Set", "IBM037");
mp.send(message);
messageId = message.getJMSMessageID();
mp.close();
session.close();
return messageId;
}

Request response over HTTP with Spring and activemq

I am building a simple REST api which connects a web server to a back end service, which performs a simple check and sends a response.
So client (over HTTP) -> to Web Server (over ACTIVEMQ/CAMEL)-> to Checking-Service, and back again.
The endpoint for the GET request is "/{id}". I'm trying to make this send a message through queue:ws-out to queue:cs-in and map it all the way back again to the original GET request.
The Checking-Service (cs) code is fine, it simply changes a value in the CheckMessage object to true using jmslistener.
I've searched the web thoroughly for examples, but can't get anything to work. The closest one I found was the following.
This is what I have so far on the Web Server (ws).
RestController
import ...
#RestController
public class RESTController extends Exception{
#Autowired
CamelContext camelContext;
#Autowired
JmsTemplate jmsTemplate;
#GetMapping("/{id}")
public String testCamel(#PathVariable String id) {
//Object used to send out
CheckMessage outMsg = new CheckMessage(id);
//Object used to receive response
CheckMessage inMsg = new CheckMessage(id);
//Sending the message out (working)
jmsTemplate.convertAndSend("ws-out", outMsg);
//Returning the response to the client (need correlation to the out message"
return jmsTemplate.receiveSelectedAndConvert("ws-in", ??);
}
}
Listener on ws
#Service
public class WSListener {
//For receiving the response from Checking-Service
#JmsListener(destination = "ws-in")
public void receiveMessage(CheckMessage response) {
}
}
Thanks!
your receive messages from "ws-in" with 2 consumers jmsTemplate.receiveSelectedAndConvert and WSListener !! message from a queue is consumed by one of the 2.
you send messages to "ws-out" and consume from "ws-in" ?? last queue
is empty and not receive any message, you have to send messages to
it
you need a valid selector to retrieve the message with receiveSelectedAndConvert based on JMSCorrelationID as the example you mntioned or the id received from the rest request but you need to add this id to the message headers like below
this.jmsTemplate.convertAndSend("ws-out", id, new MessageCreator() {
#Override
public Message createMessage(Session session) throws JMSException {
TextMessage tm = session.createTextMessage(new CheckMessage(id));
tm.setJMSCorrelationID(id);
return tm;
}
});
return jmsTemplate.receiveSelectedAndConvert("ws-in", "JMSCorrelationID='" + id+ "'");
forward messages from "ws-out" to "ws-in"
#Service
public class WSListener {
//For receiving the response from Checking-Service
#JmsListener(destination = "ws-out")
public void receiveMessage(CheckMessage response) {
jmsTemplate.convertAndSend("ws-in", response);
}
}

Can I use Spring WebFlux to implement REST services which get data through Kafka request/response topics?

I'm developing REST service which, in turn, will query slow legacy system so response time will be measured in seconds. We also expect massive load so I was thinking about asynchronous/non-blocking approaches to avoid hundreds of "servlet" threads blocked on calls to slow system.
As I see this can be implemented using AsyncContext which is present in new servlet API specs. I even developed small prototype and it seems to be working.
On the other hand it looks like I can achieve the same using Spring WebFlux.
Unfortunately I did not find any example where custom "backend" calls are wrapped with Mono/Flux. Most of the examples just reuse already-prepared reactive connectors, like ReactiveCassandraOperations.java, etc.
My data flow is the following:
JS client --> Spring RestController --> send request to Kafka topic --> read response from Kafka reply topic --> return data to client
Can I wrap Kafka steps into Mono/Flux and how to do this?
How my RestController method should look like?
Here is my simple implementation which achieves the same using Servlet 3.1 API
//took the idea from some Jetty examples
public class AsyncRestServlet extends HttpServlet {
...
#Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String result = (String) req.getAttribute(RESULTS_ATTR);
if (result == null) { //data not ready yet: schedule async processing
final AsyncContext async = req.startAsync();
//generate some unique request ID
String uid = "req-" + String.valueOf(req.hashCode());
//share it to Kafka receive together with AsyncContext
//when Kafka receiver will get the response it will put it in Servlet request attribute and call async.dispatch()
//This doGet() method will be called again and it will send the response to client
receiver.rememberKey(uid, async);
//send request to Kafka
sender.send(uid, param);
//data is not ready yet so we are releasing Servlet thread
return;
}
//return result as html response
resp.setContentType("text/html");
PrintWriter out = resp.getWriter();
out.println(result);
out.close();
}
Here's a short example - Not the WebFlux client you probably had in mind, but at least it would enable you to utilize Flux and Mono for asynchronous processing, which I interpreted to be the point of your question. The web objects should work without additional configurations, but of course you will need to configure Kafka as the KafkaTemplate object will not work on its own.
#Bean // Using org.springframework.web.reactive.function.server.RouterFunction<ServerResponse>
public RouterFunction<ServerResponse> sendMessageToTopic(KafkaController kafkaController){
return RouterFunctions.route(RequestPredicates.POST("/endpoint"), kafkaController::sendMessage);
}
#Component
public class ResponseHandler {
public getServerResponse() {
return ServerResponse.ok().body(Mono.just(Status.SUCCESS), String.class);
}
}
#Component
public class KafkaController {
public Mono<ServerResponse> auditInvalidTransaction(ServerRequest request) {
return request.bodyToMono(TopicMsgMap.class)
// your HTTP call may not return immediately without this
.subscribeOn(Schedulers.single()) // for a single worker thread
.flatMap(topicMsgMap -> {
MyKafkaPublisher.sendMessages(topicMsgMap);
}.flatMap(responseHandler::getServerResponse);
}
}
#Data // model class just to easily convert the ServerRequest (from json, for ex.)
// + ~#constructors
public class TopicMsgMap() {
private Map<String, String> topicMsgMap;
}
#Service // Using org.springframework.kafka.core.KafkaTemplate<String, String>
public class MyKafkaPublisher {
#Autowired
private KafkaTemplate<String, String> template;
#Value("${topic1}")
private String topic1;
#Value("${topic2}")
private String topic2;
public void sendMessages(Map<String, String> topicMsgMap){
topicMsgMap.forEach((top, msg) -> {
if (topic.equals("topic1") kafkaTemplate.send(topic1, message);
if (topic.equals("topic2") kafkaTemplate.send(topic2, message);
});
}
}
Guessing this isn't the use-case you had in mind, but hope you find this general structure useful.
There is several approaches including KafkaReplyingRestTemplate for this problem but continuing your approach in servlet api's the solution will be something like this in spring Webflux.
Your Controller method looks like this:
#RequestMapping(path = "/completable-future", method = RequestMethod.POST)
Mono<Response> asyncTransaction(#RequestBody RequestDto requestDto, #RequestHeader Map<String, String> requestHeaders) {
String internalTransactionId = UUID.randomUUID().toString();
kafkaSender.send(Request.builder()
.transactionId(requestHeaders.get("transactionId"))
.internalTransactionId(internalTransactionId)
.sourceIban(requestDto.getSourceIban())
.destIban(requestDto.getDestIban())
.build());
CompletableFuture<Response> completableFuture = new CompletableFuture();
taskHolder.pushTask(completableFuture, internalTransactionId);
return Mono.fromFuture(completableFuture);
}
Your taskHolder component will be something like this:
#Component
public class TaskHolder {
private Map<String, CompletableFuture> taskHolder = new ConcurrentHashMap();
public void pushTask(CompletableFuture<Response> task, String transactionId) {
this.taskHolder.put(transactionId, task);
}
public Optional<CompletableFuture> remove(String transactionId) {
return Optional.ofNullable(this.taskHolder.remove(transactionId));
}
}
And finally your Kafka ResponseListener looks like this:
#Component
public class ResponseListener {
#Autowired
TaskHolder taskHolder;
#KafkaListener(topics = "reactive-response-topic", groupId = "test")
public void listen(Response response) {
taskHolder.remove(response.getInternalTransactionId()).orElse(
new CompletableFuture()).complete(response);
}
}
In this example I used internalTransactionId as CorrelationId but you can use "kafka_correlationId" that is a known kafka header.

Resources