0

Hi have implemented producer and consumer in my existing spring boot application. Producer sending message properly to topic and even consumer also listening consuming message from topic. Here producer used with StreamBridge , it called from rest endpoint from the controller.

In the consumer afte it taking message i am making a internal service call it has httpclient realted and RequestContextHolder related calls.

my consumer looks like below

Consumer<Message<String>> input(){
return str - > {
log(str);
validateservice.validatData(str);
};}

my validateData methods looks as below

validateData ()
{
.....
RequestContextHolder.currentRequestAttributes().setAttribute("text", document, RequestAttributes.SCOPE_REQUEST);
....
}

at the above line RequestContextHolder.currentRequestAttributes() ... i am getting below error

14:28:44.747 [KafkaConsumerDestination{consumerDestinationName='xyz', partitions=0, dlqName='null'}.container-0-C-1] ERROR c.c.v.f.d.s.ValidateData - Error preparing external data
java.lang.IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message, your code is probably running outside of DispatcherServlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request.
    at org.springframework.web.context.request.RequestContextHolder.currentRequestAttributes(RequestContextHolder.java:131)
    at com.dfd.vsg.fnc.utils.CustomJAXBConvert.readFromSource(CustomJAXBConvert.java:174)

I have gone through How to enable request scope in async task executor and tried all the solutions, its not working.

Updated Question :

actually validatData is used in controller only, as there are few calls want make few async, so in that process first call will send message to topic and wont look for response. and the message will by consumer and send it to the validatData() method

xxz
  • 11
  • 4
  • There is no HTTP request context in the Kafka consumer. That `RequestContextHolder` is only usable from the MVC controller on the HTTP server side. Please, elaborate why you use it from that `validateData()` ? – Artem Bilan Jun 30 '22 at 20:01
  • @ArtemBilan updated the question, please check – xxz Jun 30 '22 at 20:19
  • i tried this way https://stackoverflow.com/questions/23732089/how-to-enable-request-scope-in-async-task-executor/33337838#33337838. but it not worked throw-ed same error – xxz Jun 30 '22 at 20:21
  • 1
    There is a Kafka broker in between producer via `StreamBridge` and that your `Consumer`. Your solution is not just async - it is *distributed*. Therefore there is indeed no request context on the other side. It is just not propagated over the network. – Artem Bilan Jun 30 '22 at 20:22
  • That one is different. There is an access to an executor to propagate context. In your case there is a network and just `byte[]` as data traveling to the Kafka broker and back to the consumer. – Artem Bilan Jun 30 '22 at 20:24
  • yeah we cannt propagated over network, is this wont work for my scenario. https://stackoverflow.com/questions/23732089/how-to-enable-request-scope-in-async-task-executor/33337838#33337838. – xxz Jun 30 '22 at 20:25
  • can i hold the context within the application and used it when consumer read the topic?. i am thinking using the executor and callable we can achieve but its not working for me. I clearly followed the above links suggestions which i pointed. it should working right – xxz Jun 30 '22 at 20:32
  • 1
    That is not going to work for you since the task is scheduled from the Kafka client, not from your HTTP request controller – Artem Bilan Jun 30 '22 at 20:36

0 Answers0