Add closeable webhook

This commit is contained in:
Andrea Cavalli 2024-03-02 21:27:50 +01:00
parent 4b4364f32d
commit dee9941a1f
1 changed files with 18 additions and 1 deletions

View File

@ -1,5 +1,8 @@
package org.telegram.telegrambots.updatesreceivers;
import java.io.Closeable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import org.glassfish.grizzly.http.server.HttpServer;
import org.glassfish.grizzly.ssl.SSLContextConfigurator;
import org.glassfish.grizzly.ssl.SSLEngineConfigurator;
@ -19,12 +22,13 @@ import java.net.URI;
* @version 1.0
* Webhook to receive updates
*/
public class DefaultWebhook implements Webhook {
public class DefaultWebhook implements Webhook, Closeable {
private String keystoreServerFile;
private String keystoreServerPwd;
private String internalUrl;
private final RestApi restApi;
private HttpServer grizzlyServer;
public DefaultWebhook() {
this.restApi = new RestApi();
@ -67,6 +71,7 @@ public class DefaultWebhook implements Webhook {
}
try {
this.grizzlyServer = grizzlyServer;
grizzlyServer.start();
} catch (IOException e) {
throw new TelegramApiException("Error starting webhook server", e);
@ -83,4 +88,16 @@ public class DefaultWebhook implements Webhook {
throw new TelegramApiException("Can't find or access server keystore file.");
}
}
@Override
public void close() throws IOException {
try {
grizzlyServer.shutdown(1, TimeUnit.MINUTES).get();
} catch (ExecutionException ex) {
throw new IOException("Failed to stop grizzly server", ex.getCause());
} catch (InterruptedException e) {
// timeout
}
grizzlyServer.shutdownNow();
}
}