james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From matth...@apache.org
Subject svn commit: r1709428 - in /james/project/trunk: queue/ queue/queue-jms/ queue/queue-jms/src/ queue/queue-jms/src/test/ queue/queue-jms/src/test/java/ queue/queue-jms/src/test/java/org/ queue/queue-jms/src/test/java/org/apache/ queue/queue-jms/src/test/...
Date Mon, 19 Oct 2015 15:00:26 GMT
Author: matthieu
Date: Mon Oct 19 15:00:26 2015
New Revision: 1709428

URL: http://svn.apache.org/viewvc?rev=1709428&view=rev
Log:
JAMES-1609 Correct MBean un registry for mail queue factories

Added:
    james/project/trunk/queue/
    james/project/trunk/queue/queue-jms/
    james/project/trunk/queue/queue-jms/src/
    james/project/trunk/queue/queue-jms/src/test/
    james/project/trunk/queue/queue-jms/src/test/java/
    james/project/trunk/queue/queue-jms/src/test/java/org/
    james/project/trunk/queue/queue-jms/src/test/java/org/apache/
    james/project/trunk/queue/queue-jms/src/test/java/org/apache/james/
    james/project/trunk/queue/queue-jms/src/test/java/org/apache/james/queue/
    james/project/trunk/queue/queue-jms/src/test/java/org/apache/james/queue/library/
    james/project/trunk/queue/queue-jms/src/test/java/org/apache/james/queue/library/AbstractMailQueueFactoryTest.java
Modified:
    james/project/trunk/server/queue/queue-jms/pom.xml
    james/project/trunk/server/queue/queue-jms/src/main/java/org/apache/james/queue/library/AbstractMailQueueFactory.java

Added: james/project/trunk/queue/queue-jms/src/test/java/org/apache/james/queue/library/AbstractMailQueueFactoryTest.java
URL: http://svn.apache.org/viewvc/james/project/trunk/queue/queue-jms/src/test/java/org/apache/james/queue/library/AbstractMailQueueFactoryTest.java?rev=1709428&view=auto
==============================================================================
--- james/project/trunk/queue/queue-jms/src/test/java/org/apache/james/queue/library/AbstractMailQueueFactoryTest.java
(added)
+++ james/project/trunk/queue/queue-jms/src/test/java/org/apache/james/queue/library/AbstractMailQueueFactoryTest.java
Mon Oct 19 15:00:26 2015
@@ -0,0 +1,140 @@
+/****************************************************************
+ * 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.queue.library;
+
+import static org.mockito.Mockito.*;
+
+import org.apache.james.queue.api.MailQueue;
+import org.apache.james.queue.api.ManageableMailQueue;
+import org.apache.mailet.Mail;
+import org.junit.Before;
+import org.junit.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.management.InstanceNotFoundException;
+import javax.management.MBeanServer;
+import javax.management.ObjectName;
+import java.util.concurrent.TimeUnit;
+
+public class AbstractMailQueueFactoryTest {
+
+    private static final Logger LOGGER = LoggerFactory.getLogger(AbstractMailQueueFactoryTest.class);
+    private static final String QUEUE_1 = "queue1";
+    private static final String QUEUE_2 = "queue2";
+    private static final String QUEUE_3 = "queue3";
+
+    private AbstractMailQueueFactory abstractMailQueueFactory;
+    private MBeanServer mBeanServer;
+
+    @Before
+    public void setUp() {
+        mBeanServer = mock(MBeanServer.class);
+        abstractMailQueueFactory = new AbstractMailQueueFactory() {
+            @Override
+            protected MailQueue createMailQueue(String name) {
+                return new ManageableMailQueue() {
+
+                    @Override
+                    public void enQueue(Mail mail, long delay, TimeUnit unit) throws MailQueueException
{
+
+                    }
+
+                    @Override
+                    public void enQueue(Mail mail) throws MailQueueException {
+
+                    }
+
+                    @Override
+                    public MailQueueItem deQueue() throws MailQueueException {
+                        return null;
+                    }
+
+                    @Override
+                    public long getSize() throws MailQueueException {
+                        return 0;
+                    }
+
+                    @Override
+                    public long flush() throws MailQueueException {
+                        return 0;
+                    }
+
+                    @Override
+                    public long clear() throws MailQueueException {
+                        return 0;
+                    }
+
+                    @Override
+                    public long remove(Type type, String value) throws MailQueueException
{
+                        return 0;
+                    }
+
+                    @Override
+                    public MailQueueIterator browse() throws MailQueueException {
+                        return null;
+                    }
+                };
+            }
+        };
+        abstractMailQueueFactory.setMbeanServer(mBeanServer);
+        abstractMailQueueFactory.setLog(LOGGER);
+    }
+
+    @Test
+    public void destroyShouldRegisterManageableQueues() throws Exception {
+        abstractMailQueueFactory.getQueue(QUEUE_1);
+        verify(mBeanServer).registerMBean(any(MailQueue.class), eq(new ObjectName(AbstractMailQueueFactory.MBEAN_NAME_QUEUE_PREFIX
+ QUEUE_1)));
+    }
+
+    @Test
+    public void destroyShouldUnregisterAllRegisterQueue() throws Exception {
+        abstractMailQueueFactory.getQueue(QUEUE_1);
+        abstractMailQueueFactory.getQueue(QUEUE_2);
+        abstractMailQueueFactory.getQueue(QUEUE_3);
+        abstractMailQueueFactory.destroy();
+        verify(mBeanServer).unregisterMBean(eq(new ObjectName(AbstractMailQueueFactory.MBEAN_NAME_QUEUE_PREFIX
+ QUEUE_1)));
+        verify(mBeanServer).unregisterMBean(eq(new ObjectName(AbstractMailQueueFactory.MBEAN_NAME_QUEUE_PREFIX
+ QUEUE_2)));
+        verify(mBeanServer).unregisterMBean(eq(new ObjectName(AbstractMailQueueFactory.MBEAN_NAME_QUEUE_PREFIX
+ QUEUE_3)));
+    }
+
+    @Test
+    public void unregisterMBeanShouldWork() throws Exception {
+        abstractMailQueueFactory.getQueue(QUEUE_1);
+        abstractMailQueueFactory.unregisterMBean(AbstractMailQueueFactory.MBEAN_NAME_QUEUE_PREFIX
+ QUEUE_1);
+        verify(mBeanServer).unregisterMBean(eq(new ObjectName(AbstractMailQueueFactory.MBEAN_NAME_QUEUE_PREFIX
+ QUEUE_1)));
+    }
+
+    @Test
+    public void destroyShouldNotBeStoppedByExceptions() throws Exception {
+        abstractMailQueueFactory.getQueue(QUEUE_1);
+        abstractMailQueueFactory.getQueue(QUEUE_2);
+        abstractMailQueueFactory.getQueue(QUEUE_3);
+        doThrow(InstanceNotFoundException.class)
+            .doNothing()
+            .when(mBeanServer)
+            .unregisterMBean(new ObjectName(AbstractMailQueueFactory.MBEAN_NAME_QUEUE_PREFIX
+ QUEUE_1));
+        abstractMailQueueFactory.destroy();
+        verify(mBeanServer).unregisterMBean(eq(new ObjectName(AbstractMailQueueFactory.MBEAN_NAME_QUEUE_PREFIX
+ QUEUE_1)));
+        verify(mBeanServer).unregisterMBean(eq(new ObjectName(AbstractMailQueueFactory.MBEAN_NAME_QUEUE_PREFIX
+ QUEUE_2)));
+        verify(mBeanServer).unregisterMBean(eq(new ObjectName(AbstractMailQueueFactory.MBEAN_NAME_QUEUE_PREFIX
+ QUEUE_3)));
+    }
+
+}

Modified: james/project/trunk/server/queue/queue-jms/pom.xml
URL: http://svn.apache.org/viewvc/james/project/trunk/server/queue/queue-jms/pom.xml?rev=1709428&r1=1709427&r2=1709428&view=diff
==============================================================================
--- james/project/trunk/server/queue/queue-jms/pom.xml (original)
+++ james/project/trunk/server/queue/queue-jms/pom.xml Mon Oct 19 15:00:26 2015
@@ -97,6 +97,11 @@
             <artifactId>james-server-protocols-smtp</artifactId>
             <scope>test</scope>
         </dependency>
+        <dependency>
+            <groupId>org.mockito</groupId>
+            <artifactId>mockito-core</artifactId>
+            <scope>test</scope>
+        </dependency>
     </dependencies>
 
     <build>

Modified: james/project/trunk/server/queue/queue-jms/src/main/java/org/apache/james/queue/library/AbstractMailQueueFactory.java
URL: http://svn.apache.org/viewvc/james/project/trunk/server/queue/queue-jms/src/main/java/org/apache/james/queue/library/AbstractMailQueueFactory.java?rev=1709428&r1=1709427&r2=1709428&view=diff
==============================================================================
--- james/project/trunk/server/queue/queue-jms/src/main/java/org/apache/james/queue/library/AbstractMailQueueFactory.java
(original)
+++ james/project/trunk/server/queue/queue-jms/src/main/java/org/apache/james/queue/library/AbstractMailQueueFactory.java
Mon Oct 19 15:00:26 2015
@@ -29,6 +29,7 @@ import javax.annotation.PreDestroy;
 import javax.management.MBeanServer;
 import javax.management.ObjectName;
 
+import com.google.common.annotations.VisibleForTesting;
 import org.apache.james.lifecycle.api.LifecycleUtil;
 import org.apache.james.lifecycle.api.LogEnabled;
 import org.apache.james.queue.api.MailQueue;
@@ -43,6 +44,8 @@ import org.slf4j.Logger;
  */
 public abstract class AbstractMailQueueFactory implements MailQueueFactory, LogEnabled {
 
+    public static final String MBEAN_NAME_QUEUE_PREFIX = "org.apache.james:type=component,name=queue,queue=";
+
     protected final Map<String, MailQueue> queues = new HashMap<String, MailQueue>();
     protected Logger log;
     private boolean useJMX = true;
@@ -53,17 +56,26 @@ public abstract class AbstractMailQueueF
         this.useJMX = useJMX;
     }
 
+    @VisibleForTesting
+    void setMbeanServer(MBeanServer mbeanServer) {
+        this.mbeanServer = mbeanServer;
+    }
+
     @PostConstruct
     public void init() {
         mbeanServer = ManagementFactory.getPlatformMBeanServer();
     }
 
     @PreDestroy
-    public void destroy() {
+    public synchronized void destroy() {
         for (String mbean : mbeans) {
-            unregisterMBean(mbean);
+            try {
+                mbeanServer.unregisterMBean(new ObjectName(mbean));
+            } catch (Exception e) {
+                log.error("Error while destroying AbstractMailQueueFactory : ", e);
+            }
         }
-
+        mbeans.clear();
         for (MailQueue mailQueue : queues.values()) {
             LifecycleUtil.dispose(mailQueue);
         }
@@ -97,7 +109,7 @@ public abstract class AbstractMailQueueF
 
     protected synchronized void registerMBean(String queuename, MailQueue queue) {
 
-        String mbeanName = "org.apache.james:type=component,name=queue,queue=" + queuename;
+        String mbeanName = MBEAN_NAME_QUEUE_PREFIX + queuename;
         try {
             MailQueueManagementMBean mbean = null;
             if (queue instanceof ManageableMailQueue) {



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