Author: norman
Date: Sun Jan  9 16:54:07 2011
New Revision: 1056975

URL: http://svn.apache.org/viewvc?rev=1056975&view=rev
Log:
Allow to browse content of a MailQueue, the same should be added for browsing 
via JMX. See JAMES-1180

Modified:
    
james/server/trunk/queue-api/src/main/java/org/apache/james/queue/api/ManageableMailQueue.java
    
james/server/trunk/queue-jms/src/main/java/org/apache/james/queue/jms/JMSMailQueue.java
    
james/server/trunk/queue-jms/src/test/java/org/apache/james/queue/jms/JMSMailQueueTest.java

Modified: 
james/server/trunk/queue-api/src/main/java/org/apache/james/queue/api/ManageableMailQueue.java
URL: 
http://svn.apache.org/viewvc/james/server/trunk/queue-api/src/main/java/org/apache/james/queue/api/ManageableMailQueue.java?rev=1056975&r1=1056974&r2=1056975&view=diff
==============================================================================
--- 
james/server/trunk/queue-api/src/main/java/org/apache/james/queue/api/ManageableMailQueue.java
 (original)
+++ 
james/server/trunk/queue-api/src/main/java/org/apache/james/queue/api/ManageableMailQueue.java
 Sun Jan  9 16:54:07 2011
@@ -18,7 +18,9 @@
  ****************************************************************/
 package org.apache.james.queue.api;
 
-import java.util.List;
+import java.util.Iterator;
+
+import org.apache.mailet.Mail;
 
 /**
  * {...@link MailQueue} which is manageable
@@ -67,23 +69,26 @@ public interface ManageableMailQueue ext
     public long remove(Type type, String value) throws MailQueueException;
     
     /**
-     * Return a View on the content of the queue
+     * Allow to browse the queues content. The returned content may get 
modified
+     * while browsing it during other threads.
+     * 
      * 
      * @return content
      */
-    public List<MailQueueItemView> view() throws MailQueueException;
+    public MailQueueIterator browse() throws MailQueueException;
     
     
     /**
-     * A View of a {...@link MailQueueItem}
+     * {...@link Iterator} subclass which allows to browse the content of a 
queue
      * 
      *
      */
-    public interface MailQueueItemView {
-        public String getName();
-        public String getSender();
-        public String[] getRecipients();
-        public long getSize();
-        public long getNextRetry();
+    public interface MailQueueIterator extends Iterator<Mail> {
+        
+        /**
+         * Close the iterator. After this was called the iterator MUST NOT be 
used again
+         */
+        public void close();
     }
+  
 }

Modified: 
james/server/trunk/queue-jms/src/main/java/org/apache/james/queue/jms/JMSMailQueue.java
URL: 
http://svn.apache.org/viewvc/james/server/trunk/queue-jms/src/main/java/org/apache/james/queue/jms/JMSMailQueue.java?rev=1056975&r1=1056974&r2=1056975&view=diff
==============================================================================
--- 
james/server/trunk/queue-jms/src/main/java/org/apache/james/queue/jms/JMSMailQueue.java
 (original)
+++ 
james/server/trunk/queue-jms/src/main/java/org/apache/james/queue/jms/JMSMailQueue.java
 Sun Jan  9 16:54:07 2011
@@ -28,6 +28,7 @@ import java.util.HashMap;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
+import java.util.NoSuchElementException;
 import java.util.StringTokenizer;
 import java.util.concurrent.TimeUnit;
 
@@ -734,17 +735,15 @@ public class JMSMailQueue implements Man
         }
         return -1;
     }
-    
+
     /*
      * (non-Javadoc)
-     * @see org.apache.james.queue.api.ManageableMailQueue#view()
+     * @see org.apache.james.queue.api.ManageableMailQueue#browse()
      */
-    @SuppressWarnings("unchecked")
-    public List<MailQueueItemView> view() throws MailQueueException {
+    public MailQueueIterator browse() throws MailQueueException {
         Connection connection = null;
         Session session = null;
         QueueBrowser browser = null;
-        List<MailQueueItemView> view = new ArrayList<MailQueueItemView>();
         try {
             connection = connectionFactory.createConnection();
             connection.start();
@@ -753,22 +752,64 @@ public class JMSMailQueue implements Man
 
             browser = session.createBrowser(queue);
             
-            Enumeration messages = browser.getEnumeration();
+            final Enumeration messages = browser.getEnumeration();
+            
+            final Connection myconnection = connection;
+            final Session mysession = session;
+            final QueueBrowser mybrowser = browser;
+            
+            return new MailQueueIterator() {
+                
+                public void remove() {
+                    throw new UnsupportedOperationException("Read-only");
+                }
+                
+                public Mail next() {
+                    while (hasNext()) {
+                        try {
+                            return createMail((Message) 
messages.nextElement());
+                        } catch (MessagingException e) {
+                            logger.error("Unable to browse queue", e);
+                        } catch (JMSException e) {
+                            logger.error("Unable to browse queue", e);
+                        }
+                    }
+                        
+                    throw new NoSuchElementException();
+                    
+                }
+                
+                public boolean hasNext() {
+                    return messages.hasMoreElements();
+                }
+                
+                public void close() {
+                    
+                    try {
+                        if (mybrowser != null)
+                            mybrowser.close();
+                    } catch (JMSException e1) {
+                        // ignore here
+                    }
+
+                    try {
+                        if (mysession != null)
+                            mysession.close();
+                    } catch (JMSException e1) {
+                        // ignore here
+                    }
+
+                    try {
+                        if (myconnection != null)
+                            myconnection.close();
+                    } catch (JMSException e1) {
+                        // ignore here
+                    }                    
+                }
+            };
             
-            while(messages.hasMoreElements()) {
-                Message m = (Message) messages.nextElement();
-                String name = m.getStringProperty(JAMES_MAIL_NAME);
-                long size = m.getLongProperty(JAMES_MAIL_MESSAGE_SIZE);
-                String sender = m.getStringProperty(JAMES_MAIL_SENDER);
-                String[] recipients = 
m.getStringProperty(JAMES_MAIL_RECIPIENTS).split(JAMES_MAIL_SEPARATOR);
-                long retry = m.getLongProperty(JAMES_NEXT_DELIVERY);
-                view.add(new SimpleMailQueueItemView(name, sender, recipients, 
size, retry));
-            }
-            return view;
         } catch (Exception e) {
-            logger.error("Unable to get size of queue " + queuename, e);
-            throw new MailQueueException("Unable to get size of queue " + 
queuename, e);
-        } finally {
+            
             try {
                 if (browser != null)
                     browser.close();
@@ -789,64 +830,9 @@ public class JMSMailQueue implements Man
             } catch (JMSException e1) {
                 // ignore here
             }
-        }        
-    }
-    
-    protected class SimpleMailQueueItemView implements MailQueueItemView {
-        private String name;
-        private String sender;
-        private long size;
-        private long retry;
-        private String[] recipients;
-
-        public SimpleMailQueueItemView(String name, String sender, String[] 
recipients, long size, long retry) {
-            this.name = name;
-            this.sender = sender;
-            this.recipients = recipients;
-            this.size = size;
-            this.retry = retry;
-        }
-        
-        /*
-         * (non-Javadoc)
-         * @see 
org.apache.james.queue.api.ManageableMailQueue.MailQueueItemView#getName()
-         */
-        public String getName() {
-            return name;
-        }
-
-        /*
-         * (non-Javadoc)
-         * @see 
org.apache.james.queue.api.ManageableMailQueue.MailQueueItemView#getRecipients()
-         */
-        public String[] getRecipients() {
-            return recipients;
-        }
-
-        /*
-         * (non-Javadoc)
-         * @see 
org.apache.james.queue.api.ManageableMailQueue.MailQueueItemView#getSender()
-         */
-        public String getSender() {
-            return sender;
-        }
-
-        /*
-         * (non-Javadoc)
-         * @see 
org.apache.james.queue.api.ManageableMailQueue.MailQueueItemView#getSize()
-         */
-        public long getSize() {
-            return size;
-        }
-
-        /*
-         * (non-Javadoc)
-         * @see 
org.apache.james.queue.api.ManageableMailQueue.MailQueueItemView#getNextRetry()
-         */
-        public long getNextRetry() {
-            return retry;
+            logger.error("Unable to get size of queue " + queuename, e);
+            throw new MailQueueException("Unable to get size of queue " + 
queuename, e);
         }
-        
     }
 
 }

Modified: 
james/server/trunk/queue-jms/src/test/java/org/apache/james/queue/jms/JMSMailQueueTest.java
URL: 
http://svn.apache.org/viewvc/james/server/trunk/queue-jms/src/test/java/org/apache/james/queue/jms/JMSMailQueueTest.java?rev=1056975&r1=1056974&r2=1056975&view=diff
==============================================================================
--- 
james/server/trunk/queue-jms/src/test/java/org/apache/james/queue/jms/JMSMailQueueTest.java
 (original)
+++ 
james/server/trunk/queue-jms/src/test/java/org/apache/james/queue/jms/JMSMailQueueTest.java
 Sun Jan  9 16:54:07 2011
@@ -40,6 +40,7 @@ import org.apache.commons.logging.impl.S
 import org.apache.james.core.MailImpl;
 import org.apache.james.queue.api.ManageableMailQueue;
 import org.apache.james.queue.api.MailQueue.MailQueueItem;
+import org.apache.james.queue.api.ManageableMailQueue.MailQueueIterator;
 import org.apache.mailet.Mail;
 import org.apache.mailet.MailAddress;
 
@@ -389,5 +390,38 @@ public class JMSMailQueueTest extends Te
         assertEquals(0, queue.getSize());
     }
     
+    public void testBrowse() throws MessagingException, InterruptedException, 
IOException {
+        // should be empty
+        assertEquals(0, queue.getSize());
+        
+        Mail mail = createMail();
+        Mail mail2 =createMail();
+
+        queue.enQueue(mail);
+        queue.enQueue(mail2);
+        
+        Thread.sleep(200);
+        
+        assertEquals(2, queue.getSize());
+        
+        MailQueueIterator it = queue.browse();
+        checkMail(mail, it.next());
+        checkMail(mail2, it.next());
+        assertFalse(it.hasNext());
+        it.close();
+
+        assertEquals(2, queue.getSize());
+        MailQueueItem item2 = queue.deQueue();
+        checkMail(mail, item2.getMail());
+        item2.done(true);
+        Thread.sleep(200);
+
+        assertEquals(1, queue.getSize());
+        it = queue.browse();
+        checkMail(mail2, it.next());
+        assertFalse(it.hasNext());
+        it.close();
+    }
+    
 
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to