All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.zodiac.autoconfigure.netty2.server.NettyEmbeddedAutoConfiguration Maven / Gradle / Ivy

There is a newer version: 1.6.8
Show newest version
package org.zodiac.autoconfigure.netty2.server;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.ListableBeanFactory;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.boot.autoconfigure.web.servlet.MultipartProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.core.annotation.AnnotationAwareOrderComparator;
import org.springframework.core.env.Environment;
import org.springframework.core.io.ResourceLoader;
import org.zodiac.autoconfigure.netty.condition.ConditionalOnNettyServerEnabled;
import org.zodiac.autoconfigure.netty2.NettyServerProperties;
import org.zodiac.commons.concurrent.thread.AbortPolicyWithReport;
import org.zodiac.commons.constants.SystemPropertiesConstants;
import org.zodiac.netty.protocol.http.util.HttpServletAbortPolicyWithReport;

import java.net.InetSocketAddress;
import java.util.Collection;
import java.util.concurrent.*;
import java.util.function.Supplier;

@SpringBootConfiguration
@ConditionalOnNettyServerEnabled
//@ConditionalOnProperty(name = org.zodiac.commons.constants.SystemPropertiesConstants.Zodiac.SERVER_NETTY_ENABLED, havingValue = "true")
@ConditionalOnClass(value = {io.netty.buffer.ByteBuf.class, io.netty.channel.Channel.class, org.zodiac.netty.core.config.NettyServerInfo.class})
public class NettyEmbeddedAutoConfiguration extends NettyServerConfiguration {

    public NettyEmbeddedAutoConfiguration() {
    }

    @Bean
    @ConditionalOnMissingBean
    @ConfigurationProperties(prefix = SystemPropertiesConstants.Zodiac.SERVER_NETTY_PREFIX, ignoreInvalidFields = true)
    @Override
    protected NettyServerProperties nettyServerProperties(
        ObjectProvider serverPropertiesProvider,
        ObjectProvider multipartPropertiesProvider) {
        return super.nettyServerProperties(serverPropertiesProvider, multipartPropertiesProvider);
    }

    @Bean(name = org.zodiac.netty.base.constants.NettyConstants.NETTY_TCP_SERVER_FACTORY_BEAN_NAME)
    @ConditionalOnMissingBean(name = org.zodiac.netty.base.constants.NettyConstants.NETTY_TCP_SERVER_FACTORY_BEAN_NAME)
    protected org.zodiac.netty.springboot2.server.NettyTcpSpringServerFactory nettyTcpSpringServerFactory(Collection protocolHandlers,
        Collection serverListeners, BeanFactory beanFactory,
        NettyServerProperties nettyServerProperties) {
        Supplier handlerSupplier = () -> {
            Class type = nettyServerProperties.getChannelHandler();
            return type == org.zodiac.netty.core.protocol.DynamicProtocolChannelHandler.class ?
                    new org.zodiac.netty.core.protocol.DynamicProtocolChannelHandler() : beanFactory.getBean(type);
        };
        org.zodiac.netty.springboot2.server.NettyTcpSpringServerFactory tcpServerFactory = new org.zodiac.netty.springboot2.server.NettyTcpSpringServerFactory(nettyServerProperties, handlerSupplier);
        tcpServerFactory.getProtocolHandlers().addAll(protocolHandlers);
        tcpServerFactory.getServerListeners().addAll(serverListeners);
        return tcpServerFactory;
    }

    @SpringBootConfiguration
    @ConditionalOnNettyServerEnabled
    @ConditionalOnClass(value = {org.reactivestreams.Publisher.class, io.reactivex.rxjava3.core.Observer.class})
    @ConditionalOnProperty(name = org.zodiac.netty.base.constants.NettySystemPropertiesConstants.SERVER_NETTY_NRPC_ENABLED, havingValue = "true", matchIfMissing = true)
    protected class NettyNRpcConfiguration {

        public NettyNRpcConfiguration() {
            super();
        }

        @Bean(name = org.zodiac.netty.base.constants.NettyConstants.NETTY_RPC_PROTOCOL_BEAN_NAME)
        @ConditionalOnMissingBean(name = org.zodiac.netty.base.constants.NettyConstants.NETTY_RPC_PROTOCOL_BEAN_NAME)
        protected org.zodiac.netty.protocol.NRpcProtocol nRpcProtocol(ConfigurableBeanFactory factory,
            NettyServerProperties nettyServerProperties) throws ClassNotFoundException {
            /*Preheat codec.*/
            Class.forName("org.zodiac.netty.protocol.nprc.codec.DataCodecUtil");

            org.zodiac.netty.springboot2.server.NRpcProtocolSpringAdapter protocol = new org.zodiac.netty.springboot2.server.NRpcProtocolSpringAdapter(nettyServerProperties.getApplication());
            protocol.setMessageMaxLength(nettyServerProperties.getNrpc().getServerMessageMaxLength());
            protocol.setMethodOverwriteCheck(nettyServerProperties.getNrpc().isServerMethodOverwriteCheck());
            protocol.setServerDefaultVersion(nettyServerProperties.getNrpc().getServerDefaultVersion());
            nettyServerProperties.getNrpc().getThreadPool().setRejected(HttpServletAbortPolicyWithReport.class);
            protocol.setExecutorSupplier(newExecutorSupplier(nettyServerProperties.getNrpc().getThreadPool(), factory));
            return protocol;
        }

    }

//    @Bean(name = NettyConstants.NETTY_RPC_PROTOCOL_BEAN_NAME)
//    @ConditionalOnMissingBean(name = NettyConstants.NETTY_RPC_PROTOCOL_BEAN_NAME)
//    @ConditionalOnProperty(name = NettySystemPropertiesConstants.SERVER_NETTY_NRPC_ENABLED, havingValue = "true", matchIfMissing = true)
//    protected NRpcProtocol nRpcProtocol(ConfigurableBeanFactory factory,
//        NettyServerProperties nettyServerProperties) throws ClassNotFoundException {
//        /*Preheat codec.*/
//        Class.forName("org.zodiac.netty.protocol.nprc.codec.DataCodecUtil");
//
//        NRpcProtocolSpringAdapter protocol = new NRpcProtocolSpringAdapter(nettyServerProperties.getApplication());
//        protocol.setMessageMaxLength(nettyServerProperties.getNrpc().getServerMessageMaxLength());
//        protocol.setMethodOverwriteCheck(nettyServerProperties.getNrpc().isServerMethodOverwriteCheck());
//        protocol.setServerDefaultVersion(nettyServerProperties.getNrpc().getServerDefaultVersion());
//        protocol.setExecutorSupplier(newExecutorSupplier(nettyServerProperties.getNrpc().getThreadPool(), factory));
//        return protocol;
//    }

    @SpringBootConfiguration
    @ConditionalOnNettyServerEnabled
    @ConditionalOnProperty(name = org.zodiac.netty.base.constants.NettySystemPropertiesConstants.SERVER_NETTY_HTTP_ENABLED, havingValue = "true", matchIfMissing = true)
    @ConditionalOnClass(value = {io.netty.handler.codec.http.HttpServerCodec.class, io.netty.handler.codec.http2.Http2FrameCodec.class, javax.servlet.http.HttpServlet.class, org.zodiac.netty.protocol.http.codec.NettyHttpServerCodec.class})
    //@ConditionalOnClass(name = {"io.netty.handler.codec.http.HttpServerCodec", "io.netty.handler.codec.http2.Http2FrameCodec", "javax.servlet.http.HttpServlet", "javax.websocket.MessageHandler"})
    protected class NettyHttpConfiguration {

        public NettyHttpConfiguration() {
            super();
        }

        @Bean(name = org.zodiac.netty.base.constants.NettyConstants.NETTY_HTTP_SERVLET_PROTOCOL_BEAN_NAME)
        @ConditionalOnMissingBean(name = org.zodiac.netty.base.constants.NettyConstants.NETTY_HTTP_SERVLET_PROTOCOL_BEAN_NAME)
        protected org.zodiac.netty.protocol.HttpServletProtocol httpServletProtocol(ConfigurableBeanFactory factory, ResourceLoader resourceLoader,
            NettyServerProperties nettyServerProperties) {
            org.zodiac.netty.core.protocol.http.config.NettyHttpInfo httpInfo = nettyServerProperties.getHttp();
            org.zodiac.netty.core.protocol.http.config.NettyHttpServletInfo httpServletInfo = httpInfo.getServlet();
            httpServletInfo.getThreadPool().setRejected(HttpServletAbortPolicyWithReport.class);
            Supplier executorSupplier = newExecutorSupplier(httpServletInfo.getThreadPool(), factory);
            Supplier defaultExecutorSupplier = newDefaultExecutorSupplier(httpServletInfo.getThreadPool(), factory);

            org.zodiac.netty.springboot2.server.HttpServletProtocolSpringAdapter protocol = new org.zodiac.netty.springboot2.server.HttpServletProtocolSpringAdapter(
                nettyServerProperties, resourceLoader.getClassLoader(), executorSupplier, defaultExecutorSupplier);
            protocol.setMaxInitialLineLength(httpInfo.getMaxRequestHeaderLineSize());
            protocol.setMaxHeaderSize(httpInfo.getMaxRequestHeaderSize());
            protocol.setMaxContentLength(httpInfo.getMaxRequestContentSize());
            protocol.setMaxBufferBytes(httpInfo.getMaxResponseBufferSize());
            protocol.setAutoFlushIdleMills(httpInfo.getAutoFlushIdleMills());

            factory.addBeanPostProcessor(protocol);
            return protocol;
        }
    }

//    @Bean(name = NettyConstants.NETTY_HTTP_SERVLET_PROTOCOL_BEAN_NAME)
//    @ConditionalOnMissingBean(name = NettyConstants.NETTY_HTTP_SERVLET_PROTOCOL_BEAN_NAME)
//    @ConditionalOnProperty(name = NettySystemPropertiesConstants.SERVER_NETTY_HTTP_ENABLED, havingValue = "true", matchIfMissing = true)
//    protected HttpServletProtocol httpServletProtocol(ConfigurableBeanFactory factory, ResourceLoader resourceLoader,
//        NettyServerProperties nettyServerProperties) {
//        NettyHttpInfo httpInfo = nettyServerProperties.getHttp();
//        NettyHttpServletInfo httpServletInfo = httpInfo.getServlet();
//        Supplier executorSupplier = newExecutorSupplier(httpServletInfo.getThreadPool(), factory);
//        Supplier defaultExecutorSupplier = newDefaultExecutorSupplier(httpServletInfo.getThreadPool(), factory);
//
//        HttpServletProtocolSpringAdapter protocol = new HttpServletProtocolSpringAdapter(
//            nettyServerProperties, resourceLoader.getClassLoader(), executorSupplier, defaultExecutorSupplier);
//        protocol.setMaxInitialLineLength(httpInfo.getMaxRequestHeaderLineSize());
//        protocol.setMaxHeaderSize(httpInfo.getMaxRequestHeaderSize());
//        protocol.setMaxContentLength(httpInfo.getMaxRequestContentSize());
//        protocol.setMaxBufferBytes(httpInfo.getMaxResponseBufferSize());
//        protocol.setAutoFlushIdleMills(httpInfo.getAutoFlushIdleMills());
//
//        factory.addBeanPostProcessor(protocol);
//        return protocol;
//    }

    @SpringBootConfiguration
    @ConditionalOnNettyServerEnabled
    @ConditionalOnProperty(name = org.zodiac.netty.base.constants.NettySystemPropertiesConstants.SERVER_NETTY_MQTT_ENABLED, havingValue = "true")
    @ConditionalOnClass(value = {io.netty.handler.codec.mqtt.MqttDecoder.class, io.netty.handler.codec.redis.RedisDecoder.class})
    protected class NettyMqttConfiguration {

        public NettyMqttConfiguration() {
            super();
        }

        @Bean(name = org.zodiac.netty.base.constants.NettyConstants.NETTY_MQTT_PROTOCOL_BEAN_NAME)
        @ConditionalOnMissingBean(name = org.zodiac.netty.base.constants.NettyConstants.NETTY_MQTT_PROTOCOL_BEAN_NAME)
        protected org.zodiac.netty.protocol.MqttProtocol mqttProtocol(ListableBeanFactory beanFactory, NettyServerProperties nettyServerProperties) {
            org.zodiac.netty.core.protocol.mqtt.config.NettyServerMqttInfo mqtt = nettyServerProperties.getMqtt();
            org.zodiac.netty.protocol.MqttProtocol protocol = new org.zodiac.netty.protocol.MqttProtocol(mqtt.getMaxMessageSize(), mqtt.getReaderIdleTimeSeconds(), mqtt.getAutoFlushIdleMills());
            beanFactory.getBeansOfType(org.zodiac.netty.protocol.mqtt.interception.InterceptHandler.class).values().forEach(protocol::addInterceptHandler);
            return protocol;
        }
    }

//    @Bean(name = NettyConstants.NETTY_MQTT_PROTOCOL_BEAN_NAME)
//    @ConditionalOnMissingBean(name = NettyConstants.NETTY_MQTT_PROTOCOL_BEAN_NAME)
//    @ConditionalOnProperty(name = NettySystemPropertiesConstants.SERVER_NETTY_MQTT_ENABLED, havingValue = "true")
//    protected MqttProtocol mqttProtocol(ListableBeanFactory beanFactory, NettyServerProperties nettyServerProperties) {
//        NettyServerMqttInfo mqtt = nettyServerProperties.getMqtt();
//        MqttProtocol protocol = new MqttProtocol(mqtt.getMaxMessageSize(), mqtt.getReaderIdleTimeSeconds(), mqtt.getAutoFlushIdleMills());
//        beanFactory.getBeansOfType(InterceptHandler.class).values().forEach(protocol::addInterceptHandler);
//        return protocol;
//    }

    @SpringBootConfiguration
    @ConditionalOnNettyServerEnabled
    @ConditionalOnProperty(name = org.zodiac.netty.base.constants.NettySystemPropertiesConstants.SERVER_NETTY_MYSQL_ENABLED, havingValue = "true")
    protected class NettyMySqlConfiguration {

        public NettyMySqlConfiguration() {
            super();
        }

        @Bean(name = org.zodiac.netty.base.constants.NettyConstants.NETTY_MYSQL_PROTOCOL_BEAN_NAME)
        @ConditionalOnMissingBean(name = org.zodiac.netty.base.constants.NettyConstants.NETTY_MYSQL_PROTOCOL_BEAN_NAME)
        protected org.zodiac.netty.protocol.MySqlProtocol mysqlServerProtocol(ListableBeanFactory beanFactory,
            @Autowired(required = false) Collection mysqlPacketListeners,
            NettyServerProperties nettyServerProperties) {
            org.zodiac.netty.core.protocol.mysql.config.NettyServerMySqlInfo mysql = nettyServerProperties.getMysql();
            org.zodiac.netty.protocol.MySqlProtocol protocol = new org.zodiac.netty.protocol.MySqlProtocol(new InetSocketAddress(mysql.getMysqlHost(), mysql.getMysqlPort()));
            protocol.setMaxPacketSize(mysql.getMaxPacketSize());
            if (mysqlPacketListeners != null) {
                protocol.getMysqlPacketListeners().addAll(mysqlPacketListeners);
            }
            protocol.getMysqlPacketListeners().sort(AnnotationAwareOrderComparator.INSTANCE);

            if (mysql.getFrontendBusinessHandler() != org.zodiac.netty.protocol.mysql.client.MySqlFrontendBusinessHandler.class) {
                String[] names = beanFactory.getBeanNamesForType(mysql.getFrontendBusinessHandler());
                for (String name : names) {
                    if (beanFactory.isSingleton(name)) {
                        throw new AssertionError("\nNetty Server Properties AssertionError(!isSingleton('" + name + "')) -> \n" +
                                "Need is the prototype. please add -> @org.springframework.context.annotation.Scope(\"prototype\").\n" +
                                "server:\n" +
                                "\tnetty:\n" +
                                "\t\tmysql:\n" +
                                "\t\t\tfrontendBusinessHandler: " + mysql.getFrontendBusinessHandler().getName() + "\n");
                    }
                }
                protocol.setFrontendBusinessHandler(() -> (org.zodiac.netty.protocol.mysql.client.MySqlFrontendBusinessHandler)beanFactory.getBean(mysql.getFrontendBusinessHandler()));
            }

            if (mysql.getBackendBusinessHandler() != org.zodiac.netty.protocol.mysql.server.MySqlBackendBusinessHandler.class) {
                String[] names = beanFactory.getBeanNamesForType(mysql.getBackendBusinessHandler());
                for (String name : names) {
                    if (beanFactory.isSingleton(name)) {
                        throw new AssertionError("\nNetty Server Properties AssertionError(!isSingleton('" + name + "')) -> \n" +
                                "Need is the prototype. please add -> @org.springframework.context.annotation.Scope(\"prototype\").\n" +
                                "server:\n" +
                                "\tnetty:\n" +
                                "\t\tmysql:\n" +
                                "\t\t\tbackendBusinessHandler: " + mysql.getBackendBusinessHandler().getName() + "\n");
                    }
                }
                protocol.setBackendBusinessHandler(() -> (org.zodiac.netty.protocol.mysql.server.MySqlBackendBusinessHandler)beanFactory.getBean(mysql.getBackendBusinessHandler()));
            }
            return protocol;
        }

        @Bean(name = org.zodiac.netty.base.constants.NettyConstants.NETTY_MYSQL_WRITER_LOG_FILE_LISTENER_BEAN_NAME)
        @ConditionalOnMissingBean(name = org.zodiac.netty.base.constants.NettyConstants.NETTY_MYSQL_WRITER_LOG_FILE_LISTENER_BEAN_NAME)
        protected org.zodiac.netty.protocol.mysql.listener.WriterLogFilePacketListener mysqlWriterLogFilePacketListener(Environment environment,
            NettyServerProperties nettyServerProperties) {
            org.zodiac.netty.core.protocol.mysql.config.NettyServerMySqlInfo mysql = nettyServerProperties.getMysql();
            org.zodiac.netty.protocol.mysql.listener.WriterLogFilePacketListener listener = new org.zodiac.netty.protocol.mysql.listener.WriterLogFilePacketListener();
            listener.setEnable(mysql.getProxyLog().isEnabled());
            listener.setLogFileName(environment.resolvePlaceholders(mysql.getProxyLog().getLogFileName()));
            listener.setLogPath(environment.resolvePlaceholders(mysql.getProxyLog().getLogPath()));
            listener.setLogWriteInterval(mysql.getProxyLog().getFlushIntervalMills());
            return listener;
        }
    }

//    @Bean(name = NettyConstants.NETTY_MYSQL_PROTOCOL_BEAN_NAME)
//    @ConditionalOnMissingBean(name = NettyConstants.NETTY_MYSQL_PROTOCOL_BEAN_NAME)
//    @ConditionalOnProperty(name = NettySystemPropertiesConstants.SERVER_NETTY_MYSQL_ENABLED, havingValue = "true")
//    protected MySqlProtocol mysqlServerProtocol(ListableBeanFactory beanFactory,
//        @Autowired(required = false) Collection mysqlPacketListeners,
//        NettyServerProperties nettyServerProperties) {
//        NettyServerMySqlInfo mysql = nettyServerProperties.getMysql();
//        MySqlProtocol protocol = new MySqlProtocol(new InetSocketAddress(mysql.getMysqlHost(), mysql.getMysqlPort()));
//        protocol.setMaxPacketSize(mysql.getMaxPacketSize());
//        if (mysqlPacketListeners != null) {
//            protocol.getMysqlPacketListeners().addAll(mysqlPacketListeners);
//        }
//        protocol.getMysqlPacketListeners().sort(AnnotationAwareOrderComparator.INSTANCE);
//
//        if (mysql.getFrontendBusinessHandler() != MySqlFrontendBusinessHandler.class) {
//            String[] names = beanFactory.getBeanNamesForType(mysql.getFrontendBusinessHandler());
//            for (String name : names) {
//                if (beanFactory.isSingleton(name)) {
//                    throw new AssertionError("\nNetty Server Properties AssertionError(!isSingleton('" + name + "')) -> \n" +
//                            "Need is the prototype. please add -> @org.springframework.context.annotation.Scope(\"prototype\").\n" +
//                            "server:\n" +
//                            "\tnetty:\n" +
//                            "\t\tmysql:\n" +
//                            "\t\t\tfrontendBusinessHandler: " + mysql.getFrontendBusinessHandler().getName() + "\n");
//                }
//            }
//            protocol.setFrontendBusinessHandler(() -> (MySqlFrontendBusinessHandler)beanFactory.getBean(mysql.getFrontendBusinessHandler()));
//        }
//
//        if (mysql.getBackendBusinessHandler() != MySqlBackendBusinessHandler.class) {
//            String[] names = beanFactory.getBeanNamesForType(mysql.getBackendBusinessHandler());
//            for (String name : names) {
//                if (beanFactory.isSingleton(name)) {
//                    throw new AssertionError("\nNetty Server Properties AssertionError(!isSingleton('" + name + "')) -> \n" +
//                            "Need is the prototype. please add -> @org.springframework.context.annotation.Scope(\"prototype\").\n" +
//                            "server:\n" +
//                            "\tnetty:\n" +
//                            "\t\tmysql:\n" +
//                            "\t\t\tbackendBusinessHandler: " + mysql.getBackendBusinessHandler().getName() + "\n");
//                }
//            }
//            protocol.setBackendBusinessHandler(() -> (MySqlBackendBusinessHandler)beanFactory.getBean(mysql.getBackendBusinessHandler()));
//        }
//        return protocol;
//    }
//
//    @Bean(name = NettyConstants.NETTY_MYSQL_WRITER_LOG_FILE_LISTENER_BEAN_NAME)
//    @ConditionalOnMissingBean(name = NettyConstants.NETTY_MYSQL_WRITER_LOG_FILE_LISTENER_BEAN_NAME)
//    @ConditionalOnProperty(name = NettySystemPropertiesConstants.SERVER_NETTY_MYSQL_ENABLED, havingValue = "true")
//    protected WriterLogFilePacketListener mysqlWriterLogFilePacketListener(Environment environment,
//        NettyServerProperties nettyServerProperties) {
//        NettyServerMySqlInfo mysql = nettyServerProperties.getMysql();
//        WriterLogFilePacketListener listener = new WriterLogFilePacketListener();
//        listener.setEnable(mysql.getProxyLog().isEnabled());
//        listener.setLogFileName(environment.resolvePlaceholders(mysql.getProxyLog().getLogFileName()));
//        listener.setLogPath(environment.resolvePlaceholders(mysql.getProxyLog().getLogPath()));
//        listener.setLogWriteInterval(mysql.getProxyLog().getFlushIntervalMills());
//        return listener;
//    }

    protected Supplier newExecutorSupplier(org.zodiac.netty.core.protocol.http.config.NettyHttpServletInfo.ServerThreadPool pool, ConfigurableBeanFactory factory) {
        Supplier executorSupplier;
        if (pool.isEnabled()) {
            if (pool.getExecutor() == org.zodiac.netty.base.thread.NettyThreadPoolExecutor.class) {
                RejectedExecutionHandler rejectedHandler;
                if (pool.getRejected() == org.zodiac.netty.protocol.http.util.HttpServletAbortPolicyWithReport.class) {
                    rejectedHandler = new org.zodiac.netty.protocol.http.util.HttpServletAbortPolicyWithReport(pool.getPoolName(), pool.getDumpPath(), "HttpServlet");
                } else {
                    rejectedHandler = factory.getBean(pool.getRejected());
                }

                String poolName = pool.getPoolName();
                int coreThreads = pool.getCoreThreads();
                int maxThreads = pool.getMaxThreads();
                int queues = pool.getQueues();
                int keepAliveSeconds = pool.getKeepAliveSeconds();
                boolean fixed = pool.isFixed();
                org.zodiac.netty.base.thread.NettyThreadPoolExecutor executor = newNettyThreadPoolExecutor(poolName, coreThreads, maxThreads, queues, keepAliveSeconds, fixed, rejectedHandler);
                executorSupplier = () -> executor;
            } else {
                Executor executor = factory.getBean(pool.getExecutor());
                executorSupplier = () -> executor;
            }
        } else {
            executorSupplier = () -> null;
        }
        return executorSupplier;
    }

    protected Supplier newDefaultExecutorSupplier(org.zodiac.netty.core.protocol.http.config.NettyHttpServletInfo.ServerThreadPool pool, ConfigurableBeanFactory factory) {
        RejectedExecutionHandler rejectedHandler;
        if (pool.getRejected() == org.zodiac.netty.protocol.http.util.HttpServletAbortPolicyWithReport.class) {
            rejectedHandler = new org.zodiac.netty.protocol.http.util.HttpServletAbortPolicyWithReport(pool.getPoolName(), pool.getDumpPath(), "Default Pool HttpServlet");
        } else {
            rejectedHandler = factory.getBean(pool.getRejected());
        }
        return new LazyPool(this, pool, rejectedHandler);
    }

    protected Supplier newExecutorSupplier(org.zodiac.netty.core.protocol.remote.config.NettyServerNRpcInfo.ServerThreadPool pool, ConfigurableBeanFactory factory) {
        Supplier executorSupplier;
        if (pool.isEnabled()) {
            if (pool.getExecutor() == org.zodiac.netty.base.thread.NettyThreadPoolExecutor.class) {
                RejectedExecutionHandler rejectedHandler;
                if (pool.getRejected() == AbortPolicyWithReport.class) {
                    rejectedHandler = new AbortPolicyWithReport(pool.getPoolName(), pool.getDumpPath(), "Nrpc");
                } else {
                    rejectedHandler = factory.getBean(pool.getRejected());
                }

                String poolName = pool.getPoolName();
                int coreThreads = pool.getCoreThreads();
                int maxThreads = pool.getMaxThreads();
                int queues = pool.getQueues();
                int keepAliveSeconds = pool.getKeepAliveSeconds();
                boolean fixed = pool.isFixed();
                org.zodiac.netty.base.thread.NettyThreadPoolExecutor executor = newNettyThreadPoolExecutor(poolName, coreThreads, maxThreads, queues, keepAliveSeconds, fixed, rejectedHandler);
                executorSupplier = () -> executor;
            } else {
                executorSupplier = () -> factory.getBean(pool.getExecutor());
            }
        } else {
            executorSupplier = () -> null;
        }
        return executorSupplier;
    }

    protected org.zodiac.netty.base.thread.NettyThreadPoolExecutor newNettyThreadPoolExecutor(
            String poolName,
            int coreThreads,
            int maxThreads,
            int queues,
            int keepAliveSeconds,
            boolean fixed,
            RejectedExecutionHandler handler) {
        BlockingQueue workQueue = queues == 0 ?
                new SynchronousQueue<>() :
                (queues < 0 ? new LinkedBlockingQueue<>(Integer.MAX_VALUE)
                        : new LinkedBlockingQueue<>(queues));
        if (fixed) {
            int max = Math.max(coreThreads, maxThreads);
            coreThreads = max;
            maxThreads = max;
        }
        int priority = Thread.NORM_PRIORITY;
        boolean daemon = true;
        return new org.zodiac.netty.base.thread.NettyThreadPoolExecutor(
                coreThreads, maxThreads, keepAliveSeconds, TimeUnit.SECONDS,
                workQueue, poolName, priority, daemon, handler);
    }

    public static class LazyPool implements Supplier {
        protected final org.zodiac.netty.core.protocol.http.config.NettyHttpServletInfo.ServerThreadPool pool;
        protected final RejectedExecutionHandler rejectedHandler;
        protected final NettyEmbeddedAutoConfiguration autoConfiguration;
        protected /*volatile*/ Executor executor;

        public LazyPool(NettyEmbeddedAutoConfiguration autoConfiguration, org.zodiac.netty.core.protocol.http.config.NettyHttpServletInfo.ServerThreadPool pool, RejectedExecutionHandler rejectedHandler) {
            this.autoConfiguration = autoConfiguration;
            this.pool = pool;
            this.rejectedHandler = rejectedHandler;
        }

        @Override
        public Executor get() {
            if (executor == null) {
                synchronized (this) {
                    if (executor == null) {
                        String poolName = pool.getPoolName();
                        int coreThreads = pool.getCoreThreads();
                        int maxThreads = pool.getMaxThreads();
                        int queues = pool.getQueues();
                        int keepAliveSeconds = pool.getKeepAliveSeconds();
                        boolean fixed = pool.isFixed();
                        executor = autoConfiguration.newNettyThreadPoolExecutor(poolName, coreThreads, maxThreads, queues, keepAliveSeconds, fixed, rejectedHandler);
                    }
                }
            }
            return executor;
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy