polarnyy

Untitled

Jul 29th, 2025
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.18 KB | None | 0 0
  1. public class RequestUtility {
  2.  
  3.     public static <REQ extends NatsPacket, RES extends NatsPacket> CompletableFuture<RES> sendRequest(
  4.             String channel, REQ packetRequest, int duration, Class<RES> responseType) {
  5.         CompletableFuture<RES> completableFuture = new CompletableFuture<>();
  6.  
  7.         API.getInstance().getConcurrencyService().executeNats(() -> {
  8.             try {
  9.                 API.getInstance().getNatsMessengerAPI().sendCallbackPacket(
  10.                         channel,
  11.                         packetRequest,
  12.                         duration,
  13.                         new NatsCallback<RES>() {
  14.                             @Override
  15.                             public void onHandle(RES response) {
  16.                                 completableFuture.complete(response);
  17.                             }
  18.  
  19.                             @Override
  20.                             public void exit() {
  21.                                 completableFuture.completeExceptionally(new Exception("Failed to get a response for the packet on channel: " + channel));
  22.                             }
  23.                         }
  24.                 );
  25.             } catch (Exception e) {
  26.                 completableFuture.completeExceptionally(e);
  27.             }
  28.         });
  29.  
  30.         return completableFuture;
  31.     }
  32.  
  33.     public static <REQ extends NatsPacket, RES extends NatsPacket> CompletableFuture<RES> sendRequests(
  34.             String primaryChannel, String fallbackChannel, REQ packetRequest, int duration, Class<RES> responseType) {
  35.         CompletableFuture<RES> completableFuture = new CompletableFuture<>();
  36.  
  37.         API.getInstance().getConcurrencyService().executeNats(() -> {
  38.             try {
  39.                 API.getInstance().getNatsMessengerAPI().sendCallbackPacket(
  40.                         primaryChannel,
  41.                         packetRequest,
  42.                         duration,
  43.                         new NatsCallback<RES>() {
  44.                             @Override
  45.                             public void onHandle(RES response) {
  46.                                 completableFuture.complete(response);
  47.                             }
  48.  
  49.                             @Override
  50.                             public void exit() {
  51.                                 // If the primary channel fails, try the fallback channel
  52.                                 API.getInstance().getConcurrencyService().executeNats(() -> {
  53.                                     try {
  54.                                         API.getInstance().getNatsMessengerAPI().sendCallbackPacket(
  55.                                                 fallbackChannel,
  56.                                                 packetRequest,
  57.                                                 duration,
  58.                                                 new NatsCallback<RES>() {
  59.                                                     @Override
  60.                                                     public void onHandle(RES response) {
  61.                                                         completableFuture.complete(response);
  62.                                                     }
  63.  
  64.                                                     @Override
  65.                                                     public void exit() {
  66.                                                         completableFuture.completeExceptionally(
  67.                                                                 new Exception("Failed to get a response from both channels: "
  68.                                                                         + primaryChannel + " and " + fallbackChannel));
  69.                                                     }
  70.                                                 }
  71.                                         );
  72.                                     } catch (Exception e) {
  73.                                         completableFuture.completeExceptionally(e);
  74.                                     }
  75.                                 });
  76.                             }
  77.                         }
  78.                 );
  79.             } catch (Exception e) {
  80.                 completableFuture.completeExceptionally(e);
  81.             }
  82.         });
  83.  
  84.         return completableFuture;
  85.     }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment