Author: norman Date: Mon Jan 10 08:48:13 2011 New Revision: 1057121 URL: http://svn.apache.org/viewvc?rev=1057121&view=rev Log: Allow to browse mailqueue 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 james/server/trunk/queue-library/src/main/java/org/apache/james/queue/library/MailQueueManagement.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=1057121&r1=1057120&r2=1057121&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 Mon Jan 10 08:48:13 2011 @@ -83,12 +83,34 @@ public interface ManageableMailQueue ext * * */ - public interface MailQueueIterator extends Iterator<Mail> { + public interface MailQueueIterator extends Iterator<MailQueueItemView> { /** * Close the iterator. After this was called the iterator MUST NOT be used again */ public void close(); } + + /** + * Represent a View over a queue {...@link MailQueueItem} + * + * + */ + public interface MailQueueItemView { + + /** + * Return the Mail + * + * @return mail + */ + public Mail getMail(); + + /** + * Return the timestamp when the mail will be ready for dequeuing or -1 if there is no restriction set.. + * + * @return nextDelivery + */ + public long getNextDelivery(); + } } 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=1057121&r1=1057120&r2=1057121&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 Mon Jan 10 08:48:13 2011 @@ -495,7 +495,7 @@ public class JMSMailQueue implements Man browser = session.createBrowser(queue); - Enumeration messages = browser.getEnumeration(); + Enumeration<Message> messages = browser.getEnumeration(); while(messages.hasMoreElements()) { messages.nextElement(); @@ -740,6 +740,7 @@ public class JMSMailQueue implements Man * (non-Javadoc) * @see org.apache.james.queue.api.ManageableMailQueue#browse() */ + @SuppressWarnings("unchecked") public MailQueueIterator browse() throws MailQueueException { Connection connection = null; Session session = null; @@ -752,7 +753,7 @@ public class JMSMailQueue implements Man browser = session.createBrowser(queue); - final Enumeration messages = browser.getEnumeration(); + final Enumeration<Message> messages = browser.getEnumeration(); final Connection myconnection = connection; final Session mysession = session; @@ -771,10 +772,30 @@ public class JMSMailQueue implements Man * (non-Javadoc) * @see java.util.Iterator#next() */ - public Mail next() { + public MailQueueItemView next() { while (hasNext()) { try { - return createMail((Message) messages.nextElement()); + Message m = messages.nextElement(); + final Mail mail = createMail(m); + final long nextDelivery = m.getLongProperty(JAMES_NEXT_DELIVERY); + return new MailQueueItemView() { + + /* + * (non-Javadoc) + * @see org.apache.james.queue.api.ManageableMailQueue.MailQueueItemView#getNextDelivery() + */ + public long getNextDelivery() { + return nextDelivery; + } + + /* + * (non-Javadoc) + * @see org.apache.james.queue.api.ManageableMailQueue.MailQueueItemView#getMail() + */ + public Mail getMail() { + return mail; + } + }; } catch (MessagingException e) { logger.error("Unable to browse queue", e); } catch (JMSException e) { @@ -794,6 +815,10 @@ public class JMSMailQueue implements Man return messages.hasMoreElements(); } + /* + * (non-Javadoc) + * @see org.apache.james.queue.api.ManageableMailQueue.MailQueueIterator#close() + */ public void close() { try { 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=1057121&r1=1057120&r2=1057121&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 Mon Jan 10 08:48:13 2011 @@ -405,8 +405,8 @@ public class JMSMailQueueTest extends Te assertEquals(2, queue.getSize()); MailQueueIterator it = queue.browse(); - checkMail(mail, it.next()); - checkMail(mail2, it.next()); + checkMail(mail, it.next().getMail()); + checkMail(mail2, it.next().getMail()); assertFalse(it.hasNext()); it.close(); @@ -418,7 +418,7 @@ public class JMSMailQueueTest extends Te assertEquals(1, queue.getSize()); it = queue.browse(); - checkMail(mail2, it.next()); + checkMail(mail2, it.next().getMail()); assertFalse(it.hasNext()); it.close(); } Modified: james/server/trunk/queue-library/src/main/java/org/apache/james/queue/library/MailQueueManagement.java URL: http://svn.apache.org/viewvc/james/server/trunk/queue-library/src/main/java/org/apache/james/queue/library/MailQueueManagement.java?rev=1057121&r1=1057120&r2=1057121&view=diff ============================================================================== --- james/server/trunk/queue-library/src/main/java/org/apache/james/queue/library/MailQueueManagement.java (original) +++ james/server/trunk/queue-library/src/main/java/org/apache/james/queue/library/MailQueueManagement.java Mon Jan 10 08:48:13 2011 @@ -18,8 +18,11 @@ ****************************************************************/ package org.apache.james.queue.library; +import java.io.Serializable; import java.util.ArrayList; +import java.util.Collection; import java.util.HashMap; +import java.util.Iterator; import java.util.List; import java.util.Map; @@ -34,9 +37,11 @@ import javax.management.openmbean.Simple import org.apache.james.queue.api.MailQueueManagementMBean; import org.apache.james.queue.api.ManageableMailQueue; import org.apache.james.queue.api.MailQueue.MailQueueException; +import org.apache.james.queue.api.ManageableMailQueue.MailQueueItemView; import org.apache.james.queue.api.ManageableMailQueue.MailQueueIterator; import org.apache.james.queue.api.ManageableMailQueue.Type; import org.apache.mailet.Mail; +import org.apache.mailet.MailAddress; /** * @@ -129,16 +134,96 @@ public class MailQueueManagement extends * (non-Javadoc) * @see org.apache.james.queue.api.MailQueueManagementMBean#browse() */ + @SuppressWarnings("unchecked") public List<CompositeData> browse() throws Exception { MailQueueIterator it = queue.browse(); List<CompositeData> data = new ArrayList<CompositeData>(); + String[] names = new String[] { + "name", + "sender", + "state", + "recipients", + "size", + "lastUpdated", + "remoteAddress", + "remoteHost", + "errorMessage", + "attributes", + "nextDelivery" + }; + String[] descs = new String[] { + "Unique name", + "Sender", + "Current state", + "Recipients", + "Size in bytes", + "Timestamp of last update", + "IPAddress of the sender", + "Hostname of the sender", + "Errormessage if any", + "Attributes stored", + "Timestamp of when the next delivery attempt will be make" + }; + OpenType<?>[] types = new OpenType[] { + SimpleType.STRING, + SimpleType.STRING, + SimpleType.STRING, + SimpleType.STRING, + SimpleType.LONG, + SimpleType.LONG, + SimpleType.STRING, + SimpleType.STRING, + SimpleType.STRING, + SimpleType.STRING, + SimpleType.LONG + }; + while(it.hasNext()) { - Mail m = it.next(); + + MailQueueItemView mView = it.next(); + Mail m = mView.getMail(); + long nextDelivery = mView.getNextDelivery(); Map<String, Object> map = new HashMap<String, Object>(); - map.put("name", m.getName()); - map.put("sender", m.getSender()); - map.put("state", m.getState()); - CompositeDataSupport c = new CompositeDataSupport(new CompositeType(Mail.class.getName(), "Mail", new String[] {"name", "sender", "state"}, new String[] {"The name of the Mail", "The sender of the Mail", "The state of the Mail"}, new OpenType[] { SimpleType.STRING, SimpleType.STRING, SimpleType.STRING}), map); + map.put(names[0], m.getName()); + String sender = null; + MailAddress senderAddress = m.getSender(); + if (senderAddress != null) { + sender = senderAddress.toString(); + } + map.put(names[1], sender); + map.put(names[2], m.getState()); + + StringBuilder rcptsBuilder = new StringBuilder(); + Collection<MailAddress> rcpts = m.getRecipients(); + if (rcpts != null) { + Iterator<MailAddress> rcptsIt = rcpts.iterator(); + while(rcptsIt.hasNext()) { + rcptsBuilder.append(rcptsIt.next().toString()); + if (rcptsIt.hasNext()) { + rcptsBuilder.append(","); + } + } + } + map.put(names[3], rcptsBuilder.toString()); + map.put(names[4], m.getMessageSize()); + map.put(names[5], m.getLastUpdated().getTime()); + map.put(names[6], m.getRemoteAddr()); + map.put(names[7], m.getRemoteHost()); + map.put(names[8], m.getErrorMessage()); + Map<String, String> attrs = new HashMap<String, String>(); + Iterator<String> attrNames = m.getAttributeNames(); + while(attrNames.hasNext()) { + String attrName = attrNames.next(); + String attrValueString = null; + Serializable attrValue = m.getAttribute(attrName); + if (attrValue != null) { + attrValueString = attrValue.toString(); + } + attrs.put(attrName, attrValueString); + } + map.put(names[9], attrs.toString()); + map.put(names[10], nextDelivery); + CompositeDataSupport c = new CompositeDataSupport(new CompositeType(Mail.class.getName(), "Queue Mail", names, descs, types), map); data.add(c); } it.close(); --------------------------------------------------------------------- To unsubscribe, e-mail: server-dev-unsubscr...@james.apache.org For additional commands, e-mail: server-dev-h...@james.apache.org