关闭ScheduledExecutorService

huangapple go评论60阅读模式
英文:

Shutting down a ScheduledExecutorService

问题

以下是您的代码的翻译部分:

ScheduledExecutorService exec = Executors.newScheduledThreadPool(0);
String[] args = event.getMessage().getContentRaw().split(" ");
if (args[0].equalsIgnoreCase("!futuresOn")) {
    Color red = new Color(255, 0, 0);
    Color green = new Color(0, 204, 0);
    Runnable futRun = new Runnable() {
        public void run() {
            EmbedBuilder futuresBot = new EmbedBuilder();
            futuresBot.setAuthor("期货机器人", null, event.getAuthor().getAvatarUrl());
   
            try {
                futuresBot.addField("**标普500**", getSPY(), false);
                futuresBot.addField("**纳斯达克**", getNASDAQ(), false);
                futuresBot.addField("**道琼斯**", getDOW(), false);
                if (getSPY().contains("+")) {
                    futuresBot.setColor(green);
                } 
                else {
                    futuresBot.setColor(red);
                }
            }
            catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
   
            futuresBot.setFooter("Cantina Capital", event.getGuild().getIconUrl());
            event.getChannel().sendMessage(futuresBot.build()).queue();
        }
    };
    
    exec.scheduleAtFixedRate(futRun, 0, 1, TimeUnit.HOURS);
}

if (args[0].equalsIgnoreCase("!futuresoff")) {
    event.getChannel().sendMessage("期货机器人已关闭。").queue();
    exec.shutdownNow();
}

请注意,代码中的注释和变量名没有翻译,保持原样。

英文:

So I have a discord bot where I am trying to send a message every hour. I got that part of it working using the scheduled executor service. It starts when I say !futureson

I want to be able to stop it when I send !futuresoff

However, I cannot get this to work properly. Here is my code:

    String[] args = event.getMessage().getContentRaw().split(" ");
if (args[0].equalsIgnoreCase("!futuresOn")) {
Color red = new Color(255, 0, 0);
Color green = new Color(0, 204, 0);
Runnable futRun = new Runnable() {
public void run() {
EmbedBuilder futuresBot = new EmbedBuilder();
futuresBot.setAuthor("Futures Bot", null, event.getAuthor().getAvatarUrl());
try {
futuresBot.addField("**S&P 500**", getSPY(), false);
futuresBot.addField("**NASDAQ**", getNASDAQ(), false);
futuresBot.addField("**DOW**", getDOW(), false);
if (getSPY().contains("+")) {
futuresBot.setColor(green);
} 
else {
futuresBot.setColor(red);
}
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
futuresBot.setFooter("Cantina Capital", event.getGuild().getIconUrl());
event.getChannel().sendMessage(futuresBot.build()).queue();
}
};
exec.scheduleAtFixedRate(futRun, 0, 1, TimeUnit.HOURS);
}
if (args[0].equalsIgnoreCase("!futuresoff")) {
event.getChannel().sendMessage("Futures bot off.").queue();
exec.shutdownNow();
}

I have tried this a few different ways, but I can't get it to work properly. It is getting to the !futuresoff part because it sends the Futures bot off message. I just want it to be able to send the message once an hour when the !futureson has been sent and not when !futuresoff is sent. I also tried
ScheduledThreadPoolExecutor exec = new ScheduledThreadPoolExecutor(0);

I would appreciate any help or someone that could point me in the right direction.

答案1

得分: 1

你应该将scheduleAtFixedRate()的结果保存在你的Listener类中。

ScheduledFuture scheduledFuture = null;

然后你可以使用这个对象的cancel()方法来停止它。在调度之前,你还应该测试它是否为null,并在取消之前测试它是否已设置:

String[] args = event.getMessage().getContentRaw().split(" ");
if (args[0].equalsIgnoreCase("!futuresOn")) {
    Color red = new Color(255, 0, 0);
    Color green = new Color(0, 204, 0);
    Runnable futRun = new Runnable() {
        public void run() {
            EmbedBuilder futuresBot = new EmbedBuilder();
            futuresBot.setAuthor("Futures Bot", null, event.getAuthor().getAvatarUrl());

            try {
                futuresBot.addField("**S&P 500**", getSPY(), false);
                futuresBot.addField("**NASDAQ**", getNASDAQ(), false);
                futuresBot.addField("**DOW**", getDOW(), false);
                if (getSPY().contains("+")) {
                    futuresBot.setColor(green);
                } else {
                    futuresBot.setColor(red);
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            futuresBot.setFooter("Cantina Capital", event.getGuild().getIconUrl());
            event.getChannel().sendMessage(futuresBot.build()).queue();
        }
    };

    if (scheduledFuture == null)
        scheduledFuture = exec.scheduleAtFixedRate(futRun, 0, 1, TimeUnit.HOURS);
}

if (args[0].equalsIgnoreCase("!futuresoff")) {
    event.getChannel().sendMessage("Futures bot off.").queue();
    if (scheduledFuture != null) {
        scheduledFuture.cancel(true);
        scheduledFuture = null;
    }
}
英文:

First you should save the result of scheduleAtFixedRate() inside your Listener class.

ScheduledFuture scheduledFuture = null;

Then you can use cancel() on this object to stop it.
You also should test if it is null before scheduling and test if it is set before canceling:

String[] args = event.getMessage().getContentRaw().split(" ");
if (args[0].equalsIgnoreCase("!futuresOn")) {
Color red = new Color(255, 0, 0);
Color green = new Color(0, 204, 0);
Runnable futRun = new Runnable() {
public void run() {
EmbedBuilder futuresBot = new EmbedBuilder();
futuresBot.setAuthor("Futures Bot", null, event.getAuthor().getAvatarUrl());
try {
futuresBot.addField("**S&P 500**", getSPY(), false);
futuresBot.addField("**NASDAQ**", getNASDAQ(), false);
futuresBot.addField("**DOW**", getDOW(), false);
if (getSPY().contains("+")) {
futuresBot.setColor(green);
}
else {
futuresBot.setColor(red);
}
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
futuresBot.setFooter("Cantina Capital", event.getGuild().getIconUrl());
event.getChannel().sendMessage(futuresBot.build()).queue();
}
};
if (scheduledFuture == null)
scheduledFuture = exec.scheduleAtFixedRate(futRun, 0, 1, TimeUnit.HOURS);
}
if (args[0].equalsIgnoreCase("!futuresoff")) {
event.getChannel().sendMessage("Futures bot off.").queue();
if (scheduledFuture != null) {
scheduledFuture.cancel(true);
scheduledFuture = null;
}
}
```
</details>

huangapple
  • 本文由 发表于 2020年8月2日 05:25:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/63210222.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定