From 1e3b7d8273c0b4f651ead36fd7b01b397056e89e Mon Sep 17 00:00:00 2001 From: Trustin Lee Date: Mon, 24 Mar 2014 11:39:55 +0900 Subject: [PATCH] Replace LocalEventLoopGroup with DefaultEventLoopGroup Motivation: LocalEventLoopGroup and LocalEventLoop are not really special for LocalChannels. It can be used for other channel implementations as long as they don't require special handling. Modifications: - Add DefaultEventLoopGroup and DefaultEventLoop - Deprecate LocalEventLoopGroup and make it extend DefaultEventLoopGroup - Add DefaultEventLoop and remove LocalEventLoop - Fix inspector warnings Result: - Better class names. --- .../io/netty/example/localecho/LocalEcho.java | 4 +- ...alEventLoop.java => DefaultEventLoop.java} | 29 ++++++++-- .../netty/channel/DefaultEventLoopGroup.java | 56 +++++++++++++++++++ .../channel/local/LocalEventLoopGroup.java | 21 ++----- .../io/netty/bootstrap/BootstrapTest.java | 10 ++-- .../io/netty/channel/BaseChannelTest.java | 18 +++--- .../channel/DefaultChannelPipelineTest.java | 3 +- .../netty/channel/local/LocalChannelTest.java | 11 ++-- .../local/LocalTransportThreadModelTest.java | 7 ++- .../local/LocalTransportThreadModelTest2.java | 7 ++- .../local/LocalTransportThreadModelTest3.java | 9 +-- 11 files changed, 123 insertions(+), 52 deletions(-) rename transport/src/main/java/io/netty/channel/{local/LocalEventLoop.java => DefaultEventLoop.java} (56%) create mode 100644 transport/src/main/java/io/netty/channel/DefaultEventLoopGroup.java diff --git a/example/src/main/java/io/netty/example/localecho/LocalEcho.java b/example/src/main/java/io/netty/example/localecho/LocalEcho.java index cd707fcdc0..bc9aca12ff 100644 --- a/example/src/main/java/io/netty/example/localecho/LocalEcho.java +++ b/example/src/main/java/io/netty/example/localecho/LocalEcho.java @@ -20,10 +20,10 @@ import io.netty.bootstrap.ServerBootstrap; import io.netty.channel.Channel; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelInitializer; +import io.netty.channel.DefaultEventLoopGroup; import io.netty.channel.EventLoopGroup; import io.netty.channel.local.LocalAddress; import io.netty.channel.local.LocalChannel; -import io.netty.channel.local.LocalEventLoopGroup; import io.netty.channel.local.LocalServerChannel; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.handler.logging.LogLevel; @@ -44,7 +44,7 @@ public class LocalEcho { // Address to bind on / connect to. final LocalAddress addr = new LocalAddress(port); - EventLoopGroup serverGroup = new LocalEventLoopGroup(); + EventLoopGroup serverGroup = new DefaultEventLoopGroup(); EventLoopGroup clientGroup = new NioEventLoopGroup(); // NIO event loops are also OK try { // Note that we can use any event loop to ensure certain local channels diff --git a/transport/src/main/java/io/netty/channel/local/LocalEventLoop.java b/transport/src/main/java/io/netty/channel/DefaultEventLoop.java similarity index 56% rename from transport/src/main/java/io/netty/channel/local/LocalEventLoop.java rename to transport/src/main/java/io/netty/channel/DefaultEventLoop.java index b9aa21976c..53d2e3b12b 100644 --- a/transport/src/main/java/io/netty/channel/local/LocalEventLoop.java +++ b/transport/src/main/java/io/netty/channel/DefaultEventLoop.java @@ -13,15 +13,36 @@ * License for the specific language governing permissions and limitations * under the License. */ -package io.netty.channel.local; +package io.netty.channel; -import io.netty.channel.SingleThreadEventLoop; +import io.netty.util.concurrent.DefaultThreadFactory; import java.util.concurrent.Executor; +import java.util.concurrent.ThreadFactory; -final class LocalEventLoop extends SingleThreadEventLoop { +public class DefaultEventLoop extends SingleThreadEventLoop { - LocalEventLoop(LocalEventLoopGroup parent, Executor executor) { + public DefaultEventLoop() { + this((EventLoopGroup) null); + } + + public DefaultEventLoop(ThreadFactory threadFactory) { + this(null, threadFactory); + } + + public DefaultEventLoop(Executor executor) { + this(null, executor); + } + + public DefaultEventLoop(EventLoopGroup parent) { + this(parent, new DefaultThreadFactory(DefaultEventLoop.class)); + } + + public DefaultEventLoop(EventLoopGroup parent, ThreadFactory threadFactory) { + super(parent, threadFactory, true); + } + + public DefaultEventLoop(EventLoopGroup parent, Executor executor) { super(parent, executor, true); } diff --git a/transport/src/main/java/io/netty/channel/DefaultEventLoopGroup.java b/transport/src/main/java/io/netty/channel/DefaultEventLoopGroup.java new file mode 100644 index 0000000000..6e8ba13452 --- /dev/null +++ b/transport/src/main/java/io/netty/channel/DefaultEventLoopGroup.java @@ -0,0 +1,56 @@ +/* + * Copyright 2012 The Netty Project + * + * The Netty Project licenses this file to you under the Apache License, + * version 2.0 (the "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ +package io.netty.channel; + +import java.util.concurrent.Executor; +import java.util.concurrent.ThreadFactory; + +/** + * {@link MultithreadEventLoopGroup} which must be used for the local transport. + */ +public class DefaultEventLoopGroup extends MultithreadEventLoopGroup { + + /** + * Create a new instance with the default number of threads. + */ + public DefaultEventLoopGroup() { + this(0); + } + + /** + * Create a new instance + * + * @param nThreads the number of threads to use + */ + public DefaultEventLoopGroup(int nThreads) { + this(nThreads, null); + } + + /** + * Create a new instance + * + * @param nThreads the number of threads to use + * @param threadFactory the {@link ThreadFactory} or {@code null} to use the default + */ + public DefaultEventLoopGroup(int nThreads, ThreadFactory threadFactory) { + super(nThreads, threadFactory); + } + + @Override + protected EventLoop newChild(Executor executor, Object... args) throws Exception { + return new DefaultEventLoop(this, executor); + } +} diff --git a/transport/src/main/java/io/netty/channel/local/LocalEventLoopGroup.java b/transport/src/main/java/io/netty/channel/local/LocalEventLoopGroup.java index 2e0a56d413..2bd3ff611e 100644 --- a/transport/src/main/java/io/netty/channel/local/LocalEventLoopGroup.java +++ b/transport/src/main/java/io/netty/channel/local/LocalEventLoopGroup.java @@ -15,23 +15,20 @@ */ package io.netty.channel.local; -import io.netty.channel.MultithreadEventLoopGroup; -import io.netty.util.concurrent.EventExecutor; +import io.netty.channel.DefaultEventLoopGroup; -import java.util.concurrent.Executor; import java.util.concurrent.ThreadFactory; /** - * {@link MultithreadEventLoopGroup} which must be used for the local transport. + * @deprecated Use {@link DefaultEventLoopGroup} instead. */ -public class LocalEventLoopGroup extends MultithreadEventLoopGroup { +@Deprecated +public class LocalEventLoopGroup extends DefaultEventLoopGroup { /** * Create a new instance with the default number of threads. */ - public LocalEventLoopGroup() { - this(0); - } + public LocalEventLoopGroup() { } /** * Create a new instance @@ -39,7 +36,7 @@ public class LocalEventLoopGroup extends MultithreadEventLoopGroup { * @param nThreads the number of threads to use */ public LocalEventLoopGroup(int nThreads) { - this(nThreads, null); + super(nThreads); } /** @@ -51,10 +48,4 @@ public class LocalEventLoopGroup extends MultithreadEventLoopGroup { public LocalEventLoopGroup(int nThreads, ThreadFactory threadFactory) { super(nThreads, threadFactory); } - - @Override - protected EventExecutor newChild( - Executor executor, Object... args) throws Exception { - return new LocalEventLoop(this, executor); - } } diff --git a/transport/src/test/java/io/netty/bootstrap/BootstrapTest.java b/transport/src/test/java/io/netty/bootstrap/BootstrapTest.java index efcadfb6a1..78ce3bbb20 100644 --- a/transport/src/test/java/io/netty/bootstrap/BootstrapTest.java +++ b/transport/src/test/java/io/netty/bootstrap/BootstrapTest.java @@ -19,10 +19,10 @@ package io.netty.bootstrap; import io.netty.channel.ChannelHandler.Sharable; import io.netty.channel.ChannelInboundHandler; import io.netty.channel.ChannelInboundHandlerAdapter; +import io.netty.channel.DefaultEventLoopGroup; import io.netty.channel.EventLoopGroup; import io.netty.channel.local.LocalAddress; import io.netty.channel.local.LocalChannel; -import io.netty.channel.local.LocalEventLoopGroup; import io.netty.util.concurrent.Future; import org.junit.Test; @@ -33,8 +33,8 @@ public class BootstrapTest { @Test(timeout = 10000) public void testBindDeadLock() throws Exception { - EventLoopGroup groupA = new LocalEventLoopGroup(1); - EventLoopGroup groupB = new LocalEventLoopGroup(1); + EventLoopGroup groupA = new DefaultEventLoopGroup(1); + EventLoopGroup groupB = new DefaultEventLoopGroup(1); try { ChannelInboundHandler dummyHandler = new DummyHandler(); @@ -81,8 +81,8 @@ public class BootstrapTest { @Test(timeout = 10000) public void testConnectDeadLock() throws Exception { - EventLoopGroup groupA = new LocalEventLoopGroup(1); - EventLoopGroup groupB = new LocalEventLoopGroup(1); + EventLoopGroup groupA = new DefaultEventLoopGroup(1); + EventLoopGroup groupB = new DefaultEventLoopGroup(1); try { ChannelInboundHandler dummyHandler = new DummyHandler(); diff --git a/transport/src/test/java/io/netty/channel/BaseChannelTest.java b/transport/src/test/java/io/netty/channel/BaseChannelTest.java index 907ed2def4..6c957f96f1 100644 --- a/transport/src/test/java/io/netty/channel/BaseChannelTest.java +++ b/transport/src/test/java/io/netty/channel/BaseChannelTest.java @@ -16,25 +16,25 @@ package io.netty.channel; -import static org.junit.Assert.assertEquals; import io.netty.bootstrap.Bootstrap; import io.netty.bootstrap.ServerBootstrap; import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; import io.netty.channel.local.LocalChannel; -import io.netty.channel.local.LocalEventLoopGroup; import io.netty.channel.local.LocalServerChannel; +import static org.junit.Assert.*; + class BaseChannelTest { private final LoggingHandler loggingHandler; BaseChannelTest() { - this.loggingHandler = new LoggingHandler(); + loggingHandler = new LoggingHandler(); } ServerBootstrap getLocalServerBootstrap() { - EventLoopGroup serverGroup = new LocalEventLoopGroup(); + EventLoopGroup serverGroup = new DefaultEventLoopGroup(); ServerBootstrap sb = new ServerBootstrap(); sb.group(serverGroup); sb.channel(LocalServerChannel.class); @@ -48,12 +48,12 @@ class BaseChannelTest { } Bootstrap getLocalClientBootstrap() { - EventLoopGroup clientGroup = new LocalEventLoopGroup(); + EventLoopGroup clientGroup = new DefaultEventLoopGroup(); Bootstrap cb = new Bootstrap(); cb.channel(LocalChannel.class); cb.group(clientGroup); - cb.handler(this.loggingHandler); + cb.handler(loggingHandler); return cb; } @@ -65,16 +65,16 @@ class BaseChannelTest { } void assertLog(String expected) { - String actual = this.loggingHandler.getLog(); + String actual = loggingHandler.getLog(); assertEquals(expected, actual); } void clearLog() { - this.loggingHandler.clear(); + loggingHandler.clear(); } void setInterest(LoggingHandler.Event... events) { - this.loggingHandler.setInterest(events); + loggingHandler.setInterest(events); } } diff --git a/transport/src/test/java/io/netty/channel/DefaultChannelPipelineTest.java b/transport/src/test/java/io/netty/channel/DefaultChannelPipelineTest.java index 086d0d433c..29efd28c3f 100644 --- a/transport/src/test/java/io/netty/channel/DefaultChannelPipelineTest.java +++ b/transport/src/test/java/io/netty/channel/DefaultChannelPipelineTest.java @@ -21,7 +21,6 @@ import io.netty.bootstrap.ServerBootstrap; import io.netty.channel.ChannelHandler.Sharable; import io.netty.channel.local.LocalAddress; import io.netty.channel.local.LocalChannel; -import io.netty.channel.local.LocalEventLoopGroup; import io.netty.channel.local.LocalServerChannel; import io.netty.util.AbstractReferenceCounted; import io.netty.util.ReferenceCountUtil; @@ -43,7 +42,7 @@ import static org.junit.Assert.*; public class DefaultChannelPipelineTest { - private static final EventLoopGroup group = new LocalEventLoopGroup(1); + private static final EventLoopGroup group = new DefaultEventLoopGroup(1); private Channel self; private Channel peer; diff --git a/transport/src/test/java/io/netty/channel/local/LocalChannelTest.java b/transport/src/test/java/io/netty/channel/local/LocalChannelTest.java index f109040a2e..c72729b5b9 100644 --- a/transport/src/test/java/io/netty/channel/local/LocalChannelTest.java +++ b/transport/src/test/java/io/netty/channel/local/LocalChannelTest.java @@ -22,6 +22,7 @@ import io.netty.channel.Channel; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelInboundHandlerAdapter; import io.netty.channel.ChannelInitializer; +import io.netty.channel.DefaultEventLoopGroup; import io.netty.channel.EventLoopGroup; import io.netty.channel.SimpleChannelInboundHandler; import io.netty.util.internal.logging.InternalLogger; @@ -43,8 +44,8 @@ public class LocalChannelTest { @Test public void testLocalAddressReuse() throws Exception { for (int i = 0; i < 2; i ++) { - EventLoopGroup clientGroup = new LocalEventLoopGroup(); - EventLoopGroup serverGroup = new LocalEventLoopGroup(); + EventLoopGroup clientGroup = new DefaultEventLoopGroup(); + EventLoopGroup serverGroup = new DefaultEventLoopGroup(); LocalAddress addr = new LocalAddress(LOCAL_ADDR_ID); Bootstrap cb = new Bootstrap(); ServerBootstrap sb = new ServerBootstrap(); @@ -97,8 +98,8 @@ public class LocalChannelTest { @Test public void testWriteFailsFastOnClosedChannel() throws Exception { - EventLoopGroup clientGroup = new LocalEventLoopGroup(); - EventLoopGroup serverGroup = new LocalEventLoopGroup(); + EventLoopGroup clientGroup = new DefaultEventLoopGroup(); + EventLoopGroup serverGroup = new DefaultEventLoopGroup(); LocalAddress addr = new LocalAddress(LOCAL_ADDR_ID); Bootstrap cb = new Bootstrap(); ServerBootstrap sb = new ServerBootstrap(); @@ -147,7 +148,7 @@ public class LocalChannelTest { @Test public void testServerCloseChannelSameEventLoop() throws Exception { LocalAddress addr = new LocalAddress(LOCAL_ADDR_ID); - LocalEventLoopGroup group = new LocalEventLoopGroup(1); + EventLoopGroup group = new DefaultEventLoopGroup(1); final CountDownLatch latch = new CountDownLatch(1); ServerBootstrap sb = new ServerBootstrap() .group(group) diff --git a/transport/src/test/java/io/netty/channel/local/LocalTransportThreadModelTest.java b/transport/src/test/java/io/netty/channel/local/LocalTransportThreadModelTest.java index f69bad9ba8..4ac3047a32 100644 --- a/transport/src/test/java/io/netty/channel/local/LocalTransportThreadModelTest.java +++ b/transport/src/test/java/io/netty/channel/local/LocalTransportThreadModelTest.java @@ -24,6 +24,7 @@ import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelInboundHandlerAdapter; import io.netty.channel.ChannelInitializer; import io.netty.channel.ChannelPromise; +import io.netty.channel.DefaultEventLoopGroup; import io.netty.channel.EventLoopGroup; import io.netty.util.ReferenceCountUtil; import io.netty.util.concurrent.DefaultEventExecutorGroup; @@ -49,7 +50,7 @@ public class LocalTransportThreadModelTest { @BeforeClass public static void init() { // Configure a test server - group = new LocalEventLoopGroup(); + group = new DefaultEventLoopGroup(); ServerBootstrap sb = new ServerBootstrap(); sb.group(group) .channel(LocalServerChannel.class) @@ -84,7 +85,7 @@ public class LocalTransportThreadModelTest { @Test(timeout = 5000) public void testStagedExecution() throws Throwable { - EventLoopGroup l = new LocalEventLoopGroup(4, new DefaultThreadFactory("l")); + EventLoopGroup l = new DefaultEventLoopGroup(4, new DefaultThreadFactory("l")); EventExecutorGroup e1 = new DefaultEventExecutorGroup(4, new DefaultThreadFactory("e1")); EventExecutorGroup e2 = new DefaultEventExecutorGroup(4, new DefaultThreadFactory("e2")); ThreadNameAuditor h1 = new ThreadNameAuditor(); @@ -227,7 +228,7 @@ public class LocalTransportThreadModelTest { @Test(timeout = 30000) @Ignore public void testConcurrentMessageBufferAccess() throws Throwable { - EventLoopGroup l = new LocalEventLoopGroup(4, new DefaultThreadFactory("l")); + EventLoopGroup l = new DefaultEventLoopGroup(4, new DefaultThreadFactory("l")); EventExecutorGroup e1 = new DefaultEventExecutorGroup(4, new DefaultThreadFactory("e1")); EventExecutorGroup e2 = new DefaultEventExecutorGroup(4, new DefaultThreadFactory("e2")); EventExecutorGroup e3 = new DefaultEventExecutorGroup(4, new DefaultThreadFactory("e3")); diff --git a/transport/src/test/java/io/netty/channel/local/LocalTransportThreadModelTest2.java b/transport/src/test/java/io/netty/channel/local/LocalTransportThreadModelTest2.java index 45218327aa..4d2aa46fa2 100644 --- a/transport/src/test/java/io/netty/channel/local/LocalTransportThreadModelTest2.java +++ b/transport/src/test/java/io/netty/channel/local/LocalTransportThreadModelTest2.java @@ -22,6 +22,7 @@ import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelHandler.Sharable; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelInboundHandlerAdapter; +import io.netty.channel.DefaultEventLoopGroup; import io.netty.util.ReferenceCountUtil; import org.junit.Test; @@ -40,14 +41,14 @@ public class LocalTransportThreadModelTest2 { ServerBootstrap serverBootstrap = new ServerBootstrap(); LocalHander serverHandler = new LocalHander("SERVER"); serverBootstrap - .group(new LocalEventLoopGroup(), new LocalEventLoopGroup()) + .group(new DefaultEventLoopGroup(), new DefaultEventLoopGroup()) .channel(LocalServerChannel.class) .childHandler(serverHandler); Bootstrap clientBootstrap = new Bootstrap(); LocalHander clientHandler = new LocalHander("CLIENT"); clientBootstrap - .group(new LocalEventLoopGroup()) + .group(new DefaultEventLoopGroup()) .channel(LocalChannel.class) .remoteAddress(new LocalAddress(LOCAL_CHANNEL)).handler(clientHandler); @@ -100,7 +101,7 @@ public class LocalTransportThreadModelTest2 { public final AtomicInteger count = new AtomicInteger(0); - public LocalHander(String name) { + LocalHander(String name) { this.name = name; } diff --git a/transport/src/test/java/io/netty/channel/local/LocalTransportThreadModelTest3.java b/transport/src/test/java/io/netty/channel/local/LocalTransportThreadModelTest3.java index bfb3b4bd67..2c636b5b24 100644 --- a/transport/src/test/java/io/netty/channel/local/LocalTransportThreadModelTest3.java +++ b/transport/src/test/java/io/netty/channel/local/LocalTransportThreadModelTest3.java @@ -23,6 +23,7 @@ import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelInboundHandlerAdapter; import io.netty.channel.ChannelInitializer; import io.netty.channel.ChannelPromise; +import io.netty.channel.DefaultEventLoopGroup; import io.netty.channel.EventLoopGroup; import io.netty.util.ReferenceCountUtil; import io.netty.util.concurrent.DefaultEventExecutorGroup; @@ -62,7 +63,7 @@ public class LocalTransportThreadModelTest3 { @BeforeClass public static void init() { // Configure a test server - group = new LocalEventLoopGroup(); + group = new DefaultEventLoopGroup(); ServerBootstrap sb = new ServerBootstrap(); sb.group(group) .channel(LocalServerChannel.class) @@ -116,7 +117,7 @@ public class LocalTransportThreadModelTest3 { } private static void testConcurrentAddRemove(boolean inbound) throws Exception { - EventLoopGroup l = new LocalEventLoopGroup(4, new DefaultThreadFactory("l")); + EventLoopGroup l = new DefaultEventLoopGroup(4, new DefaultThreadFactory("l")); EventExecutorGroup e1 = new DefaultEventExecutorGroup(4, new DefaultThreadFactory("e1")); EventExecutorGroup e2 = new DefaultEventExecutorGroup(4, new DefaultThreadFactory("e2")); EventExecutorGroup e3 = new DefaultEventExecutorGroup(4, new DefaultThreadFactory("e3")); @@ -210,7 +211,7 @@ public class LocalTransportThreadModelTest3 { for (;;) { EventType event = events.poll(); if (event == null) { - Assert.assertTrue("Missing events:" + expectedEvents.toString(), expectedEvents.isEmpty()); + Assert.assertTrue("Missing events:" + expectedEvents, expectedEvents.isEmpty()); break; } Assert.assertEquals(event, expectedEvents.poll()); @@ -258,7 +259,7 @@ public class LocalTransportThreadModelTest3 { private final Queue events; private final boolean inbound; - public EventRecorder(Queue events, boolean inbound) { + EventRecorder(Queue events, boolean inbound) { this.events = events; this.inbound = inbound; }