remove shutdownhook

This commit is contained in:
Oliver 2020-06-15 00:28:37 +02:00
parent 112722d1c5
commit 1de6e05cfa
Signed by: okaestne
GPG Key ID: 06A81B143EA9588F
2 changed files with 19 additions and 32 deletions

View File

@ -19,7 +19,7 @@ public class AsyncHandler implements Runnable {
public void enque(Task t) {
enque(t, false);
}
public void enque(Task t, boolean priority) {
if(priority) {
synchronized (tasks) {
@ -49,19 +49,19 @@ public class AsyncHandler implements Runnable {
int failed = 0;
while(!tasks.isEmpty()) {
Task current;
synchronized (tasks) {
current = tasks.remove(0);
}
if(current == null)
continue;
//run task
try {
try {
Object obj = api.request(current.apimethod, current.parameters);
Callback<?, ?> callb = current.callback;
Callback<?, ?> callb = current.callback;
while(callb != null) {
obj = (Object) callb.callObj(obj);
callb = callb.next;
@ -72,7 +72,6 @@ public class AsyncHandler implements Runnable {
failed ++;
if(failed > 10)
try {
Thread.yield();
Thread.sleep(1000);//wait 1 second
} catch(InterruptedException ignored) {}
//reenque
@ -95,18 +94,18 @@ public class AsyncHandler implements Runnable {
String parameters;
Callback<JSONObject, ?> callback = null;
Callback<Throwable, ?> exceptionhandl = null;
public Task(String apimethod, String parameters) {
this.apimethod = apimethod;
this.parameters = parameters;
}
public Task(String apimethod, String parameters, Callback<JSONObject, ?> callback) {
this.apimethod = apimethod;
this.parameters = parameters;
this.callback = callback;
}
public Task setExceptionhandl(Callback<Throwable, ?> exceptionhandl) {
this.exceptionhandl = exceptionhandl;
return this;

View File

@ -112,9 +112,6 @@ public class TelegramAPI implements Runnable {
} else {
throw new IllegalStateException("Still Running.");
}
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
stop();
}, "TelegramAPI Shutdown Hook"));
}
public void addAdmin(long admin) {
@ -128,7 +125,7 @@ public class TelegramAPI implements Runnable {
public void requestAsync(String request, String parameter) {
this.async.enque(request, parameter);
}
public JSONObject request(String request, String parameter) throws IOException {
return request(request, parameter, true);
}
@ -141,7 +138,7 @@ public class TelegramAPI implements Runnable {
con.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter(con.getOutputStream());
wr.write(parameter);
wr.flush();
wr.flush();
if(logging) {
String small = parameter;
@ -175,7 +172,7 @@ public class TelegramAPI implements Runnable {
s.close();
return sb.toString();
}
public void sendAsync(long chatid, String msg) {
sendMessage(new MessageBuilder().setAsync().setReciver(chatid).setText(msg).build());
}
@ -218,7 +215,7 @@ public class TelegramAPI implements Runnable {
sendMessage(new MessageBuilder().setText(msg).setReciver(user.getID()).build());
}
}).start();
}).start();
}
public void answerCallbackQuery(String callbackid, String text, boolean async) {
@ -278,9 +275,9 @@ public class TelegramAPI implements Runnable {
public void updateMarkup(long chatid, int msg_id, TReplyMarkup rm, boolean async) {
try {
if(rm == null) return;//nope
String q = "chat_id=" + chatid + "&message_id=" + msg_id + "&reply_markup=" + URLEncoder.encode(rm.toJSONString(), "UTF-8");
if(async) {
this.async.enque("editMessageReplyMarkup", q);
} else {
@ -290,22 +287,13 @@ public class TelegramAPI implements Runnable {
log.log("", e);
}
}
public void stop() {
run = false;
log.log("tapi stoped.");
if(thread == null) return;
thread.interrupt();
Thread.yield();//try to not get into that loop
while(isRunning()) {
thread.interrupt();
Thread.yield();
try {
Thread.sleep(10);
} catch(InterruptedException e) {}
}
thread = null;
log.log("TelegramAPI stoped.");
}
@Override
@ -417,7 +405,7 @@ public class TelegramAPI implements Runnable {
public EventManager getEventManager() {
return evntmgr;
}
/**
* seconds to wait for longpolling or milliseconds to wait petween shortpolling
* @param d
@ -445,7 +433,7 @@ public class TelegramAPI implements Runnable {
public static boolean isSendable(long filesize) {
return filesize < TELEGRAMFILESIZELIMIT;
}
public static Callback<JSONObject, TMessage> getCallbackAdapter(TelegramAPI api) {
return new Callback<JSONObject, TMessage>() {
@Override
@ -574,7 +562,7 @@ public class TelegramAPI implements Runnable {
public class APIError extends IOException {
/**
*
*
*/
private static final long serialVersionUID = 1L;
String params;