Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package pl.ogmc.api.nats.api.packet;
- import java.util.concurrent.CompletableFuture;
- import pl.ogmc.api.API;
- import pl.ogmc.api.nats.api.callback.NatsCallback;
- /**
- * Author: polarnyy
- * Created: 29 July 2025, 18:02
- * Project: ogmc
- * Package: pl.ogmc.api.nats.api.packet
- */
- public class RequestUtility {
- public static <REQ extends NatsPacket, RES extends NatsPacket> CompletableFuture<RES> sendRequest(
- String channel, REQ packetRequest, int duration, Class<RES> responseType) {
- CompletableFuture<RES> completableFuture = new CompletableFuture<>();
- API.getInstance().getConcurrencyService().executeNats(() -> {
- try {
- API.getInstance().getNatsMessengerAPI().sendCallbackPacket(
- channel,
- packetRequest,
- duration,
- new NatsCallback<RES>() {
- @Override
- public void onHandle(RES response) {
- completableFuture.complete(response);
- }
- @Override
- public void exit() {
- completableFuture.completeExceptionally(new Exception("Failed to get a response for the packet on channel: " + channel));
- }
- }
- );
- } catch (Exception e) {
- completableFuture.completeExceptionally(e);
- }
- });
- return completableFuture;
- }
- public static <REQ extends NatsPacket, RES extends NatsPacket> CompletableFuture<RES> sendRequests(
- String primaryChannel, String fallbackChannel, REQ packetRequest, int duration, Class<RES> responseType) {
- CompletableFuture<RES> completableFuture = new CompletableFuture<>();
- API.getInstance().getConcurrencyService().executeNats(() -> {
- try {
- API.getInstance().getNatsMessengerAPI().sendCallbackPacket(
- primaryChannel,
- packetRequest,
- duration,
- new NatsCallback<RES>() {
- @Override
- public void onHandle(RES response) {
- completableFuture.complete(response);
- }
- @Override
- public void exit() {
- API.getInstance().getConcurrencyService().executeNats(() -> {
- try {
- API.getInstance().getNatsMessengerAPI().sendCallbackPacket(
- fallbackChannel,
- packetRequest,
- duration,
- new NatsCallback<RES>() {
- @Override
- public void onHandle(RES response) {
- completableFuture.complete(response);
- }
- @Override
- public void exit() {
- completableFuture.completeExceptionally(
- new Exception("Failed to get a response from both channels: "
- + primaryChannel + " and " + fallbackChannel));
- }
- }
- );
- } catch (Exception e) {
- completableFuture.completeExceptionally(e);
- }
- });
- }
- }
- );
- } catch (Exception e) {
- completableFuture.completeExceptionally(e);
- }
- });
- return completableFuture;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment