james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From adup...@apache.org
Subject [1/2] james-project git commit: JAMES-2563 Add JPA HealthCheck
Date Tue, 06 Nov 2018 17:08:44 GMT
Repository: james-project
Updated Branches:
  refs/heads/master 956ee5a32 -> 1c7c2fe7a


JAMES-2563 Add JPA HealthCheck


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

Branch: refs/heads/master
Commit: e8ed41737dc77574bb435cb2b9810dae8c3b6db5
Parents: 45d311c
Author: Luiz Henrique Pegoraro <pegorarohluiz@gmail.com>
Authored: Sat Oct 20 17:25:16 2018 -0300
Committer: Antoine Duprat <aduprat@linagora.com>
Committed: Tue Nov 6 17:55:44 2018 +0100

----------------------------------------------------------------------
 .../james/jpa/healthcheck/JPAHealthCheck.java   | 63 ++++++++++++++++++
 .../jpa/healthcheck/JPAHealthCheckTest.java     | 68 ++++++++++++++++++++
 2 files changed, 131 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/e8ed4173/server/data/data-jpa/src/main/java/org/apache/james/jpa/healthcheck/JPAHealthCheck.java
----------------------------------------------------------------------
diff --git a/server/data/data-jpa/src/main/java/org/apache/james/jpa/healthcheck/JPAHealthCheck.java
b/server/data/data-jpa/src/main/java/org/apache/james/jpa/healthcheck/JPAHealthCheck.java
new file mode 100644
index 0000000..5ab69b7
--- /dev/null
+++ b/server/data/data-jpa/src/main/java/org/apache/james/jpa/healthcheck/JPAHealthCheck.java
@@ -0,0 +1,63 @@
+/****************************************************************
+ * 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.jpa.healthcheck;
+
+import static org.apache.james.core.healthcheck.Result.healthy;
+import static org.apache.james.core.healthcheck.Result.unhealthy;
+
+import javax.inject.Inject;
+import javax.persistence.EntityManagerFactory;
+
+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;
+
+
+public class JPAHealthCheck implements HealthCheck {
+
+    private static final Logger LOGGER = LoggerFactory.getLogger(JPAHealthCheck.class);
+    private final EntityManagerFactory entityManagerFactory;
+
+    @Inject
+    public JPAHealthCheck(EntityManagerFactory entityManagerFactory) {
+        this.entityManagerFactory = entityManagerFactory;
+    }
+
+    @Override
+    public ComponentName componentName() {
+        return new ComponentName("JPA Backend");
+    }
+
+    @Override
+    public Result check() {
+        LOGGER.debug("Checking if EntityManager is created successfully");
+        try {
+            if (entityManagerFactory.createEntityManager().isOpen()) {
+                LOGGER.debug("EntityManager can execute queries, the connection is healthy");
+                return healthy(componentName());
+            }
+        } catch (IllegalStateException stateException) {
+            LOGGER.debug("EntityManagerFactory or EntityManager thrown an IllegalStateException,
the connection is unhealthy");
+            return unhealthy(componentName());
+        }
+        return unhealthy(componentName());
+    }
+}

http://git-wip-us.apache.org/repos/asf/james-project/blob/e8ed4173/server/data/data-jpa/src/test/java/org/apache/james/jpa/healthcheck/JPAHealthCheckTest.java
----------------------------------------------------------------------
diff --git a/server/data/data-jpa/src/test/java/org/apache/james/jpa/healthcheck/JPAHealthCheckTest.java
b/server/data/data-jpa/src/test/java/org/apache/james/jpa/healthcheck/JPAHealthCheckTest.java
new file mode 100644
index 0000000..b749d96
--- /dev/null
+++ b/server/data/data-jpa/src/test/java/org/apache/james/jpa/healthcheck/JPAHealthCheckTest.java
@@ -0,0 +1,68 @@
+/****************************************************************
+ * 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.jpa.healthcheck;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.fail;
+
+import org.apache.james.backends.jpa.JpaTestCluster;
+import org.apache.james.core.healthcheck.Result;
+import org.apache.james.core.healthcheck.ResultStatus;
+import org.apache.james.mailrepository.jpa.JPAUrl;
+import org.junit.Rule;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.rules.ExpectedException;
+
+class JPAHealthCheckTest {
+
+    private JPAHealthCheck jpaHealthCheck;
+    private JpaTestCluster jpaTestCluster;
+
+    @Rule
+    ExpectedException expectedException = ExpectedException.none();
+
+    @BeforeEach
+    void setUp() {
+        jpaTestCluster = JpaTestCluster.create(JPAUrl.class);
+        jpaHealthCheck = new JPAHealthCheck(jpaTestCluster.getEntityManagerFactory());
+    }
+
+    @Test
+    void testWhenActive() {
+        Result result = jpaHealthCheck.check();
+        ResultStatus healthy = ResultStatus.HEALTHY;
+        assertThat(result.getStatus()).as("Result %s status should be %s", result.getStatus(),
healthy)
+                .isEqualTo(healthy);
+    }
+
+    @Test
+    void testWhenInactive() {
+        jpaTestCluster.getEntityManagerFactory().close();
+        Result result = Result.healthy(jpaHealthCheck.componentName());
+        try {
+            result = jpaHealthCheck.check();
+        } catch (IllegalStateException e) {
+            fail("The exception of the EMF was not handled property.ยช");
+        }
+        ResultStatus unhealthy = ResultStatus.UNHEALTHY;
+        assertThat(result.getStatus()).as("Result %s status should be %s", result.getStatus(),
unhealthy)
+                .isEqualTo(unhealthy);
+    }
+}


---------------------------------------------------------------------
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