关闭 ScheduledExecutorService

Shutting down a ScheduledExecutorService

所以我有一个不和谐的机器人,我试图每小时发送一条消息。我使用预定的执行程序服务让它的那一部分工作。当我说 !futureson

时它就开始了

我希望在发送 !futuresoff 时能够停止它

但是,我无法让它正常工作。这是我的代码:

    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();
    }

我尝试了几种不同的方法,但无法正常工作。它正在进入 !futuresoff 部分,因为它发送了 Futures 机器人关闭消息。我只希望它能够在发送 !futureson 时每小时发送一次消息,而不是在发送 !futuresoff 时发送消息。我也试过 ScheduledThreadPoolExecutor exec = new ScheduledThreadPoolExecutor(0);

如果有人能给我指明正确的方向,我将不胜感激。

首先,您应该将 scheduleAtFixedRate() 的结果保存在您的监听器 class 中。

ScheduledFuture scheduledFuture = null;

然后你可以在这个对象上使用cancel()来阻止它。 您还应该在安排之前测试它是否为空,并在取消之前测试它是否已设置:

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;
    }
}
    ```