View Javadoc

1   /*
2    * Copyright 2005-2010 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.springframework.ws.transport.mail.monitor;
18  
19  import javax.mail.Folder;
20  import javax.mail.Message;
21  import javax.mail.MessagingException;
22  import javax.mail.event.MessageCountAdapter;
23  import javax.mail.event.MessageCountEvent;
24  import javax.mail.event.MessageCountListener;
25  
26  import org.springframework.util.Assert;
27  
28  import com.sun.mail.imap.IMAPFolder;
29  
30  /**
31   * Implementation of the [email protected] MonitoringStrategy} interface that uses the IMAP IDLE command for asynchronous message
32   * detection.
33   * <p/>
34   * <b>Note</b> that this implementation is only suitable for use with IMAP servers which support the IDLE command.
35   * Additionally, this strategy requires JavaMail version 1.4.1.
36   *
37   * @author Arjen Poutsma
38   * @since 1.5.0
39   */
40  public class ImapIdleMonitoringStrategy extends AbstractMonitoringStrategy {
41  
42      private MessageCountListener messageCountListener;
43  
44      @Override
45      protected void waitForNewMessages(Folder folder) throws MessagingException, InterruptedException {
46          Assert.isInstanceOf(IMAPFolder.class, folder);
47          IMAPFolder imapFolder = (IMAPFolder) folder;
48          // retrieve unseen messages before we enter the blocking idle call
49          if (searchForNewMessages(folder).length > 0) {
50              return;
51          }
52          if (messageCountListener == null) {
53              createMessageCountListener();
54          }
55          folder.addMessageCountListener(messageCountListener);
56          try {
57              imapFolder.idle();
58          }
59          finally {
60              folder.removeMessageCountListener(messageCountListener);
61          }
62      }
63  
64      private void createMessageCountListener() {
65          messageCountListener = new MessageCountAdapter() {
66              @Override
67              public void messagesAdded(MessageCountEvent e) {
68                  Message[] messages = e.getMessages();
69                  for (Message message : messages) {
70                      try {
71                          // this will return the flow to the idle call, above
72                          message.getLineCount();
73                      }
74                      catch (MessagingException ex) {
75                          // ignore
76                      }
77                  }
78              }
79          };
80      }
81  }