View Javadoc

1   /*
2    * Copyright 2007 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      protected void waitForNewMessages(Folder folder) throws MessagingException, InterruptedException {
48          Thread.sleep(pollingInterval);
49          afterSleep(folder);
50      }
51  
52      /**
53       * Invoked after the {@link Thread#sleep(long)} method has been invoked. This implementation calls {@link
54       * Folder#getMessageCount()}, to force new messages to be seen.
55       *
56       * @param folder the folder to check for new messages
57       * @throws MessagingException in case of JavaMail errors
58       */
59      protected void afterSleep(Folder folder) throws MessagingException {
60          folder.getMessageCount();
61      }
62  
63  }