Skip to content

Commit

Permalink
- wrapped client in another callable and passed attributes of curren…
Browse files Browse the repository at this point in the history
…t request context to child threads.
  • Loading branch information
bilak committed Sep 12, 2016
1 parent 33df973 commit 8082c3c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.springframework.security.task.DelegatingSecurityContextAsyncTaskExecutor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;

import java.security.Principal;
import java.util.Arrays;
Expand Down Expand Up @@ -49,12 +50,30 @@ public ResponseEntity<List<String>> callInCurrentThread(Principal principal) {
@GetMapping("/entries/another-thread")
public ResponseEntity<List<String>> callInAnotherThread(Principal principal) throws ExecutionException, InterruptedException {
logger.debug("Principal from rest {}", principal);
Future<List<String>> result = executor.submit(new SampleServiceClientCaller(sampleServiceClient));
Future<List<String>> result = executor.submit(
new DelegatingRequestAttributesSampleServiceClientCaller(new SampleServiceClientCaller(sampleServiceClient))
);
return Optional.ofNullable(result.get())
.map(r -> ResponseEntity.ok(r))
.map(r -> ResponseEntity.ok(r))
.orElse(new ResponseEntity<List<String>>(HttpStatus.NOT_FOUND));
}

public static class DelegatingRequestAttributesSampleServiceClientCaller<V> implements Callable<V> {

private Callable<V> delegate;

public DelegatingRequestAttributesSampleServiceClientCaller(Callable<V> delegate) {
RequestContextHolder.setRequestAttributes(RequestContextHolder.currentRequestAttributes(), true);
this.delegate = delegate;
}

@Override
public V call() throws Exception {

return delegate.call();
}
}

public static class SampleServiceClientCaller implements Callable<List<String>> {

private SampleServiceClient sampleServiceClient;
Expand Down
2 changes: 1 addition & 1 deletion api-service/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ security:

feign:
hystrix:
enabled: false
enabled: true

logging:
level:
Expand Down

0 comments on commit 8082c3c

Please sign in to comment.