ARTEMIS-915 WebComponent stopped when backup failback

This commit is contained in:
Howard Gao 2017-01-12 10:11:10 +08:00
parent e4821be038
commit 07ea08a845
7 changed files with 103 additions and 11 deletions

View File

@ -134,7 +134,7 @@ public class Run extends LockAbstract {
if (file.exists()) { if (file.exists()) {
try { try {
try { try {
server.stop(); server.stop(true);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -155,7 +155,7 @@ public class Run extends LockAbstract {
@Override @Override
public void run() { public void run() {
try { try {
server.stop(); server.stop(true);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }

View File

@ -16,10 +16,10 @@
*/ */
package org.apache.activemq.artemis.components; package org.apache.activemq.artemis.components;
import org.apache.activemq.artemis.core.server.ActiveMQComponent; import org.apache.activemq.artemis.core.server.ServiceComponent;
import org.apache.activemq.artemis.dto.ComponentDTO; import org.apache.activemq.artemis.dto.ComponentDTO;
public interface ExternalComponent extends ActiveMQComponent { public interface ExternalComponent extends ServiceComponent {
void configure(ComponentDTO config, String artemisInstance, String artemisHome) throws Exception; void configure(ComponentDTO config, String artemisInstance, String artemisHome) throws Exception;
} }

View File

@ -16,13 +16,13 @@
*/ */
package org.apache.activemq.artemis.integration; package org.apache.activemq.artemis.integration;
import org.apache.activemq.artemis.core.server.ActiveMQComponent;
import org.apache.activemq.artemis.core.server.ActiveMQServer; import org.apache.activemq.artemis.core.server.ActiveMQServer;
import org.apache.activemq.artemis.core.server.ServiceComponent;
/** /**
* A Broker os a set of ActiveMQComponents that create a Server, for instance core and jms. * A Broker os a set of ActiveMQComponents that create a Server, for instance core and jms.
*/ */
public interface Broker extends ActiveMQComponent { public interface Broker extends ServiceComponent {
ActiveMQServer getServer(); ActiveMQServer getServer();
} }

View File

@ -28,6 +28,7 @@ import org.apache.activemq.artemis.core.config.impl.FileConfiguration;
import org.apache.activemq.artemis.core.server.ActiveMQComponent; import org.apache.activemq.artemis.core.server.ActiveMQComponent;
import org.apache.activemq.artemis.core.server.ActiveMQServer; import org.apache.activemq.artemis.core.server.ActiveMQServer;
import org.apache.activemq.artemis.core.server.RoutingType; import org.apache.activemq.artemis.core.server.RoutingType;
import org.apache.activemq.artemis.core.server.ServiceComponent;
import org.apache.activemq.artemis.dto.ServerDTO; import org.apache.activemq.artemis.dto.ServerDTO;
import org.apache.activemq.artemis.integration.bootstrap.ActiveMQBootstrapLogger; import org.apache.activemq.artemis.integration.bootstrap.ActiveMQBootstrapLogger;
import org.apache.activemq.artemis.jms.server.config.JMSQueueConfiguration; import org.apache.activemq.artemis.jms.server.config.JMSQueueConfiguration;
@ -113,14 +114,23 @@ public class FileBroker implements Broker {
@Override @Override
public void stop() throws Exception { public void stop() throws Exception {
stop(false);
}
@Override
public void stop(boolean isShutdown) throws Exception {
if (!started) { if (!started) {
return; return;
} }
ActiveMQComponent[] mqComponents = new ActiveMQComponent[components.size()]; ActiveMQComponent[] mqComponents = new ActiveMQComponent[components.size()];
components.values().toArray(mqComponents); components.values().toArray(mqComponents);
for (int i = mqComponents.length - 1; i >= 0; i--) { for (int i = mqComponents.length - 1; i >= 0; i--) {
if (mqComponents[i] instanceof ServiceComponent) {
((ServiceComponent)mqComponents[i]).stop(isShutdown);
} else {
mqComponents[i].stop(); mqComponents[i].stop();
} }
}
started = false; started = false;
} }
@ -151,4 +161,5 @@ public class FileBroker implements Broker {
public ActiveMQServer getServer() { public ActiveMQServer getServer() {
return (ActiveMQServer) components.get("core"); return (ActiveMQServer) components.get("core");
} }
} }

View File

@ -0,0 +1,25 @@
/*
* 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 org.apache.activemq.artemis.core.server;
/**
* A Component that needs to know the stop reason.
*/
public interface ServiceComponent extends ActiveMQComponent {
void stop(boolean isShutdown) throws Exception;
}

View File

@ -124,6 +124,9 @@ public class WebServerComponent implements ExternalComponent {
@Override @Override
public void start() throws Exception { public void start() throws Exception {
if (isStarted()) {
return;
}
server.start(); server.start();
ActiveMQWebLogger.LOGGER.webserverStarted(webServerConfig.bind); ActiveMQWebLogger.LOGGER.webserverStarted(webServerConfig.bind);
if (jolokiaUrl != null) { if (jolokiaUrl != null) {
@ -131,8 +134,7 @@ public class WebServerComponent implements ExternalComponent {
} }
} }
@Override public void internalStop() throws Exception {
public void stop() throws Exception {
server.stop(); server.stop();
if (webContexts != null) { if (webContexts != null) {
File tmpdir = null; File tmpdir = null;
@ -170,4 +172,16 @@ public class WebServerComponent implements ExternalComponent {
handlers.addHandler(webapp); handlers.addHandler(webapp);
return webapp; return webapp;
} }
@Override
public void stop() throws Exception {
stop(false);
}
@Override
public void stop(boolean isShutdown) throws Exception {
if (isShutdown) {
internalStop();
}
}
} }

View File

@ -95,7 +95,49 @@ public class WebServerComponentTest extends Assert {
// Wait for the server to close the connection. // Wait for the server to close the connection.
ch.close(); ch.close();
Assert.assertTrue(webServerComponent.isStarted()); Assert.assertTrue(webServerComponent.isStarted());
webServerComponent.stop(true);
Assert.assertFalse(webServerComponent.isStarted());
}
@Test
public void testComponentStopBehavior() throws Exception {
WebServerDTO webServerDTO = new WebServerDTO();
webServerDTO.bind = "http://localhost:8161";
webServerDTO.path = "webapps";
WebServerComponent webServerComponent = new WebServerComponent();
Assert.assertFalse(webServerComponent.isStarted());
webServerComponent.configure(webServerDTO, "./src/test/resources/", "./src/test/resources/");
webServerComponent.start();
// Make the connection attempt.
CountDownLatch latch = new CountDownLatch(1);
final ClientHandler clientHandler = new ClientHandler(latch);
bootstrap.group(group).channel(NioSocketChannel.class).handler(new ChannelInitializer() {
@Override
protected void initChannel(Channel ch) throws Exception {
ch.pipeline().addLast(new HttpClientCodec());
ch.pipeline().addLast(clientHandler);
}
});
Channel ch = bootstrap.connect("localhost", 8161).sync().channel();
URI uri = new URI(URL);
// Prepare the HTTP request.
HttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, uri.getRawPath());
request.headers().set(HttpHeaders.Names.HOST, "localhost");
// Send the HTTP request.
ch.writeAndFlush(request);
assertTrue(latch.await(5, TimeUnit.SECONDS));
assertEquals(clientHandler.body, "12345");
// Wait for the server to close the connection.
ch.close();
Assert.assertTrue(webServerComponent.isStarted());
//usual stop won't actually stop it
webServerComponent.stop(); webServerComponent.stop();
assertTrue(webServerComponent.isStarted());
webServerComponent.stop(true);
Assert.assertFalse(webServerComponent.isStarted()); Assert.assertFalse(webServerComponent.isStarted());
} }
@ -145,7 +187,7 @@ public class WebServerComponentTest extends Assert {
// Wait for the server to close the connection. // Wait for the server to close the connection.
ch.close(); ch.close();
Assert.assertTrue(webServerComponent.isStarted()); Assert.assertTrue(webServerComponent.isStarted());
webServerComponent.stop(); webServerComponent.stop(true);
Assert.assertFalse(webServerComponent.isStarted()); Assert.assertFalse(webServerComponent.isStarted());
} }
@ -198,7 +240,7 @@ public class WebServerComponentTest extends Assert {
// Wait for the server to close the connection. // Wait for the server to close the connection.
ch.close(); ch.close();
Assert.assertTrue(webServerComponent.isStarted()); Assert.assertTrue(webServerComponent.isStarted());
webServerComponent.stop(); webServerComponent.stop(true);
Assert.assertFalse(webServerComponent.isStarted()); Assert.assertFalse(webServerComponent.isStarted());
} }