public class IdleManager extends Object
        ExecutorService es = Executors.newCachedThreadPool();
        final IdleManager idleManager = new IdleManager(session, es);
 
         @Resource
        ManagedExecutorService es;
        final IdleManager idleManager = new IdleManager(session, es);
 
        Folder folder = store.getFolder("INBOX");
        folder.open(Folder.READ_WRITE);
        folder.addMessageCountListener(new MessageCountAdapter() {
            public void messagesAdded(MessageCountEvent ev) {
                Folder folder = (Folder)ev.getSource();
                Message[] msgs = ev.getMessages();
                System.out.println("Folder: " + folder +
                    " got " + msgs.length + " new messages");
                try {
                    // process new messages
                    idleManager.watch(folder); // keep watching for new messages
                } catch (MessagingException mex) {
                    // handle exception related to the Folder
                }
            }
        });
        idleManager.watch(folder);
 
        // the following should be done once...
        Properties props = session.getProperties();
        props.put("mail.event.scope", "session"); // or "application"
        props.put("mail.event.executor", es);
 The IdleManager is created with a Session, which it uses only to control debug output. A single IdleManager instance can watch multiple Folders from multiple Stores and multiple Sessions.
 Due to limitations in the Java SE nio support, a
 SocketChannel must be used instead
 of a Socket to connect to the server.  However,
 SocketChannels don't support all the features of Sockets, such as connecting
 through a SOCKS proxy server.  SocketChannels also don't support
 simultaneous read and write, which means that the
 idle method can't be used if
 SocketChannels are being used; use this IdleManager instead.
 To enable support for SocketChannels instead of Sockets, set the
 mail.imap.usesocketchannels property in the Session used to
 access the IMAP Folder.  (Or mail.imaps.usesocketchannels if
 you're using the "imaps" protocol.)  This will effect all connections in
 that Session, but you can create another Session without this property set
 if you need to use the features that are incompatible with SocketChannels.
 
NOTE: The IdleManager, and all APIs and properties related to it, should be considered EXPERIMENTAL. They may be changed in the future in ways that are incompatible with applications using the current APIs.
| Constructor and Description | 
|---|
| IdleManager(Session session,
           Executor es)Create an IdleManager. | 
| Modifier and Type | Method and Description | 
|---|---|
| boolean | isRunning()Is the IdleManager currently running?  The IdleManager starts
 running when the Executor schedules its task. | 
| void | stop()Stop the IdleManager. | 
| void | watch(Folder folder)Watch the Folder for new messages and other events using the IMAP IDLE
 command. | 
public IdleManager(Session session, Executor es) throws IOException
session - the Session containing configuration informationes - the Executor used to create threadsIOException - for Selector failurespublic boolean isRunning()
stop method, or if it terminates abnormally due
 to an unexpected error.public void watch(Folder folder) throws MessagingException
folder - the folder to watchMessagingException - for errors related to the folderpublic void stop()
Copyright © 2019 JBoss by Red Hat. All rights reserved.