首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么Netty不执行我预定的任务?

为什么Netty不执行我预定的任务?
EN

Stack Overflow用户
提问于 2016-07-16 15:26:19
回答 1查看 537关注 0票数 0

使用Java 8和Netty 4.1.1.Final,我本来希望下面的测试用例能够成功,但它超时了。我不懂的是什么?nettys事件循环和任务调度?

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


CountDownLatch latch;

TimerHandler handler;

static class TimerHandler extends ChannelInboundHandlerAdapter {

    ChannelHandlerContext ctx;

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        super.channelActive(ctx);
        this.ctx = ctx;
    }

    private void timeout(final long ms) {
        ctx.executor().schedule(() -> {
            ctx.fireUserEventTriggered(ms);
        }, ms, TimeUnit.MILLISECONDS);
    }

}

static class TimeoutReactor extends ChannelInboundHandlerAdapter {
    CountDownLatch latch;

    public TimeoutReactor(CountDownLatch latch) {
        super();
        this.latch = latch;
    }

    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
        System.out.println("userEventTriggered");
        latch.countDown();
        super.userEventTriggered(ctx, evt);
    }

}

@Before
public void setUp() throws Exception {
    latch = new CountDownLatch(2);
    handler = new TimerHandler();
    TimeoutReactor reactor = new TimeoutReactor(latch);
    new EmbeddedChannel(handler, reactor);
}

@Test(timeout = 1000)
public void test() throws InterruptedException {

    handler.timeout(30);
    handler.timeout(20);
    latch.await();
}

}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-07-18 05:31:13

这是因为EmbeddedChannel不是真正的通道实现,主要用于测试和嵌入式ChannelHandlers。您需要在给定的时间框架后调用"runPendingTasks()“来运行它。如果您使用“真正的”通道实现,它将在没有任何额外方法调用的情况下工作。

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38412579

复制
相关文章

相似问题

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