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.MessagingException;
21  
22  /**
23   * Implementation of the {@link MonitoringStrategy} interface that uses a simple polling mechanism. Defines a {@link
24   * #setPollingInterval(long) polling interval} property which defines the interval in between message polls.
25   * <p/>
26   * <b>Note</b> that this implementation is not suitable for use with POP3 servers. Use the {@link
27   * Pop3PollingMonitoringStrategy} instead.
28   *
29   * @author Arjen Poutsma
30   * @since 1.5.0
31   */
32  public class PollingMonitoringStrategy extends AbstractMonitoringStrategy {
33  
34      /** Defines the default polling frequency. Set to 1000 * 60 milliseconds (i.e. 1 minute). */
35      public static final long DEFAULT_POLLING_FREQUENCY = 1000 * 60;
36  
37      private long pollingInterval = DEFAULT_POLLING_FREQUENCY;
38  
39      /**
40       * Sets the interval used in between message polls, <strong>in milliseconds</strong>. The default is 1000 * 60 ms,
41       * that is 1 minute.
42       */
43      public void setPollingInterval(long pollingInterval) {
44          this.pollingInterval = pollingInterval;
45      }
46  
47      @Override
48      protected void waitForNewMessages(Folder folder) throws MessagingException, InterruptedException {
49          Thread.sleep(pollingInterval);
50          afterSleep(folder);
51      }
52  
53      /**
54       * Invoked after the {@link Thread#sleep(long)} method has been invoked. This implementation calls {@link
55       * Folder#getMessageCount()}, to force new messages to be seen.
56       *
57       * @param folder the folder to check for new messages
58       * @throws MessagingException in case of JavaMail errors
59       */
60      protected void afterSleep(Folder folder) throws MessagingException {
61          folder.getMessageCount();
62      }
63  
64  }