remove shutdownhook
This commit is contained in:
parent
112722d1c5
commit
1de6e05cfa
|
@ -19,7 +19,7 @@ public class AsyncHandler implements Runnable {
|
||||||
public void enque(Task t) {
|
public void enque(Task t) {
|
||||||
enque(t, false);
|
enque(t, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void enque(Task t, boolean priority) {
|
public void enque(Task t, boolean priority) {
|
||||||
if(priority) {
|
if(priority) {
|
||||||
synchronized (tasks) {
|
synchronized (tasks) {
|
||||||
|
@ -49,19 +49,19 @@ public class AsyncHandler implements Runnable {
|
||||||
int failed = 0;
|
int failed = 0;
|
||||||
while(!tasks.isEmpty()) {
|
while(!tasks.isEmpty()) {
|
||||||
Task current;
|
Task current;
|
||||||
|
|
||||||
synchronized (tasks) {
|
synchronized (tasks) {
|
||||||
current = tasks.remove(0);
|
current = tasks.remove(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
if(current == null)
|
if(current == null)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
//run task
|
//run task
|
||||||
try {
|
try {
|
||||||
try {
|
try {
|
||||||
Object obj = api.request(current.apimethod, current.parameters);
|
Object obj = api.request(current.apimethod, current.parameters);
|
||||||
Callback<?, ?> callb = current.callback;
|
Callback<?, ?> callb = current.callback;
|
||||||
while(callb != null) {
|
while(callb != null) {
|
||||||
obj = (Object) callb.callObj(obj);
|
obj = (Object) callb.callObj(obj);
|
||||||
callb = callb.next;
|
callb = callb.next;
|
||||||
|
@ -72,7 +72,6 @@ public class AsyncHandler implements Runnable {
|
||||||
failed ++;
|
failed ++;
|
||||||
if(failed > 10)
|
if(failed > 10)
|
||||||
try {
|
try {
|
||||||
Thread.yield();
|
|
||||||
Thread.sleep(1000);//wait 1 second
|
Thread.sleep(1000);//wait 1 second
|
||||||
} catch(InterruptedException ignored) {}
|
} catch(InterruptedException ignored) {}
|
||||||
//reenque
|
//reenque
|
||||||
|
@ -95,18 +94,18 @@ public class AsyncHandler implements Runnable {
|
||||||
String parameters;
|
String parameters;
|
||||||
Callback<JSONObject, ?> callback = null;
|
Callback<JSONObject, ?> callback = null;
|
||||||
Callback<Throwable, ?> exceptionhandl = null;
|
Callback<Throwable, ?> exceptionhandl = null;
|
||||||
|
|
||||||
public Task(String apimethod, String parameters) {
|
public Task(String apimethod, String parameters) {
|
||||||
this.apimethod = apimethod;
|
this.apimethod = apimethod;
|
||||||
this.parameters = parameters;
|
this.parameters = parameters;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Task(String apimethod, String parameters, Callback<JSONObject, ?> callback) {
|
public Task(String apimethod, String parameters, Callback<JSONObject, ?> callback) {
|
||||||
this.apimethod = apimethod;
|
this.apimethod = apimethod;
|
||||||
this.parameters = parameters;
|
this.parameters = parameters;
|
||||||
this.callback = callback;
|
this.callback = callback;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Task setExceptionhandl(Callback<Throwable, ?> exceptionhandl) {
|
public Task setExceptionhandl(Callback<Throwable, ?> exceptionhandl) {
|
||||||
this.exceptionhandl = exceptionhandl;
|
this.exceptionhandl = exceptionhandl;
|
||||||
return this;
|
return this;
|
||||||
|
|
|
@ -112,9 +112,6 @@ public class TelegramAPI implements Runnable {
|
||||||
} else {
|
} else {
|
||||||
throw new IllegalStateException("Still Running.");
|
throw new IllegalStateException("Still Running.");
|
||||||
}
|
}
|
||||||
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
|
|
||||||
stop();
|
|
||||||
}, "TelegramAPI Shutdown Hook"));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addAdmin(long admin) {
|
public void addAdmin(long admin) {
|
||||||
|
@ -128,7 +125,7 @@ public class TelegramAPI implements Runnable {
|
||||||
public void requestAsync(String request, String parameter) {
|
public void requestAsync(String request, String parameter) {
|
||||||
this.async.enque(request, parameter);
|
this.async.enque(request, parameter);
|
||||||
}
|
}
|
||||||
|
|
||||||
public JSONObject request(String request, String parameter) throws IOException {
|
public JSONObject request(String request, String parameter) throws IOException {
|
||||||
return request(request, parameter, true);
|
return request(request, parameter, true);
|
||||||
}
|
}
|
||||||
|
@ -141,7 +138,7 @@ public class TelegramAPI implements Runnable {
|
||||||
con.setDoOutput(true);
|
con.setDoOutput(true);
|
||||||
OutputStreamWriter wr = new OutputStreamWriter(con.getOutputStream());
|
OutputStreamWriter wr = new OutputStreamWriter(con.getOutputStream());
|
||||||
wr.write(parameter);
|
wr.write(parameter);
|
||||||
wr.flush();
|
wr.flush();
|
||||||
|
|
||||||
if(logging) {
|
if(logging) {
|
||||||
String small = parameter;
|
String small = parameter;
|
||||||
|
@ -175,7 +172,7 @@ public class TelegramAPI implements Runnable {
|
||||||
s.close();
|
s.close();
|
||||||
return sb.toString();
|
return sb.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void sendAsync(long chatid, String msg) {
|
public void sendAsync(long chatid, String msg) {
|
||||||
sendMessage(new MessageBuilder().setAsync().setReciver(chatid).setText(msg).build());
|
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());
|
sendMessage(new MessageBuilder().setText(msg).setReciver(user.getID()).build());
|
||||||
|
|
||||||
}
|
}
|
||||||
}).start();
|
}).start();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void answerCallbackQuery(String callbackid, String text, boolean async) {
|
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) {
|
public void updateMarkup(long chatid, int msg_id, TReplyMarkup rm, boolean async) {
|
||||||
try {
|
try {
|
||||||
if(rm == null) return;//nope
|
if(rm == null) return;//nope
|
||||||
|
|
||||||
String q = "chat_id=" + chatid + "&message_id=" + msg_id + "&reply_markup=" + URLEncoder.encode(rm.toJSONString(), "UTF-8");
|
String q = "chat_id=" + chatid + "&message_id=" + msg_id + "&reply_markup=" + URLEncoder.encode(rm.toJSONString(), "UTF-8");
|
||||||
|
|
||||||
if(async) {
|
if(async) {
|
||||||
this.async.enque("editMessageReplyMarkup", q);
|
this.async.enque("editMessageReplyMarkup", q);
|
||||||
} else {
|
} else {
|
||||||
|
@ -290,22 +287,13 @@ public class TelegramAPI implements Runnable {
|
||||||
log.log("", e);
|
log.log("", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void stop() {
|
public void stop() {
|
||||||
run = false;
|
run = false;
|
||||||
log.log("tapi stoped.");
|
|
||||||
if(thread == null) return;
|
if(thread == null) return;
|
||||||
|
|
||||||
thread.interrupt();
|
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;
|
thread = null;
|
||||||
|
log.log("TelegramAPI stoped.");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -417,7 +405,7 @@ public class TelegramAPI implements Runnable {
|
||||||
public EventManager getEventManager() {
|
public EventManager getEventManager() {
|
||||||
return evntmgr;
|
return evntmgr;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* seconds to wait for longpolling or milliseconds to wait petween shortpolling
|
* seconds to wait for longpolling or milliseconds to wait petween shortpolling
|
||||||
* @param d
|
* @param d
|
||||||
|
@ -445,7 +433,7 @@ public class TelegramAPI implements Runnable {
|
||||||
public static boolean isSendable(long filesize) {
|
public static boolean isSendable(long filesize) {
|
||||||
return filesize < TELEGRAMFILESIZELIMIT;
|
return filesize < TELEGRAMFILESIZELIMIT;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Callback<JSONObject, TMessage> getCallbackAdapter(TelegramAPI api) {
|
public static Callback<JSONObject, TMessage> getCallbackAdapter(TelegramAPI api) {
|
||||||
return new Callback<JSONObject, TMessage>() {
|
return new Callback<JSONObject, TMessage>() {
|
||||||
@Override
|
@Override
|
||||||
|
@ -574,7 +562,7 @@ public class TelegramAPI implements Runnable {
|
||||||
|
|
||||||
public class APIError extends IOException {
|
public class APIError extends IOException {
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
String params;
|
String params;
|
||||||
|
|
Loading…
Reference in New Issue