From 78d8afe4614042cae0a43559d6467fc6900dfb35 Mon Sep 17 00:00:00 2001 From: QilongZhang Date: Mon, 8 Jul 2019 14:03:50 +0800 Subject: [PATCH] Destroy server (#198) * un-destroy server * destroy server * destroy server * add test case --- pom.xml | 2 +- sofa-boot-core/pom.xml | 4 +- .../boot/container/ServerConfigContainer.java | 39 +++++---- sofa-boot-plugin/pom.xml | 4 +- sofa-boot-samples/pom.xml | 4 +- sofa-boot-starter/pom.xml | 4 +- .../sofa/rpc/test/service/ServiceTest.java | 79 +++++++++++++++++++ 7 files changed, 106 insertions(+), 30 deletions(-) create mode 100644 sofa-boot-starter/src/test/java/com/alipay/sofa/rpc/test/service/ServiceTest.java diff --git a/pom.xml b/pom.xml index 32274e8..9b466e6 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ com.alipay.sofa sofa-rpc-boot-projects - 6.0.3 + 6.0.4-SNAPSHOT pom ${project.groupId}:${project.artifactId} diff --git a/sofa-boot-core/pom.xml b/sofa-boot-core/pom.xml index 46c78be..38c78ca 100644 --- a/sofa-boot-core/pom.xml +++ b/sofa-boot-core/pom.xml @@ -10,7 +10,7 @@ 4.0.0 rpc-sofa-boot-core - 6.0.3 + 6.0.4-SNAPSHOT https://github.com/sofastack/sofa-rpc @@ -23,7 +23,7 @@ 2.4.10 3.0.14 false - 5.5.5 + 5.5.7-SNAPSHOT 0.10 3.3 3.0.1 diff --git a/sofa-boot-core/src/main/java/com/alipay/sofa/rpc/boot/container/ServerConfigContainer.java b/sofa-boot-core/src/main/java/com/alipay/sofa/rpc/boot/container/ServerConfigContainer.java index a7a3cdb..8c92c98 100644 --- a/sofa-boot-core/src/main/java/com/alipay/sofa/rpc/boot/container/ServerConfigContainer.java +++ b/sofa-boot-core/src/main/java/com/alipay/sofa/rpc/boot/container/ServerConfigContainer.java @@ -24,6 +24,7 @@ import com.alipay.sofa.rpc.boot.log.SofaBootRpcLoggerFactory; import com.alipay.sofa.rpc.common.RpcConstants; import com.alipay.sofa.rpc.config.ServerConfig; +import com.alipay.sofa.rpc.server.Server; import com.alipay.sofa.rpc.server.bolt.BoltServer; import org.slf4j.Logger; import org.springframework.util.StringUtils; @@ -420,34 +421,30 @@ ServerConfig createDubboServerConfig() { * 释放所有 ServerConfig 对应的资源,并移除所有的 ServerConfig。 */ public void closeAllServer() { - if (boltServerConfig != null) { - boltServerConfig.destroy(); - boltServerConfig = null; - } - - if (restServerConfig != null) { - restServerConfig.destroy(); - restServerConfig = null; - } + destroyServerConfig(boltServerConfig); + destroyServerConfig(restServerConfig); + destroyServerConfig(dubboServerConfig); + destroyServerConfig(h2cServerConfig); - if (dubboServerConfig != null) { - dubboServerConfig.destroy(); - dubboServerConfig = null; + for (Map.Entry entry : customServerConfigs.entrySet()) { + final ServerConfig serverConfig = entry.getValue(); + destroyServerConfig(serverConfig); } - if (h2cServerConfig != null) { - h2cServerConfig.destroy(); - h2cServerConfig = null; - } + boltServerConfig = null; + restServerConfig = null; + dubboServerConfig = null; + h2cServerConfig = null; + customServerConfigs.clear(); + } - for (Map.Entry entry : customServerConfigs.entrySet()) { - final ServerConfig serverConfig = entry.getValue(); - if (serverConfig != null) { + private void destroyServerConfig(ServerConfig serverConfig) { + if (serverConfig != null) { + Server server = serverConfig.getServer(); + if (server != null && server.hasNoEntry()) { serverConfig.destroy(); } } - - customServerConfigs.clear(); } /** diff --git a/sofa-boot-plugin/pom.xml b/sofa-boot-plugin/pom.xml index 774a735..a1f7f06 100644 --- a/sofa-boot-plugin/pom.xml +++ b/sofa-boot-plugin/pom.xml @@ -10,11 +10,11 @@ 4.0.0 rpc-sofa-boot-plugin - 6.0.3 + 6.0.4-SNAPSHOT rpc-sofa-boot-plugin - 5.5.5 + 5.5.7-SNAPSHOT false 2.10.4 3.0.1 diff --git a/sofa-boot-samples/pom.xml b/sofa-boot-samples/pom.xml index cfba7f3..724e038 100644 --- a/sofa-boot-samples/pom.xml +++ b/sofa-boot-samples/pom.xml @@ -7,14 +7,14 @@ com.alipay.sofa sofa-rpc-boot-projects - 6.0.3 + 6.0.4-SNAPSHOT 2.4.10 3.0.14 3.1.4 - 6.0.3 + 6.0.4-SNAPSHOT 2.9.1 4.12 1.4.2.RELEASE diff --git a/sofa-boot-starter/pom.xml b/sofa-boot-starter/pom.xml index bd04c11..7257543 100644 --- a/sofa-boot-starter/pom.xml +++ b/sofa-boot-starter/pom.xml @@ -10,7 +10,7 @@ 4.0.0 rpc-sofa-boot-starter - 6.0.3 + 6.0.4-SNAPSHOT https://github.com/sofastack/sofa-rpc @@ -18,7 +18,7 @@ 1.8 UTF-8 false - 6.0.3 + 6.0.4-SNAPSHOT 3.3 3.0.1 3.0 diff --git a/sofa-boot-starter/src/test/java/com/alipay/sofa/rpc/test/service/ServiceTest.java b/sofa-boot-starter/src/test/java/com/alipay/sofa/rpc/test/service/ServiceTest.java new file mode 100644 index 0000000..69115de --- /dev/null +++ b/sofa-boot-starter/src/test/java/com/alipay/sofa/rpc/test/service/ServiceTest.java @@ -0,0 +1,79 @@ +/* + * 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.sofa.rpc.test.service; + +import com.alipay.rpc.common.service.facade.SampleService; +import com.alipay.sofa.rpc.boot.config.SofaBootRpcConfigConstants; +import com.alipay.sofa.rpc.boot.container.ServerConfigContainer; +import com.alipay.sofa.rpc.config.ServerConfig; +import com.alipay.sofa.runtime.api.annotation.SofaReferenceBinding; +import com.alipay.sofa.runtime.api.annotation.SofaService; +import com.alipay.sofa.runtime.api.annotation.SofaServiceBinding; +import org.junit.AfterClass; +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.test.annotation.DirtiesContext; +import org.springframework.test.context.junit4.SpringRunner; + +/** + * @author qilong.zql + * @since 6.0.4 + */ +@SpringBootTest +@RunWith(SpringRunner.class) +public class ServiceTest { + + @Autowired + private ServerConfigContainer serverConfigContainer; + private static ServerConfig serverConfig; + + @Test + @DirtiesContext + public void testService() { + Assert.assertNotNull(serverConfigContainer); + serverConfig = serverConfigContainer.getServerConfig(SofaBootRpcConfigConstants.RPC_PROTOCOL_BOLT); + Assert.assertNotNull(serverConfig); + Assert.assertNotNull(serverConfig.getServer()); + Assert.assertFalse(serverConfig.getServer().hasNoEntry()); + } + + @AfterClass + public static void afterClass() { + Assert.assertNull(serverConfig.getServer()); + } + + @Configuration + @EnableAutoConfiguration + static class ServiceTestConfiguration { + @Bean + @SofaService(bindings = { @SofaServiceBinding(bindingType = "bolt") }) + public SampleService sampleService() { + return new SampleService() { + @Override + public String echoStr(String name) { + return "sampleService"; + } + }; + } + } +} \ No newline at end of file