[SIS-14] adding the on message in its own thread

This commit is contained in:
Sheldan
2022-12-10 19:00:21 +01:00
parent 7e3b23aec0
commit 127ff821d1

View File

@@ -11,6 +11,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import static dev.sheldan.sissi.module.debra.config.DebraFeatureConfig.DEBRA_DONATION_NOTIFICATION_DELAY_CONFIG_KEY; import static dev.sheldan.sissi.module.debra.config.DebraFeatureConfig.DEBRA_DONATION_NOTIFICATION_DELAY_CONFIG_KEY;
@@ -40,23 +41,25 @@ public class WebsocketListener extends WebSocketListener implements AsyncStartup
@Override @Override
public void onMessage(WebSocket webSocket, String text) { public void onMessage(WebSocket webSocket, String text) {
log.info("Handling received message on websocket."); CompletableFuture.runAsync(() -> {
try { log.info("Handling received message on websocket.");
Long targetServerId = Long.parseLong(System.getenv(DEBRA_DONATION_NOTIFICATION_SERVER_ID_ENV_NAME)); try {
Long delayMillis = configService.getLongValueOrConfigDefault(DEBRA_DONATION_NOTIFICATION_DELAY_CONFIG_KEY, targetServerId); Long targetServerId = Long.parseLong(System.getenv(DEBRA_DONATION_NOTIFICATION_SERVER_ID_ENV_NAME));
log.info("Waiting {} milli seconds to send notification.", delayMillis); Long delayMillis = configService.getLongValueOrConfigDefault(DEBRA_DONATION_NOTIFICATION_DELAY_CONFIG_KEY, targetServerId);
Thread.sleep(delayMillis); log.info("Waiting {} milli seconds to send notification.", delayMillis);
log.info("Loading new donation amount and sending notification."); Thread.sleep(delayMillis);
Donation donation = donationService.parseDonationFromMessage(text); log.info("Loading new donation amount and sending notification.");
donationService.sendDonationNotification(donation).thenAccept(unused -> { Donation donation = donationService.parseDonationFromMessage(text);
log.info("Successfully notified about donation."); donationService.sendDonationNotification(donation).thenAccept(unused -> {
}).exceptionally(throwable -> { log.info("Successfully notified about donation.");
log.error("Failed to notify about donation.", throwable); }).exceptionally(throwable -> {
return null; log.error("Failed to notify about donation.", throwable);
}); return null;
} catch (Exception exception) { });
log.error("Failed to handle websocket message.", exception); } catch (Exception exception) {
} log.error("Failed to handle websocket message.", exception);
}
});
} }
@Override @Override