Heroku 在运行 java 电报机器人 90 秒后崩溃

Heroku crash after 90 sec working java telegram bot

我使用 api - https://github.com/rubenlagus/TelegramBots 编写了 java Telegram 机器人。我在我的 PC 上测试它,效果很好。所以我决定在 Heroku 上测试它,然而,在工作 90 秒后它崩溃了(前 90 秒运行良好)。 Heroku 日志:

2018-07-09T08:01:45.000000+00:00 app[api]: Build succeeded
2018-07-09T08:01:46.940201+00:00 heroku[web.1]: Starting process with command `java -Dserver.port=18914 -jar target/AllaBot-1.0.0.jar`
2018-07-09T08:01:49.163121+00:00 app[web.1]: Setting JAVA_TOOL_OPTIONS defaults based on dyno size. Custom settings will override them.
2018-07-09T08:01:49.166682+00:00 app[web.1]: Picked up JAVA_TOOL_OPTIONS: -Xmx300m -Xss512k -Dfile.encoding=UTF-8
2018-07-09T08:03:17.028453+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 90 seconds of launch
2018-07-09T08:03:17.028604+00:00 heroku[web.1]: Stopping process with SIGKILL
2018-07-09T08:03:17.121323+00:00 heroku[web.1]: Process exited with status 137
2018-07-09T08:03:17.140915+00:00 heroku[web.1]: State changed from starting to crashed

我的程序文件:

web: java -Dserver.port=$PORT -jar target/AllaBot-1.0.0.jar

据我了解,我需要更改项目中的端口,是吗?我尝试使用互联网上的一些提示,但它们对我不起作用。

Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 90 seconds of launch

错误告诉我们你需要监听 heroku 给我们的端口。那么让我们开始吧

    public class Bot extends TelegramLongPollingBot {
    private static final String TOKEN = System.getenv("TOKEN");
    private static final String BOT_USERNAME = System.getenv("BOT_USERNAME");
    private static final String PORT = System.getenv("PORT");

    public void onUpdateReceived(Update update) {

    }

    public String getBotUsername() {
        return BOT_USERNAME;
    }

    public String getBotToken() {
        return TOKEN;
    }

    public static void main(String[] args) {
        ApiContextInitializer.init();
        TelegramBotsApi api = new TelegramBotsApi();
        try {
            api.registerBot(new Bot());
        } catch (TelegramApiRequestException e) {
            e.printStackTrace();
        }

        try (ServerSocket serverSocket = new ServerSocket(Integer.valueOf(PORT))) {
            while (true) {
                Socket clientSocket = serverSocket.accept();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Sergey Nik 的回答在我浪费大量时间尝试修复此 R10 错误后对我帮助很大。我可以附加 Procfile 应该放在根目录中,看起来像这样:

web: java $JAVA_OPTS -Dserver.port=$PORT -jar target/<custom-bot>.jar