Skip to content

Commit

Permalink
Tuning/threadpool exit (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
xmtsui authored May 30, 2018
1 parent fd27931 commit 97d65ac
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 6 deletions.
6 changes: 5 additions & 1 deletion src/main/java/com/alipay/remoting/RemotingServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ public boolean start() {
return this.doStart();
} catch (Throwable t) {
started.set(false);
this.stop();
logger.error("ERROR: Failed to start the Server!", t);
return false;
}
Expand All @@ -92,6 +93,7 @@ public boolean start(String ip) {
return this.doStart(ip);
} catch (Throwable t) {
started.set(false);
this.stop();
logger.error("ERROR: Failed to start the Server!", t);
return false;
}
Expand All @@ -110,7 +112,9 @@ public boolean start(String ip) {
* <li>If you need, you should destroy it, and instantiate another one.
*/
public void stop() {
if (started.compareAndSet(true, false)) {
if (inited.get() || started.get()) {
inited.compareAndSet(true, false);
started.compareAndSet(true, false);
this.doStop();
} else {
throw new IllegalStateException("ERROR: The server has already stopped!");
Expand Down
13 changes: 11 additions & 2 deletions src/main/java/com/alipay/remoting/rpc/RpcServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,13 @@
/**
* Server for Rpc.
*
* Usage:
* You can initialize RpcServer with one of the three constructors:
* {@link #RpcServer(int)}, {@link #RpcServer(int, boolean)}, {@link #RpcServer(int, boolean, boolean)}
* Then call start() to start a rpc server, and call stop() to stop a rpc server.
* When rpc server has been stopped, it can not be start again.
* You should create another instance of RpcServer to start if you need.
*
* @author jiangping
* @version $Id: RpcServer.java, v 0.1 2015-8-31 PM5:22:22 tao Exp $
*/
Expand Down Expand Up @@ -98,7 +105,7 @@ public class RpcServer extends RemotingServer {
private ConcurrentHashMap<String, UserProcessor<?>> userProcessors = new ConcurrentHashMap<String, UserProcessor<?>>(
4);

/** boss event loop group*/
/** boss event loop group, boss group should not be daemon, need shutdown manually */
private final EventLoopGroup bossGroup = new NioEventLoopGroup(
1,
new NamedThreadFactory(
Expand Down Expand Up @@ -282,7 +289,9 @@ protected boolean doStart(String ip) throws InterruptedException {
*/
@Override
protected void doStop() {
this.channelFuture.channel().close();
if (null != this.channelFuture) {
this.channelFuture.channel().close();
}
if (this.globalSwitch.isOn(GlobalSwitch.SERVER_SYNC_STOP)) {
this.bossGroup.shutdownGracefully().awaitUninterruptibly();
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public static void main(String[] args) {
logger.error(errMsg, e);
Assert.fail(errMsg);
} catch (InterruptedException e) {
e.printStackTrace();
logger.error("interrupted!");
}
client.shutdown();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,12 @@ public RpcServerDemoByMain() {
// 3. register user processor for client request
server.registerUserProcessor(serverUserProcessor);
// 4. server start
server.start();
System.out.println("server start ok!");
if (server.start()) {
System.out.println("server start ok!");
} else {
System.out.println("server start failed!");
}
// server.getRpcServer().stop();
}

public static void main(String[] args) {
Expand Down
83 changes: 83 additions & 0 deletions src/test/java/com/alipay/remoting/rpc/RpcServerTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF 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 com.alipay.remoting.rpc;

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**

* test rpc server and stop logic
*
* @author tsui 

* @version $Id: RpcServerTest.java, v 0.1 2018-05-29 15:27 tsui Exp $$

*/
public class RpcServerTest {
static Logger logger = LoggerFactory.getLogger(RpcServerTest.class);

@Before
public void init() {
}

@After
public void stop() {
}

@Test
public void doTestStartAndStop() {
doTestStartAndStop(true);
doTestStartAndStop(false);
}

private void doTestStartAndStop(boolean syncStop) {
// 1. start a rpc server successfully
RpcServer rpcServer1 = new RpcServer(1111, false, syncStop);
if (!rpcServer1.start()) {
Assert.fail("Should not reach here");
logger.warn("start fail");
} else {
logger.warn("start success");
}

// 2. start a rpc server with the same port number failed
RpcServer rpcServer2 = new RpcServer(1111, false, syncStop);
if (!rpcServer2.start()) {
logger.warn("start fail");
} else {
Assert.fail("Should not reach here");
logger.warn("start success");
}

// 3. stop the first rpc server successfully
try {
rpcServer1.stop();
} catch (IllegalStateException e) {
Assert.fail("Should not reach here");
}

// 4. stop the second rpc server failed, for if start failed, stop method will be called automatically
try {
rpcServer2.stop();
Assert.fail("Should not reach here");
} catch (IllegalStateException e) {
// expect
}
}
}
33 changes: 33 additions & 0 deletions src/test/java/com/alipay/remoting/util/ThreadTestUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF 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 com.alipay.remoting.util;

/**

* utils of thread operations
*
* @author tsui 

* @version $Id: ThreadTestUtils.java, v 0.1 2018-05-29 15:29 tsui Exp $$

*/
public class ThreadTestUtils {
public static void sleep(long duration) {
try {
Thread.sleep(duration);
} catch (InterruptedException e) {
//ignore
}
}
}

0 comments on commit 97d65ac

Please sign in to comment.