"java.io.IOException: java.net.BindException: Address already in use" 在 Java Maven Heroku 应用中

"java.io.IOException: java.net.BindException: Address already in use" in Java Maven Heroku App

我正在尝试使用 API 从 google 工作表中提取数据,并且我的代码在我的本地计算机上运行。但是,在 Heroku 中部署它之后,我得到了这个错误。这是我怀疑在Heroku部署时失败的部分代码。

private static Credential getCredentials(final NetHttpTransport HTTP_TRANSPORT) throws IOException {
        InputStream in = ProcessGoogleSheets.class.getResourceAsStream(CREDENTIALS_FINAL_PATH);
        if (in == null) {
            throw new FileNotFoundException("Resource not found: " + CREDENTIALS_FINAL_PATH);
        }
        GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));

        GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
                HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES)
                .setDataStoreFactory(new FileDataStoreFactory(new java.io.File(TOKENS_DIRECTORY_PATH)))
                .setAccessType("offline")
                .build();
        LocalServerReceiver receiver = new LocalServerReceiver.Builder().setPort(8080).build();
        return new AuthorizationCodeInstalledApp(flow, receiver).authorize("user");
    }

这是Heroku返回的日志。

2022-03-18T06:12:02.088566+00:00 app[web.1]: Mar 18, 2022 6:12:02 AM org.apache.catalina.core.StandardWrapperValve invoke
2022-03-18T06:12:02.088574+00:00 app[web.1]: SEVERE: Servlet.service() for servlet [GetTrackOrderData] in context with path [] threw exception
2022-03-18T06:12:02.088574+00:00 app[web.1]: java.io.IOException: java.net.BindException: Address already in use
2022-03-18T06:12:02.088575+00:00 app[web.1]: at com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver.getRedirectUri(LocalServerReceiver.java:127)
2022-03-18T06:12:02.088576+00:00 app[web.1]: at com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp.authorize(AuthorizationCodeInstalledApp.java:121)
2022-03-18T06:12:02.088576+00:00 app[web.1]: at com.servlets.ProcessGoogleSheets.getCredentials(ProcessGoogleSheets.java:46)
2022-03-18T06:12:02.088577+00:00 app[web.1]: at com.servlets.ProcessGoogleSheets.getData(ProcessGoogleSheets.java:51)
2022-03-18T06:12:02.088577+00:00 app[web.1]: at com.servlets.GetTrackOrderData.processRequest(GetTrackOrderData.java:20)
2022-03-18T06:12:02.088578+00:00 app[web.1]: at com.servlets.GetTrackOrderData.doPost(GetTrackOrderData.java:68)
2022-03-18T06:12:02.088578+00:00 app[web.1]: at javax.servlet.http.HttpServlet.service(HttpServlet.java:660)
2022-03-18T06:12:02.088578+00:00 app[web.1]: at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
2022-03-18T06:12:02.088579+00:00 app[web.1]: at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
2022-03-18T06:12:02.088579+00:00 app[web.1]: at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
2022-03-18T06:12:02.088579+00:00 app[web.1]: at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
2022-03-18T06:12:02.088580+00:00 app[web.1]: at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
2022-03-18T06:12:02.088580+00:00 app[web.1]: at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
2022-03-18T06:12:02.088580+00:00 app[web.1]: at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202)
2022-03-18T06:12:02.088581+00:00 app[web.1]: at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
2022-03-18T06:12:02.088581+00:00 app[web.1]: at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541)
2022-03-18T06:12:02.088581+00:00 app[web.1]: at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
2022-03-18T06:12:02.088581+00:00 app[web.1]: at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
2022-03-18T06:12:02.088582+00:00 app[web.1]: at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
2022-03-18T06:12:02.088582+00:00 app[web.1]: at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
2022-03-18T06:12:02.088582+00:00 app[web.1]: at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:367)
2022-03-18T06:12:02.088582+00:00 app[web.1]: at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
2022-03-18T06:12:02.088583+00:00 app[web.1]: at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:860)
2022-03-18T06:12:02.088583+00:00 app[web.1]: at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1598)
2022-03-18T06:12:02.088583+00:00 app[web.1]: at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
2022-03-18T06:12:02.088584+00:00 app[web.1]: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
2022-03-18T06:12:02.088584+00:00 app[web.1]: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
2022-03-18T06:12:02.088584+00:00 app[web.1]: at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
2022-03-18T06:12:02.088585+00:00 app[web.1]: at java.lang.Thread.run(Thread.java:750)
2022-03-18T06:12:02.088586+00:00 app[web.1]: Caused by: java.net.BindException: Address already in use
2022-03-18T06:12:02.088587+00:00 app[web.1]: at sun.nio.ch.Net.bind0(Native Method)
2022-03-18T06:12:02.088587+00:00 app[web.1]: at sun.nio.ch.Net.bind(Net.java:461)
2022-03-18T06:12:02.088587+00:00 app[web.1]: at sun.nio.ch.Net.bind(Net.java:453)
2022-03-18T06:12:02.088587+00:00 app[web.1]: at sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:222)
2022-03-18T06:12:02.088588+00:00 app[web.1]: at sun.nio.ch.ServerSocketAdaptor.bind(ServerSocketAdaptor.java:85)
2022-03-18T06:12:02.088588+00:00 app[web.1]: at org.eclipse.jetty.server.nio.SelectChannelConnector.open(SelectChannelConnector.java:187)
2022-03-18T06:12:02.088588+00:00 app[web.1]: at org.eclipse.jetty.server.AbstractConnector.doStart(AbstractConnector.java:316)
2022-03-18T06:12:02.088589+00:00 app[web.1]: at org.eclipse.jetty.server.nio.SelectChannelConnector.doStart(SelectChannelConnector.java:265)
2022-03-18T06:12:02.088589+00:00 app[web.1]: at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:64)
2022-03-18T06:12:02.088589+00:00 app[web.1]: at org.eclipse.jetty.server.Server.doStart(Server.java:293)
2022-03-18T06:12:02.088589+00:00 app[web.1]: at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:64)
2022-03-18T06:12:02.088590+00:00 app[web.1]: at com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver.getRedirectUri(LocalServerReceiver.java:123)
2022-03-18T06:12:02.088591+00:00 app[web.1]: ... 28 more
2022-03-18T06:12:02.088591+00:00 app[web.1]:

有什么解决办法吗?提前致谢。

Address already in use -- 这里的地址通常表示PORT.

应用程序似乎已经 运行 在 Heroku 端口 8080 上,所以尝试更改端口并重新部署,你应该没问题。

我在 setPort 中使用了 -1,这个值会自动将应用程序连接到未使用的端口。

这是现在有效的代码。

private static Credential getCredentials(final NetHttpTransport HTTP_TRANSPORT) throws IOException {
    InputStream in = ProcessGoogleSheets.class.getResourceAsStream(CREDENTIALS_FINAL_PATH);
    if (in == null) {
        throw new FileNotFoundException("Resource not found: " + CREDENTIALS_FINAL_PATH);
    }
    GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));

    GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
            HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES)
            .setDataStoreFactory(new FileDataStoreFactory(new java.io.File(TOKENS_DIRECTORY_PATH)))
            .setAccessType("offline")
            .build();
    LocalServerReceiver receiver = new LocalServerReceiver.Builder().setPort(-1).build();
    return new AuthorizationCodeInstalledApp(flow, receiver).authorize("user");
}