Search in sources :

Example 6 with NamedThreadFactory

use of org.neo4j.helpers.NamedThreadFactory in project neo4j by neo4j.

the class NetworkedServerFactory method newNetworkedServer.

public ProtocolServer newNetworkedServer(final Config config, AcceptorInstanceStore acceptorInstanceStore, ElectionCredentialsProvider electionCredentialsProvider) {
    final NetworkReceiver receiver = new NetworkReceiver(networkReceiverMonitor, new NetworkReceiver.Configuration() {

        @Override
        public HostnamePort clusterServer() {
            return config.get(ClusterSettings.cluster_server);
        }

        @Override
        public int defaultPort() {
            return 5001;
        }

        @Override
        public String name() {
            return null;
        }
    }, logProvider);
    final NetworkSender sender = new NetworkSender(networkSenderMonitor, new NetworkSender.Configuration() {

        @Override
        public int defaultPort() {
            return 5001;
        }

        @Override
        public int port() {
            return config.get(ClusterSettings.cluster_server).getPort();
        }
    }, receiver, logProvider);
    ExecutorLifecycleAdapter stateMachineExecutor = new ExecutorLifecycleAdapter(() -> Executors.newSingleThreadExecutor(new NamedThreadFactory("State machine", namedThreadFactoryMonitor)));
    final ProtocolServer protocolServer = protocolServerFactory.newProtocolServer(config.get(ClusterSettings.server_id), timeoutStrategy, receiver, sender, acceptorInstanceStore, electionCredentialsProvider, stateMachineExecutor, objectInputStreamFactory, objectOutputStreamFactory, config);
    receiver.addNetworkChannelsListener(new NetworkReceiver.NetworkChannelsListener() {

        private StateTransitionLogger logger;

        @Override
        public void listeningAt(URI me) {
            protocolServer.listeningAt(me);
            if (logger == null) {
                logger = new StateTransitionLogger(logProvider, new AtomicBroadcastSerializer(objectInputStreamFactory, objectOutputStreamFactory));
                protocolServer.addStateTransitionListener(logger);
            }
        }

        @Override
        public void channelOpened(URI to) {
        }

        @Override
        public void channelClosed(URI to) {
        }
    });
    life.add(stateMachineExecutor);
    // Timeout timer - triggers every 10 ms
    life.add(new Lifecycle() {

        private ScheduledExecutorService scheduler;

        @Override
        public void init() throws Throwable {
            protocolServer.getTimeouts().tick(System.currentTimeMillis());
        }

        @Override
        public void start() throws Throwable {
            scheduler = Executors.newSingleThreadScheduledExecutor(new NamedThreadFactory("timeout"));
            scheduler.scheduleWithFixedDelay(new Runnable() {

                @Override
                public void run() {
                    long now = System.currentTimeMillis();
                    protocolServer.getTimeouts().tick(now);
                }
            }, 0, 10, TimeUnit.MILLISECONDS);
        }

        @Override
        public void stop() throws Throwable {
            scheduler.shutdownNow();
        }

        @Override
        public void shutdown() throws Throwable {
        }
    });
    // Add this last to ensure that timeout service is setup first
    life.add(sender);
    life.add(receiver);
    return protocolServer;
}
Also used : AtomicBroadcastSerializer(org.neo4j.cluster.protocol.atomicbroadcast.AtomicBroadcastSerializer) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) NamedThreadFactory(org.neo4j.helpers.NamedThreadFactory) HostnamePort(org.neo4j.helpers.HostnamePort) Lifecycle(org.neo4j.kernel.lifecycle.Lifecycle) URI(java.net.URI) NetworkReceiver(org.neo4j.cluster.com.NetworkReceiver) StateTransitionLogger(org.neo4j.cluster.statemachine.StateTransitionLogger) NetworkSender(org.neo4j.cluster.com.NetworkSender)

Aggregations

NamedThreadFactory (org.neo4j.helpers.NamedThreadFactory)6 URI (java.net.URI)2 ExecutorService (java.util.concurrent.ExecutorService)2 AtomicBroadcastSerializer (org.neo4j.cluster.protocol.atomicbroadcast.AtomicBroadcastSerializer)2 Bootstrap (io.netty.bootstrap.Bootstrap)1 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)1 ConnectException (java.net.ConnectException)1 InetSocketAddress (java.net.InetSocketAddress)1 URISyntaxException (java.net.URISyntaxException)1 ClosedChannelException (java.nio.channels.ClosedChannelException)1 ServerSocketChannel (java.nio.channels.ServerSocketChannel)1 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)1 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)1 Channel (org.jboss.netty.channel.Channel)1 ChannelFuture (org.jboss.netty.channel.ChannelFuture)1 ChannelFutureListener (org.jboss.netty.channel.ChannelFutureListener)1 ThreadRenamingRunnable (org.jboss.netty.util.ThreadRenamingRunnable)1 Test (org.junit.Test)1 NettyServer (org.neo4j.bolt.transport.NettyServer)1 NetworkReceiver (org.neo4j.cluster.com.NetworkReceiver)1