polarnyy

Untitled

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