concept call in category spring

This is an excerpt from Manning's book Spring Microservices in Action, 2E.
Credential management and propagation. How do you prevent a service client from constantly having to present their credentials for service calls involved in a transaction? To achieve this in this book, we’ll look at how token-based security standards such as OAuth2 and JSON Web Tokens (JWT) can be used to obtain a token that can be passed from service call to service call to authenticate and authorize the user.
Calls to remote services are usually synchronous and don’t cut short a long-running call. The caller of a service has no concept of a timeout to keep the service call from hanging out forever. The application developer calls the service to perform an action and waits for the service to return.
In figure 7.3, the licensing service is never going to directly invoke the organization service. Instead, when the call is made, licensing service is going to delegate the actual invocation of the service to the circuit breaker, which will take the call and wrap it in a thread (usually managed by a thread pool) that’s independent of the originating caller. By wrapping the call in a thread, the client is no longer directly waiting for the call to complete. Instead, the circuit breaker is monitoring the thread and can kill the call if the thread runs too long.
Figure 7.3 The circuit breaker trips and allows a misbehaving service call to fail quickly and gracefully.
![]()
Three scenarios are shown in figure 7.3. In the first scenario, the happy path, the circuit breaker will maintain a timer, and if the call to the remote service completes before the timer runs out, everything is good, and the licensing service can continue its work. In the partial degradation scenario two, the licensing service will call the organization service through the circuit breaker. This time, though, the organization service is running slow, and the circuit breaker will kill the connection out to the remote service if it doesn’t complete before the timer on the thread maintained by the circuit breaker times out.
The licensing service will then get an error from making the call, but the licensing service won’t have resources (that is, its own thread or connection pools) tied up waiting for the organization service to complete. If the call to the organization service is timed-out by the circuit breaker, the circuit breaker will start tracking the number of failures that have occurred.
Listing 7.3 Randomly timing out a call to the licensing service database
Rest of LicenseService.java removed for conciseness private void randomlyRunLong(){ #A Random rand = new Random(); int randomNum = rand.nextInt((3 - 1) + 1) + 1; if (randomNum==3) sleep(); } private void sleep(){ try { Thread.sleep(5000); #B throw new java.util.concurrent.TimeoutException(); } catch (InterruptedException e) { logger.error(e.getMessage()); } } @CircuitBreaker(name = "licenseService") public List<License> getLicensesByOrganization(String organizationId) { randomlyRunLong(); return licenseRepository.findByOrganizationId(organizationId); }

This is an excerpt from Manning's book Spring Security in Action.
Other responsibilities of these components also relate to the data storing as well as transiting data between different parts of the systems. By intercepting the calls to these different parts, the components can act on the data. When the data is stored, these components may apply encryption or hashing algorithms. The data encodings keep the data accessible only to privileged entities. In the Spring application, the developer has to add and configure a component to do this part of the job wherever it’s needed. Spring Security provides us with a contract through which we know what the framework requires to be implemented, and we write the implementation according to the design of the application. We can say the same thing about transiting data.
How does Global Method Security work? What’s the mechanism behind applying the authorization rules? When we enable Global Method Security in our application, we actually enable a Spring aspect. This aspect intercepts the calls to the method for which we apply authorization rules, and, based on these authorization rules, decides whether to forward the call to the intercepted method (figure 16.2).
Figure 16.2 When we enable Global Method Security, an aspect intercepts the calls to the protected methods. If the given authorization rules aren’t respected, the aspect won’t delegate the call to the protected method.
![]()
Listing 20.19 Using a WebTestClientConfigurer to define a mock user for the call
@SpringBootTest @AutoConfigureWebTestClient class MainTests { @Autowired private WebTestClient client; // Omitted code @Test void testCallHelloWithValidUserWithMockUser() { client.mutateWith(mockUser()) #A .get() .uri("/hello") .exchange() .expectStatus().isOk(); } }

This is an excerpt from Manning's book Spring Microservices in Action.
The @HystrixCommand annotation is doing two things. First, any time the helloRemoteServiceCall method is called, it won’t be directly invoked. Instead, the method will be delegated to a thread pool managed by Hystrix. If the call takes too long (default is one second), Hystrix steps in and interrupts the call. This is the implementation of the circuit breaker pattern. The second thing this annotation does is create a thread pool called helloThreadPool that’s managed by Hystrix. All calls to helloRemoteServiceCall method will only occur on this thread pool and will be isolated from any other remote service calls being made.
Calls to remote services are usually synchronous and don’t cut short a long-running call— The caller of a service has no concept of a timeout to keep the service call from hanging out forever. The application developer calls the service to perform an action and waits for the service to return.
In figure 5.3, the Service B (the client) is never going to directly invoke Service C. Instead, when the call is made, Service B is going to delegate the actual invocation of the service to the circuit breaker, which will take the call and wrap it in a thread (usually managed by a thread pool) that’s independent of the originating caller. By wrapping the call in a thread, the client is no longer directly waiting for the call to complete. Instead, the circuit breaker is monitoring the thread and can kill the call if the thread runs too long.
Figure 5.3. The circuit breaker trips and allows a misbehaving service call to fail quickly and gracefully.
![]()
Three scenarios are shown in figure 5.3. In the first scenario, the happy path, the circuit breaker will maintain a timer and if the call to the remote service completes before the timer runs out, everything is good and Service B can continue its work. In the partial degradation scenario, Service B will call Service C through the circuit breaker. This time, though, Service C is running slow and the circuit breaker will kill the connection out to the remote service if it doesn’t complete before the timer on the thread maintained by the circuit breaker times out.
Service B will then get an error from making the call, but Service B won’t have resources (that is, its own thread or connection pools) tied up waiting for Service C to complete. If the call to Service C is timed-out by the circuit breaker, the circuit breaker will start tracking the number of failures that have occurred.
This doesn't look like a lot of code, and it's not, but there is a lot of functionality inside this one annotation. With the use of the @HystrixCommand annotation, any time the getLicensesByOrg() method is called, the call will be wrapped with a Hystrix circuit breaker. The circuit breaker will interrupt any call to the getLicensesByOrg() method any time the call takes longer than 1,000 milliseconds.
This code example would be boring if the database is working properly. Let's simulate the getLicensesByOrg() method running into a slow database query by having the call take a little over a second on approximately every one in three calls. The following listing demonstrates this.
The beauty of using method-level annotations for tagging calls with circuit-breaker behavior is that it’s the same annotation whether you’re accessing a database or calling a microservice.