How do I "share" a request between threads?

Asked

Viewed 77 times

1

I have a "problem" in the system I’m working on, it’s a Java api.

Not long ago, they started deploying filters from the request parameters. For every request, it has a preHandle that filters this request and saves a request, so far so good. After that, it redirects to the mapped method that it should access, for example:

Clientaction

@PostMapping("/rota/{param1}/{param2}/{param3}")
public void process(@PathVariable String param1, @PathVariable String param2, @PathVariable String param3) {
    List<Clients> clients = clientService.getClients();

    processService.process();

    ...
}

With this, it executes a method that returns all clients, but in this method there is a request filter, if you have a request, it returns it to be used, otherwise it returns null:

Clientservice

public List<Clients> getClients() {
    HttpServletRequest request = SecurityService.getRequest();

    if (request != null) {
        .
        .
        .
        return clients;
    }
    return null;
}

Securityservice

public static HttpServletRequest getRequest() {
    HttpServletRequest request = null;
    RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
    if (requestAttributes != null && requestAttributes instanceof ServletRequestAttributes) {
        request = ((ServletRequestAttributes) requestAttributes).getRequest();
    }
    return request;
}

In this first part, it returns the list of all customers normally.

After that, he executes the processService.process(), as an example below:

Processservice

public void process() {
    List<Clients> clients = clientService.getClients();
}

Here is the "problem", in this method that is called, he can not get the list of customers, because the request blank ballot.

I tried to search, but I didn’t get a result, I found something that the request like this, it can’t be shared between the threads executed, so it cannot return the value.

I know that a solution, would run in the first method and pass the client list to the methods below, but I want to find something that I can "share" and execute in the methods called, because it is a large method, with various logics and different processes. Also, have the possibility to call this method to get clients from anywhere in the system, without having to execute in the method where the request arrives.

If this is the "problem", there is some way to "share" it between the threads?

If this is not the "problem", why does this occur ?

1 answer

0

A thread cannot access data from another thread because each thread has its own individual context.

There are several reasons for this to happen:

  1. Maintain isolation between threads while avoiding Leaking unnecessary information for threads that don’t need access to them
  2. Avoid possible memory Leaks, since in a distributed environment thousands of threads can be raised, and consequently if the context is propagated to all of them can lead to performance and load problems in the application
  3. Usually the thread goes back to a pool after its use, if the threads maintain state/context, when it is reused you may end up using incorrect information in the process execution

Anyway, when there is a need to pass on information throughout the execution of a thread, you can use a Threadlocal to save information and access it during running thread lifecycle.

The problem is that threads created by the running thread will not have access to the context of the parent thread, so you can use Inheritablethreadlocal to propagate the context of the parent thread also to the daughters, but remember, if thousands of threads share the context, and depending on what you put in this context, you may end up having problems.

Another care one should take is to always clean the ThreadLocal before/after each use, to prevent the thread from accessing outdated information by fetching it from the Holder.

It is important to note that this adds a great complexity to the system, since you will also have to control a cycle to maintain the information of the ThreadLocal.

There is even a very interesting discussion in this stackoverflow thread on the use of threads with Servlets.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.