alluxio.util.network.NettyUtils.java Source code

Java tutorial

Introduction

Here is the source code for alluxio.util.network.NettyUtils.java

Source

/*
 * The Alluxio Open Foundation licenses this work under the Apache License, version 2.0
 * (the "License"). You may not use this work except in compliance with the License, which is
 * available at www.apache.org/licenses/LICENSE-2.0
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 * either express or implied, as more fully set forth in the License.
 *
 * See the NOTICE file distributed with this work for information regarding copyright ownership.
 */

package alluxio.util.network;

import alluxio.network.ChannelType;
import alluxio.util.ThreadFactoryUtils;

import io.netty.channel.EventLoopGroup;
import io.netty.channel.ServerChannel;
import io.netty.channel.epoll.EpollEventLoopGroup;
import io.netty.channel.epoll.EpollServerSocketChannel;
import io.netty.channel.epoll.EpollSocketChannel;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;

import java.util.concurrent.ThreadFactory;

import javax.annotation.concurrent.ThreadSafe;

/**
 * Utility methods for working with Netty.
 */
@ThreadSafe
public final class NettyUtils {
    private NettyUtils() {
    }

    /**
     * Creates a Netty {@link EventLoopGroup} based on {@link ChannelType}.
     *
     * @param type Selector for which form of low-level IO we should use
     * @param numThreads target number of threads
     * @param threadPrefix name pattern for each thread. should contain '%d' to distinguish between
     *        threads.
     * @param isDaemon if true, the {@link java.util.concurrent.ThreadFactory} will create daemon
     *        threads.
     * @return EventLoopGroup matching the ChannelType
     */
    public static EventLoopGroup createEventLoop(ChannelType type, int numThreads, String threadPrefix,
            boolean isDaemon) {
        ThreadFactory threadFactory = ThreadFactoryUtils.build(threadPrefix, isDaemon);

        switch (type) {
        case NIO:
            return new NioEventLoopGroup(numThreads, threadFactory);
        case EPOLL:
            return new EpollEventLoopGroup(numThreads, threadFactory);
        default:
            throw new IllegalArgumentException("Unknown io type: " + type);
        }
    }

    /**
     * Returns the correct {@link io.netty.channel.socket.ServerSocketChannel} class based on
     * {@link ChannelType}.
     *
     * @param type Selector for which form of low-level IO we should use
     * @return ServerSocketChannel matching the ChannelType
     */
    public static Class<? extends ServerChannel> getServerChannelClass(ChannelType type) {
        switch (type) {
        case NIO:
            return NioServerSocketChannel.class;
        case EPOLL:
            return EpollServerSocketChannel.class;
        default:
            throw new IllegalArgumentException("Unknown io type: " + type);
        }
    }

    /**
     * Returns the correct {@link SocketChannel} class based on {@link ChannelType}.
     *
     * @param type Selector for which form of low-level IO we should use
     * @return SocketChannel matching the ChannelType
     */
    public static Class<? extends SocketChannel> getClientChannelClass(ChannelType type) {
        switch (type) {
        case NIO:
            return NioSocketChannel.class;
        case EPOLL:
            return EpollSocketChannel.class;
        default:
            throw new IllegalArgumentException("Unknown io type: " + type);
        }
    }
}