我有一个机器人,该机器人首先创建消息数组,然后将这些消息发送到通道。该程序使用ScheduledExecutorService
,有两个Runnable
和两个线程:第一个线程填充数组,第二个线程在数组不为空时发送请求。请求后,将从阵列中删除一条消息。该消息同时包含文本和gif:
public class BotTest {
private static final ScheduledExecutorService scheduler =
Executors.newScheduledThreadPool(2);
public static void main(String[] args) throws Exception {
final MyBot bot = new MyBot();
ArrayList<Message> messages = new ArrayList<>();
Runnable createMessages =
new Runnable(){
public void run(){
//...creating messages
}
};
scheduler.scheduleAtFixedRate(createMessages, 0, 1, TimeUnit.MINUTES);
scheduler.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
//if (messages.size()<=0) {
while (messages.size()<=0) {
System.out.println("Array is empty");
}
//else {
try {
final Message message = messages.get(0);
messages.remove(0);
bot.sendMessage(message);
}
catch (Throwable e) {
e.printStackTrace();
}
//}
}
}, 0, 1, TimeUnit.SECONDS);
}
}
sendMessage
:
public int sendMessage(Message message) throws Exception {
boolean result = false;
String linkText = rootLink + token + "/sendMessage?chat_id=@MyChat&text=" + message.getText();
String linkGif = rootLink + token + "/sendAnimation?chat_id=@MyChat&animation=" + URLEncoder.encode(message.getUrl(), "UTF-8");
int responseCode1 = new TelegramRequest(linkText).send().getCode();
int responseCode2 = new TelegramRequest(linkGif).send().getCode();
//...logging in text file
return responseCode1 + responseCode2;
}
即使自终止进程很久(几小时前)以来,我的计算机(Windows 7,IDE Intellij)仍继续发送消息。如果我关闭计算机上的Internet连接,则消息将停止发送-直到我再次将其打开。发生了什么,我该如何停止?
我还在进行登录:对于每条消息,在文本文件中进行输入。但是,没有为这些连续请求创建任何条目。这让我认为正在进行一些缓存,并且这些请求以某种方式被放入了队列,直到现在才被发送...