[ 
https://issues.apache.org/jira/browse/ARTEMIS-3808?focusedWorklogId=771795&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-771795
 ]

ASF GitHub Bot logged work on ARTEMIS-3808:
-------------------------------------------

                Author: ASF GitHub Bot
            Created on: 18/May/22 10:54
            Start Date: 18/May/22 10:54
    Worklog Time Spent: 10m 
      Work Description: gemmellr commented on code in PR #4061:
URL: https://github.com/apache/activemq-artemis/pull/4061#discussion_r875734514


##########
artemis-server/src/main/java/org/apache/activemq/artemis/core/management/impl/ActiveMQServerControlImpl.java:
##########
@@ -4479,5 +4488,71 @@ public void replay(String startScan, String endScan, 
String address, String targ
 
       server.replay(startScanDate, endScanDate, address, target, filter);
    }
+
+   @Override
+   public void stopEmbeddedWebServer() throws Exception {
+      synchronized (embeddedWebServerLock) {
+         getEmbeddedWebServerComponent().stop(true);
+      }
+   }
+
+   @Override
+   public void startEmbeddedWebServer() throws Exception {
+      synchronized (embeddedWebServerLock) {
+         getEmbeddedWebServerComponent().start();
+      }
+   }
+
+   @Override
+   public void restartEmbeddedWebServer() throws Exception {
+      
restartEmbeddedWebServer(ActiveMQDefaultConfiguration.getDefaultEmbeddedWebServerRestartTimeout());
+   }
+
+   @Override
+   public void restartEmbeddedWebServer(long timeout) throws Exception {
+      final CountDownLatch latch = new CountDownLatch(1);
+      final AtomicReference<Exception> exception = new AtomicReference<>();
+      /*
+       * This needs to be run in its own thread managed by the broker because 
if it is run on a thread managed by Jetty
+       * (e.g. if it is invoked from the web console) then the thread will die 
before Jetty can be restarted.
+       */
+      server.getThreadPool().execute(() -> {
+         try {
+            synchronized (embeddedWebServerLock) {
+               stopEmbeddedWebServer();
+               startEmbeddedWebServer();
+            }
+         } catch (Exception e) {
+            exception.set(e);
+         } finally {
+            latch.countDown();
+         }
+      });
+      boolean result = !latch.await(timeout, TimeUnit.MILLISECONDS);
+      if (result || exception.get() != null) {
+         throw 
ActiveMQMessageBundle.BUNDLE.embeddedWebServerNotRestarted(timeout, 
exception.get());
+      }

Review Comment:
   ```suggestion
         if (!latch.await(timeout, TimeUnit.MILLISECONDS)) {
            throw 
ActiveMQMessageBundle.BUNDLE.embeddedWebServerRestartTimeout(timeout);
         } else  if (exception.get() != null) {
            throw 
ActiveMQMessageBundle.BUNDLE.embeddedWebServerRestartFailed(exception.get());
         }
   ```
   Could make the failure mode and exceptions clearer if they were reported 
separately: either the execution timed out and the state isnt actually known 
(it may even execute later on just fine), or it failed by throwing while we 
waited for execution.
   
   (ActiveMQMessageBundle obviously would need changed to match the suggestion 
change)



##########
artemis-server/src/main/java/org/apache/activemq/artemis/core/server/ActiveMQMessageBundle.java:
##########
@@ -522,4 +522,10 @@ IllegalStateException invalidRoutingTypeUpdate(String 
queueName,
 
    @Message(id = 229240, value = "Connection router {0} rejected the 
connection", format = Message.Format.MESSAGE_FORMAT)
    ActiveMQRemoteDisconnectException connectionRejected(String 
connectionRouter);
+
+   @Message(id = 229241, value = "Embedded web server not found")
+   ActiveMQIllegalStateException embeddedWebServerNotFound();
+
+   @Message(id = 229242, value = "Embedded web server not restarted in {0} 
milliseconds", format = Message.Format.MESSAGE_FORMAT)
+   ActiveMQIllegalStateException embeddedWebServerNotRestarted(long timeout, 
@Cause Exception e);

Review Comment:
   Using ActiveMQIllegalStateException doesnt seem quite right. Its not that 
the user tried to do something when it was in the wrong state, its that a 
result wasnt known in an allotted time, so some kind of timeout exception woudl 
be more appropriate.



##########
tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/management/ActiveMQServerControlTest.java:
##########
@@ -4290,6 +4296,106 @@ public boolean isStarted() {
       }
    }
 
+   @Test
+   public void testManualStopStartEmbeddedWebServer() throws Exception {
+      FakeWebServerComponent fake = new FakeWebServerComponent();
+      server.addExternalComponent(fake, true);
+      Assert.assertTrue(fake.isStarted());
+
+      ActiveMQServerControl serverControl = createManagementControl();
+      serverControl.stopEmbeddedWebServer();
+      Assert.assertFalse(fake.isStarted());
+      serverControl.startEmbeddedWebServer();
+      Assert.assertTrue(fake.isStarted());
+   }
+
+   @Test
+   public void testRestartEmbeddedWebServer() throws Exception {
+      FakeWebServerComponent fake = new FakeWebServerComponent();
+      server.addExternalComponent(fake, true);
+      Assert.assertTrue(fake.isStarted());
+
+      ActiveMQServerControl serverControl = createManagementControl();
+      long time = System.currentTimeMillis();
+      Assert.assertTrue(time >= fake.getStartTime());
+      Assert.assertTrue(time > fake.getStopTime());
+      Thread.sleep(5);
+      serverControl.restartEmbeddedWebServer();
+      Assert.assertTrue(serverControl.isEmbeddedWebServerStarted());
+      Assert.assertTrue(time < fake.getStartTime());
+      Assert.assertTrue(time < fake.getStopTime());
+   }
+
+   @Test
+   public void testRestartEmbeddedWebServerTimeout() throws Exception {
+      final CountDownLatch startDelay = new CountDownLatch(1);
+      FakeWebServerComponent fake = new FakeWebServerComponent(startDelay);
+      server.addExternalComponent(fake, false);
+
+      ActiveMQServerControl serverControl = createManagementControl();
+      try {
+         serverControl.restartEmbeddedWebServer(10);
+         fail();
+      } catch (Exception e) {
+         Assert.assertTrue(e instanceof ActiveMQIllegalStateException);
+      }
+      startDelay.countDown();

Review Comment:
   The countdown should go in a finally or it might never happen. Rather than 
catching Exception it could just catch the expected exception type and let 
everything else bubble up (then you also dont need the assertion).





Issue Time Tracking
-------------------

    Worklog Id:     (was: 771795)
    Time Spent: 5h 20m  (was: 5h 10m)

> Support starting/stopping the embedded web server via mangement
> ---------------------------------------------------------------
>
>                 Key: ARTEMIS-3808
>                 URL: https://issues.apache.org/jira/browse/ARTEMIS-3808
>             Project: ActiveMQ Artemis
>          Issue Type: Improvement
>            Reporter: Justin Bertram
>            Assignee: Justin Bertram
>            Priority: Major
>          Time Spent: 5h 20m
>  Remaining Estimate: 0h
>
> It would be useful to be able to cycle the embedded web server if, for 
> example, one needed to renew the SSL certificates.



--
This message was sent by Atlassian Jira
(v8.20.7#820007)

Reply via email to