java服务抛出java.io.ioexception:打开的文件太多

ctrmrzij  于 2021-10-10  发布在  Java
关注(0)|答案(0)|浏览(371)

我有一个java服务,它调用aws lambda和elasticsearch。当我将此服务部署到aws ecs时,运行时会出现以下异常。

2021-06-21T07:36:51.967-07:00   RequestId: 08b4e9d3-ed29-4906-ab6c-486937575fd8

2021-06-21T07:37:09.206-07:00   Socket accept failed

2021-06-21T07:37:09.206-07:00   java.io.IOException: Too many open files

2021-06-21T07:37:09.206-07:00   at sun.nio.ch.ServerSocketChannelImpl.accept0(Native Method) ~[?:1.8.0_292]

2021-06-21T07:37:09.206-07:00   at sun.nio.ch.ServerSocketChannelImpl.accept(ServerSocketChannelImpl.java:421) ~[?:1.8.0_292]

2021-06-21T07:37:09.206-07:00   at sun.nio.ch.ServerSocketChannelImpl.accept(ServerSocketChannelImpl.java:249) ~[?:1.8.0_292]

2021-06-21T07:37:09.206-07:00   at org.apache.tomcat.util.net.NioEndpoint.serverSocketAccept(NioEndpoint.java:574) ~[tomcat-coyote.jar:9.0.45]

2021-06-21T07:37:09.206-07:00   at org.apache.tomcat.util.net.NioEndpoint.serverSocketAccept(NioEndpoint.java:80) ~[tomcat-coyote.jar:9.0.45]

2021-06-21T07:37:09.206-07:00   at org.apache.tomcat.util.net.Acceptor.run(Acceptor.java:106) ~[tomcat-coyote.jar:9.0.45]

2021-06-21T07:37:09.206-07:00   at java.lang.Thread.run(Thread.java:749) [?:1.8.0_292]

2021-06-21T07:37:09.259-07:00   Socket accept failed

2021-06-21T07:37:09.259-07:00   java.io.IOException: Too many open files

在我最近对代码做了一些更改之前,它一直运行良好。下面是我的服务主类的外观。

private static final Logger log = LoggerFactory.getLogger(DataService.class);

    private static final String APP_NAME = "DataService";

    private static final int TEN_SECONDS = (int) TimeUnit.SECONDS.toMillis(10);

    private static final int EXECUTOR_THREAD_POOL_SIZE = 10;

    public static void main(String[] args) throws Throwable {
        log.info("Starting with args {}", Arrays.toString(args));
        initAppConfig(args);

        String root = System.getProperty("root");
        String realm = AppConfig.getRealm().name();
        String domain = AppConfig.getDomain();

        ExecutorService auxiliaryDataExecutor = Executors.newFixedThreadPool(EXECUTOR_THREAD_POOL_SIZE);

        Injector injector = createInjector(
                new MyModule(root, domain, realm),
                new DataServiceModule(auxiliaryDataExecutor)
        );

        final TomcatServerWrapper server = injector.getInstance(TomcatServerWrapper.class);
        server.start();

        getRuntime().addShutdownHook(new Thread(() -> stopServer(server)));

        getRuntime().addShutdownHook(new Thread(auxiliaryDataExecutor::shutdown));

        // wait for termination
        Thread.currentThread().join();
    }

    private static synchronized void stopServer(TomcatServerWrapper server) {
        try {
            server.shutdown();
        } catch (Exception e) {
            log.warn("Exception while stopping TomcatServerWrapper: ", e);
        }
    }

    private static void initAppConfig(String[] args) throws InterruptedException {
        verifyArguments(args);

        AppConfig.initialize(APP_NAME, null, args);
    }

    private static void verifyArguments(String[] args) throws InterruptedException {
        boolean hasRealm = false;
        boolean hasDomain = false;
        boolean hasRoot = false;

        for (String arg : args) {
            if (arg.startsWith("--realm=")) {
                hasRealm = true;
            } else if (arg.startsWith("--domain=")) {
                hasDomain = true;
            } else if (arg.startsWith("--root=")) {
                hasRoot = true;
            }
        }

        if (hasRealm && hasDomain && hasRoot) {
            return;
        } else {
            System.out.println("The service cannot determine what environment it is running in and will shut down.");
            System.out.println("If you are trying to run from an Eclipse workspace, add the following");
            System.out.println("program arguments to your launch configuration: ");
            System.out.println("");
            Thread.sleep(TEN_SECONDS); 
            System.exit(0);
        }
    }

    private static AutoCloseable getCertMonitor(Injector injector) {
        return injector.getInstance(Key.get(AutoCloseable.class,
                Names.named("ServiceCertificateMonitor")));
    }

由于这是在aws ecs中的一个容器中运行的,我不确定从哪里开始对此进行调查。我看到的唯一错误是我在上面粘贴的cloudwatch日志中的异常。当我在我的机器上本地运行服务时,我没有看到这个错误。在此调试过程中,如有任何建议/指导,将不胜感激。提前谢谢。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题