james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From adup...@apache.org
Subject [5/7] james-project git commit: JAMES-2545 Implement a healthCheck for RabbitMQ
Date Wed, 12 Sep 2018 08:03:27 GMT
JAMES-2545 Implement a healthCheck for RabbitMQ


Project: http://git-wip-us.apache.org/repos/asf/james-project/repo
Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/0b7748ae
Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/0b7748ae
Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/0b7748ae

Branch: refs/heads/master
Commit: 0b7748aedfecf0130e6e70479a27d5c973f0a13c
Parents: a112f57
Author: Benoit Tellier <btellier@linagora.com>
Authored: Mon Sep 10 11:40:06 2018 +0700
Committer: Antoine Duprat <aduprat@linagora.com>
Committed: Wed Sep 12 10:01:13 2018 +0200

----------------------------------------------------------------------
 .../backend/mailqueue/RabbitMQHealthCheck.java  | 67 ++++++++++++++++
 .../james/backend/mailqueue/DockerRabbitMQ.java |  2 +-
 .../mailqueue/RabbitMQHealthCheckTest.java      | 83 ++++++++++++++++++++
 3 files changed, 151 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/0b7748ae/backends-common/rabbitmq/src/main/java/org/apache/james/backend/mailqueue/RabbitMQHealthCheck.java
----------------------------------------------------------------------
diff --git a/backends-common/rabbitmq/src/main/java/org/apache/james/backend/mailqueue/RabbitMQHealthCheck.java
b/backends-common/rabbitmq/src/main/java/org/apache/james/backend/mailqueue/RabbitMQHealthCheck.java
new file mode 100644
index 0000000..2287cb6
--- /dev/null
+++ b/backends-common/rabbitmq/src/main/java/org/apache/james/backend/mailqueue/RabbitMQHealthCheck.java
@@ -0,0 +1,67 @@
+/****************************************************************
+ * 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.james.backend.mailqueue;
+
+import java.net.URISyntaxException;
+import java.security.KeyManagementException;
+import java.security.NoSuchAlgorithmException;
+
+import javax.inject.Inject;
+
+import org.apache.james.core.healthcheck.ComponentName;
+import org.apache.james.core.healthcheck.HealthCheck;
+import org.apache.james.core.healthcheck.Result;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.rabbitmq.client.Connection;
+import com.rabbitmq.client.ConnectionFactory;
+
+public class RabbitMQHealthCheck implements HealthCheck {
+    private static final Logger LOGGER = LoggerFactory.getLogger(RabbitMQHealthCheck.class);
+    private static final ComponentName COMPONENT_NAME = new ComponentName("RabbitMQ backend");
+
+    private final ConnectionFactory connectionFactory;
+
+    @Inject
+    public RabbitMQHealthCheck(RabbitMQConfiguration configuration) throws NoSuchAlgorithmException,
KeyManagementException, URISyntaxException {
+        this.connectionFactory = new ConnectionFactory();
+        this.connectionFactory.setUri(configuration.getUri());
+    }
+
+    @Override
+    public ComponentName componentName() {
+        return COMPONENT_NAME;
+    }
+
+    @Override
+    public Result check() {
+        try (Connection connection = connectionFactory.newConnection()) {
+            if (connection.isOpen()) {
+                return Result.healthy(COMPONENT_NAME);
+            }
+            LOGGER.error("The created connection was not opened");
+            return Result.unhealthy(COMPONENT_NAME);
+        } catch (Exception e) {
+            LOGGER.error("Unhealthy RabbitMQ instances: could not establish a connection",
e);
+            return Result.unhealthy(COMPONENT_NAME);
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/james-project/blob/0b7748ae/backends-common/rabbitmq/src/test/java/org/apache/james/backend/mailqueue/DockerRabbitMQ.java
----------------------------------------------------------------------
diff --git a/backends-common/rabbitmq/src/test/java/org/apache/james/backend/mailqueue/DockerRabbitMQ.java
b/backends-common/rabbitmq/src/test/java/org/apache/james/backend/mailqueue/DockerRabbitMQ.java
index 6c817cf..29f6757 100644
--- a/backends-common/rabbitmq/src/test/java/org/apache/james/backend/mailqueue/DockerRabbitMQ.java
+++ b/backends-common/rabbitmq/src/test/java/org/apache/james/backend/mailqueue/DockerRabbitMQ.java
@@ -133,7 +133,7 @@ public class DockerRabbitMQ {
         joinCluster(rabbitMQ);
     }
 
-    private void stopApp() throws java.io.IOException, InterruptedException {
+    public void stopApp() throws java.io.IOException, InterruptedException {
         String stdout = container()
             .execInContainer("rabbitmqctl", "stop_app")
             .getStdout();

http://git-wip-us.apache.org/repos/asf/james-project/blob/0b7748ae/backends-common/rabbitmq/src/test/java/org/apache/james/backend/mailqueue/RabbitMQHealthCheckTest.java
----------------------------------------------------------------------
diff --git a/backends-common/rabbitmq/src/test/java/org/apache/james/backend/mailqueue/RabbitMQHealthCheckTest.java
b/backends-common/rabbitmq/src/test/java/org/apache/james/backend/mailqueue/RabbitMQHealthCheckTest.java
new file mode 100644
index 0000000..1352590
--- /dev/null
+++ b/backends-common/rabbitmq/src/test/java/org/apache/james/backend/mailqueue/RabbitMQHealthCheckTest.java
@@ -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 org.apache.james.backend.mailqueue;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+import java.net.URI;
+
+import org.apache.james.core.healthcheck.Result;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+
+@ExtendWith(DockerRabbitMQExtension.class)
+class RabbitMQHealthCheckTest {
+    private RabbitMQHealthCheck healthCheck;
+
+    @BeforeEach
+    void setUp(DockerRabbitMQ rabbitMQ) throws Exception {
+        URI amqpUri = URI.create("amqp://" + rabbitMQ.getHostIp() + ":" + rabbitMQ.getPort());
+        URI managementUri = URI.create("http://" + rabbitMQ.getHostIp() + ":" + rabbitMQ.getAdminPort());
+
+        healthCheck = new RabbitMQHealthCheck(
+            RabbitMQConfiguration.builder()
+                .amqpUri(amqpUri)
+                .managementUri(managementUri)
+                .build());
+    }
+
+    @Test
+    void checkShouldReturnHealthyWhenRabbitMQIsRunning() {
+        Result check = healthCheck.check();
+
+        assertThat(check.isHealthy()).isTrue();
+    }
+
+    @Test
+    void checkShouldReturnUnhealthyWhenRabbitMQIsNotRunning(DockerRabbitMQ rabbitMQ) throws
Exception {
+        rabbitMQ.stopApp();
+
+        Result check = healthCheck.check();
+
+        assertThat(check.isHealthy()).isFalse();
+    }
+
+    @Test
+    void checkShouldDetectWhenRabbitMQRecovered(DockerRabbitMQ rabbitMQ) throws Exception
{
+        rabbitMQ.stopApp();
+        healthCheck.check();
+
+        rabbitMQ.startApp();
+
+        Result check = healthCheck.check();
+        assertThat(check.isHealthy()).isTrue();
+    }
+
+    @Test
+    void checkShouldDetectWhenRabbitMQFail(DockerRabbitMQ rabbitMQ) throws Exception {
+        healthCheck.check();
+
+        rabbitMQ.stopApp();
+
+        Result check = healthCheck.check();
+        assertThat(check.isHealthy()).isFalse();
+    }
+}
\ No newline at end of file


---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org


Mime
View raw message