Threshold Based Flushable Executor

Notes on `ThresholdBasedFlushableExecutor`, batched logging, and a small Spring MVC request dispatch discussion around user space switching.

Notes on ThresholdBasedFlushableExecutor, batched logging, and a small Spring MVC request dispatch discussion around user space switching.

ThresholdBasedFlushableExecutor

ThresholdBasedFlushableExecutor

For batched logging:

  • incoming streams of data
  • you want to flush, store in DB or execute some operation on it, after some time is reached or after some size has been accumulated
  • so you can set the size and time threshold and ask the implementor to implement the flush strategy
  • for example flusher.flush(elementsToFlush)

For time-based scheduling we are using ScheduledExecutorService.

UserSpace Switch

In UserController there were 2 endpoints:

@Produces(MediaType.APPLICATION_JSON)
@GetMapping(path = "/config")
public EnvironmentConfigDTO config() {
    return envService.getCurrentEnvConfig();
}

@Produces(MediaType.APPLICATION_JSON)
@GetMapping(path = "/switch-config")
public EnvironmentConfigDTO switchConfig(HttpServletRequest request, HttpServletResponse response) {
    return envService.getCurrentEnvConfig();
}

Can we take HttpServletRequest and HttpServletResponse as arguments? How does DispatcherServlet redirect requests to these endpoints?

Yes, you can take HttpServletRequest and HttpServletResponse as arguments in Spring Boot controller methods. Spring automatically injects these objects when they are declared as method parameters.

How Does DispatcherServlet Work?

Spring MVC's DispatcherServlet is the central component responsible for handling HTTP requests and directing them to the appropriate controller methods.

  1. Interception of Requests
    • DispatcherServlet is configured as the front controller in Spring MVC
    • It intercepts all incoming requests, based on the URL mapping defined in Spring Boot or earlier web.xml style config
  2. Mapping to Controllers
    • DispatcherServlet uses HandlerMapping to determine which controller method should handle the request
    • It looks for @RequestMapping, @GetMapping, @PostMapping, and so on
  3. Invoking the Controller Method
    • If the method signature includes HttpServletRequest and HttpServletResponse, Spring injects them automatically
    • It creates an instance of EnvironmentConfigDTO and serializes it as JSON
  4. Sending the Response
    • The response is automatically written back to the HTTP response body

Do We Need HttpServletRequest and HttpServletResponse?

In most cases, Spring abstracts away the need to use HttpServletRequest and HttpServletResponse directly. However, you might use them when:

  • you need to read request headers, cookies, or parameters manually
  • you want to manipulate the response, for example setting cookies, modifying headers, or sending a redirect
@Produces(MediaType.APPLICATION_JSON)  // Not needed in Spring Boot
@GetMapping(path = "/config")
public EnvironmentConfigDTO config() {
    return envService.getCurrentEnvConfig();
}

Actually, in Spring Boot, @Produces(MediaType.APPLICATION_JSON) is not needed for JSON serialization. Unlike JAX-RS, where @Produces is used, Spring Boot automatically serializes objects returned from @RestController methods using Jackson, or another configured HttpMessageConverter.

What happens:

  • Calls envService.getCurrentEnvConfig() to get an instance of EnvironmentConfigDTO
  • Uses Jackson, default JSON serializer, to convert the object to a JSON response
  • Returns the JSON response with Content-Type: application/json

It automatically detects the return type and applies the appropriate HttpMessageConverter, such as:

  • MappingJackson2HttpMessageConverter for JSON
  • StringHttpMessageConverter for plain text
User
 - userSpaceDetails : Set
   - spaceId : String
   - roleIds : List<String>

So while creating a user we can give him access to certain spaces and what role in that space.

Workflow

  • on space switch -> spaceGroupId