Edit in GitHubLog an issue

Asynchronous Requests

Description#

One benefit of server-side integration is that you can leverage the huge bandwidth and computing resources available on the server-side by using parallelism. Target Java SDK supports asynchronous requests, which can reduce the effective target time to zero.

Supported Methods#

Copied to your clipboard
1CompletableFuture<TargetDeliveryResponse> getOffersAsync(TargetDeliveryRequest request);
2CompletableFuture<ResponseStatus> sendNotificationsAsync(TargetDeliveryRequest request);
3CompletableFuture<Attributes> getAttributesAsync(TargetDeliveryRequest targetRequest, String ...mboxes);

Example#

A sample Spring application Controller could look like this:

Copied to your clipboard
1@RestController
2public class TargetRestController {
3
4 @Autowired
5 private TargetClient targetJavaClient;
6
7 @GetMapping("/mboxTargetOnlyAsync")
8 public TargetDeliveryResponse mboxTargetOnlyAsync(
9 @RequestParam(name = "mbox", defaultValue = "server-side-mbox") String mbox,
10 HttpServletRequest request, HttpServletResponse response) {
11 ExecuteRequest executeRequest = new ExecuteRequest()
12 .mboxes(getMboxRequests(mbox));
13
14 TargetDeliveryRequest targetDeliveryRequest = TargetDeliveryRequest.builder()
15 .context(getContext(request))
16 .execute(executeRequest)
17 .cookies(getTargetCookies(request.getCookies()))
18 .build();
19 CompletableFuture<TargetDeliveryResponse> targetResponseAsync =
20 targetJavaClient.getOffersAsync(targetDeliveryRequest);
21 targetResponseAsync.thenAccept(tr -> setCookies(tr.getCookies(), response));
22 simulateIO();
23 TargetDeliveryResponse targetResponse = targetResponseAsync.join();
24 return targetResponse;
25 }
26
27 /**
28 * Function for simulating network calls like other microservices and database calls
29 */
30 private void simulateIO() {
31 try {
32 Thread.sleep(200L);
33 } catch (InterruptedException e) {
34 e.printStackTrace();
35 }
36 }
37
38}

This example assumes you have initialized the SDK as a spring bean and that you have utility methods available.

The Target request is fired before simulateIO and by the time it is executed target result should also be ready. Even if it is not, you will have significant savings in most cases.

Was this helpful?
  • Privacy
  • Terms of Use
  • Do not sell my personal information
  • AdChoices
Copyright © 2022 Adobe. All rights reserved.