首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Netty ThreadRenamingRunnable

Netty ThreadRenamingRunnable
EN

Stack Overflow用户
提问于 2013-12-30 16:46:54
回答 3查看 710关注 0票数 0

我很难弄清楚如何使用ThreadRenamingRunnable来重命名netty中的工作线程。我是第一次接触netty并使用netty 3.9.0-Final。

我想将工作线程重命名为“New I/O worker #X”。我对老板线程的名字没意见。

这是一个基本的服务器,它使用"pong“响应"ping”。

代码语言:javascript
复制
public class NettyPingPong {

    public static void main(String[] args) {
        ServerBootstrap bootstrap = new ServerBootstrap(
                new NioServerSocketChannelFactory(
                Executors.newCachedThreadPool(),
                Executors.newCachedThreadPool()));

        bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
            public ChannelPipeline getPipeline() throws Exception {
                return Channels.pipeline(
                        new LineBasedFrameDecoder(255,true,true),
                        new PongUpstreamHandler(),
                        new StringEncoder());

            }
        });

        bootstrap.bind(new InetSocketAddress(8899));
            out.println("im ready");
        }
    }

代码语言:javascript
复制
public class PongUpstreamHandler extends SimpleChannelUpstreamHandler {

    @Override
    public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) {

        ChannelBuffer buffer = (ChannelBuffer) e.getMessage();
        String message = new String(buffer.array());
        if (message.equalsIgnoreCase("ping")){
            e.getChannel().write("pong\n");
            out.println("ponged...");
        }
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) {
        e.getCause().printStackTrace();
        e.getChannel().close();
        out.println("closed...");
    }
}
EN

回答 3

Stack Overflow用户

发布于 2015-07-15 19:26:30

1.5年前的问题:(我带着同样的问题来到这里,在Netty 3.5.0 Final上工作。我用下面的代码解决了这个问题。

我使用了一个线程工厂,它也会生成类似于Abe提到的有意义的threadNames。而且,我将ThreadRenamingRunnable配置为不重命名我提供的线程。使用ThreadFactory并将ThreadNameDeterminer设置为当前。

代码语言:javascript
复制
ThreadRenamingRunnable.setThreadNameDeterminer(ThreadNameDeterminer.CURRENT);

Netty (至少是3.5.0)将threadName从原始值更改为建议的值"New I/O Worker #X“。上面的代码片段确保它不会改变线程名称。名称由下面的ThreadFactory决定。

代码语言:javascript
复制
public class CustomThreadFactory implements ThreadFactory {
    private final AtomicInteger     threadIdSequence    = new AtomicInteger(0);
    private String                  threadNamePrefix    = "Netty-Worker-";

    public CustomThreadFactory() {
    }
    public CustomThreadFactory(String namePrefix) {
        this.threadNamePrefix = namePrefix;
    }

    @Override
    public Thread newThread(Runnable runnable) {
        Thread newThread = new Thread(runnable, threadNamePrefix + threadIdSequence.incrementAndGet());
        if (newThread.isDaemon()) {
            newThread.setDaemon(false);
        }
        if (newThread.getPriority() != Thread.NORM_PRIORITY) {
            newThread.setPriority(Thread.NORM_PRIORITY);
        }
        newThread.setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
            @Override
            public void uncaughtException(final Thread thread, final Throwable e) {
                System.err.println(thread + " threw exception: " + e.getMessage());
                e.printStackTrace();
            }
        });
        return newThread;
    }
}
票数 1
EN

Stack Overflow用户

发布于 2013-12-30 17:39:21

也许有更好的方法可以做到这一点,但这就是我让它工作的方式。多亏了这个blog

代码语言:javascript
复制
public class NettyPingPong {

    public static void main(String[] args) {

        final String WORKER_THREADNAME_PREFIX = "worker";
        NioWorkerPool workerPool = new NioWorkerPool(Executors.newCachedThreadPool(), 20, new ThreadNameDeterminer() {
            @Override
            public String determineThreadName(String currentThreadName,String proposedThreadName) throws Exception {
                StringBuilder sb = new StringBuilder(WORKER_THREADNAME_PREFIX);
                sb.append(currentThreadName.substring(currentThreadName.lastIndexOf('-')));
                return sb.toString();
            }
        });

        ServerBootstrap bootstrap = new ServerBootstrap(
                new NioServerSocketChannelFactory(
                Executors.newCachedThreadPool(),
                workerPool));

        bootstrap.setPipelineFactory(
                new ChannelPipelineFactory() {
            public ChannelPipeline getPipeline() throws Exception {
                return Channels.pipeline(
                        new LineBasedFrameDecoder(255, true, true),
                        new PongUpstreamHandler(),
                        new StringEncoder());

            }
        });

        bootstrap.bind(new InetSocketAddress(8899));
        out.println("im ready");
    }
}
票数 0
EN

Stack Overflow用户

发布于 2013-12-31 05:13:20

你可以传入一个ThreadFactory来命名你的线程。看一下我用来命名服务器线程的ThreadFactory。下面提供的是示例用法

代码语言:javascript
复制
serverBootstrap = new ServerBootstrap(
                            new NioServerSocketChannelFactory(Executors
                                .newCachedThreadPool(new NamedThreadFactory(
                                "TCP-Server-Boss")), Executors
                                .newCachedThreadPool(new NamedThreadFactory(
                                "TCP-Server-Worker"))));
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20836139

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档