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

com.alibaba.dts.client.executor.grid.ClientNodeSystemRemotingServer Maven / Gradle / Ivy

package com.alibaba.dts.client.executor.grid;

import com.alibaba.dts.client.executor.job.context.ClientContextImpl;
import com.alibaba.dts.common.logger.SchedulerXLoggerFactory;
import com.alibaba.dts.common.logger.innerlog.Logger;
import com.alibaba.dts.common.remoting.ChannelEventListener;
import com.alibaba.dts.common.remoting.netty.NettyServerConfig;
import com.alibaba.dts.common.remoting.netty.NodeNettyRemotingServer;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;

import java.net.BindException;
import java.net.InetSocketAddress;

/**
 * @author Ronan Zhan
 * @date 2017/7/6.
 */
public class ClientNodeSystemRemotingServer extends NodeNettyRemotingServer {

    private static final Logger logger = SchedulerXLoggerFactory.getLogger(ClientNodeSystemRemotingServer.class);

    private ClientContextImpl clientContext;

    public ClientNodeSystemRemotingServer(NettyServerConfig nettyServerConfig, ClientContextImpl clientContext) {
        super(nettyServerConfig);
        this.clientContext = clientContext;
    }

    public ClientNodeSystemRemotingServer(NettyServerConfig nettyServerConfig, ChannelEventListener channelEventListener, ClientContextImpl clientContext) {
        super(nettyServerConfig, channelEventListener);
        this.clientContext = clientContext;
    }

    @Override
    public void boot(ServerBootstrap serverBootstrap) {
        try {
            boolean bindSuccess = false;
            while (!bindSuccess) {
                int listenPort = clientContext.getNodeConfig().getSystemListenPort();
                try {
                    ChannelFuture sync = serverBootstrap.bind(listenPort);
                    sync.sync();
                    bindSuccess = true;
                    InetSocketAddress addr = (InetSocketAddress) sync.channel().localAddress();
                    this.port = addr.getPort();
                } catch (Exception e) {
                    if (e instanceof BindException) {
                        logger.warn(e.getMessage() + ", port=" + listenPort + ", we will try a new port");
                        listenPort = listenPort + 1;
                        if (listenPort > 65535) {
                            listenPort = 40000;
                        }
                        clientContext.getNodeConfig().setSystemListenPort(listenPort);
                    } else if (e instanceof InterruptedException) {
                        throw (InterruptedException) e;
                    } else {
                        throw new RuntimeException(e);
                    }
                }
            }
        } catch (InterruptedException e1) {
            throw new RuntimeException("this.serverBootstrap.bind().sync() InterruptedException", e1);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy