ScheduledExecutorService停止随机运行

时间:2019-03-20 03:30:03

标签: java runnable scheduledexecutorservice discord-jda

我有一个使用Discord JDA API的非常复杂的机器人,对于我的定期检查器以检查状态更改,我有一个ScheduledExecutorService。问题是,当我向机器人输入命令时,服务就有随机(?)机会停止工作,从而使整个程序无响应,而堆栈跟踪中没有任何错误。我通过放置一个简单的计时器来推断这一点,它似乎只是随机停止(不是,实际上,更像是它没有确定的模式)。这是检查器的类别:


import net.dv8tion.jda.core.JDA;
import net.dv8tion.jda.core.entities.Guild;
import net.dv8tion.jda.core.entities.User;
import us.verif.bot.sql.Sql;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class PeriodicCheck implements Runnable {
    int i = 0;
    private JDA jda;

    public PeriodicCheck(JDA jda) {
        this.jda = jda;
    }


    public void run() {
        i++;
        System.out.println(i);
        try {
            Connection connection = DataSource.getConnection();
            connection.setCatalog("verifus");
            PreparedStatement preparedStatement = connection.prepareStatement("select * from `activatedservers` where `expireDate` <= NOW()");
            ResultSet rs = preparedStatement.executeQuery();

            while (rs.next()) {
                String guildId = Config.getGuildId();
                User user = jda.getGuildById(guildId).getOwner().getUser();
                String server = jda.getGuildById(guildId).getName();
                user.openPrivateChannel().queue((channel) -> channel.sendMessage("Your Verifus activation for your server `" + server + "` has expired. \nVisit https://verif.us/ to purchase.").queue());
            }
            connection.close();
            Sql.deleteExpiredGuilds();

            Connection connection1 = DataSource.getConnection();
            connection1.setCatalog(Config.getGuildId());
            PreparedStatement preparedStatement1 = connection1.prepareStatement("select * from `verifiedusers` where `expireDate` <= NOW()");
            ResultSet rs1 = preparedStatement1.executeQuery();
            while (rs1.next()) {
                User user = jda.getUserById(rs1.getString("discordId"));
                String serverName = jda.getGuildById(Config.getGuildId()).getName();
                Guild guild = jda.getGuildById(Config.getGuildId());
                String role = rs1.getString("roleId");
                user.openPrivateChannel().queue((channel) -> channel.sendMessage("Your role `" + jda.getRoleById(role).getName() + "` in the server `" + serverName + "` has expired.").queue());
                guild.getController().removeSingleRoleFromMember(guild.getMemberById(rs1.getString("discordId")), jda.getRoleById(role)).queue();
            }
            connection1.close();
            Sql.deleteExpiredUsers();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

这是启动服务的Main类的一部分:

        PeriodicCheck periodicCheck = new PeriodicCheck(api);
        ScheduledExecutorService scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();
        try {
            scheduledExecutorService.scheduleAtFixedRate(periodicCheck, 0, 1, TimeUnit.SECONDS);
        } catch (Exception e) {
            e.printStackTrace();
        }

如果我在制作这些文件时没有记错的话,那么我很确定程序中的其他地方正在引起这种情况,而这将很难找到。
请注意,这确实是在我使用TimerTask而不是ScheduledExecutorService之前发生的,这是因为我没有关闭我的SQL连接,但是我确保关闭了我代码中的所有连接。

如果什么都没发生,我可以提供更多的程序类,尽管我不知道是哪个程序引起的,因为它随每个命令随机发生。可能是因为我的计算机/互联网吗?我正在用我的个人PC在家进行测试。

在我的checker类中还有另外两个未明确说明的方法,它们是: Sql.deleteExpiredGuilds

        try {
            Connection connection = DataSource.getConnection();
            connection.setCatalog("verifus");
            connection.prepareStatement("delete from activatedservers where expireDate <= NOW()").executeUpdate();
            connection.close();
        } catch (SQLException e) { e.printStackTrace(); }
    }

Sql.deleteExpiredUsers

        try {
            Connection connection = DataSource.getConnection();
            connection.setCatalog(Config.getGuildId());
            connection.prepareStatement("delete from verifiedusers where expireDate <= NOW()").executeUpdate();
            connection.close();
        } catch (SQLException e) { e.printStackTrace(); }
    }

0 个答案:

没有答案