44import io .github .trashemail .EmailsToTelegramService .Configuration .ImapClientServiceConfig ;
55import com .sun .mail .imap .IMAPFolder ;
66import com .sun .mail .imap .IMAPStore ;
7+ import com .sun .mail .imap .protocol .IMAPProtocol ;
8+ import com .sun .mail .iap .ProtocolException ;
79
810import org .slf4j .Logger ;
911import org .slf4j .LoggerFactory ;
@@ -31,13 +33,13 @@ public class ImapClient {
3133
3234 private static final Logger log = LoggerFactory .getLogger (ImapClient .class );
3335
34- private static String username ;
35- private static String password ;
36- private static String imapHost ;
37- private static String imapPort ;
36+ private static String username ;
37+ private static String password ;
38+ private static String imapHost ;
39+ private static String imapPort ;
3840
3941 @ PostConstruct
40- public void init (){
42+ public void init () {
4143 username = imapClientServiceConfig .getImap ().getEmail ();
4244 password = imapClientServiceConfig .getImap ().getPassword ();
4345 imapHost = imapClientServiceConfig .getImap ().getHost ();
@@ -82,11 +84,27 @@ public void messagesAdded(MessageCountEvent event) {
8284 }
8385 });
8486
85- IdleThread idleThread = new IdleThread ( inbox );
86- idleThread . setDaemon ( false );
87+ Thread idleThread = new Thread ( new KeepAliveRunnable (( IMAPFolder ) inbox ) );
88+
8789 idleThread .start ();
90+ while (!Thread .interrupted ()) {
91+ try {
92+ ensureOpen (inbox );
93+ log .info ("IMAP client: IDLE Listening state ..." );
94+ ((IMAPFolder ) inbox ).idle ();
95+ } catch (Exception e ) {
96+ e .printStackTrace ();
97+ try {
98+ Thread .sleep (100 );
99+ } catch (InterruptedException e1 ) {
100+ }
101+ }
102+ }
103+
104+ if (idleThread .isAlive ()) {
105+ idleThread .interrupt ();
106+ }
88107
89- idleThread .join ();
90108 } catch (Exception e ) {
91109 e .printStackTrace ();
92110 } finally {
@@ -95,37 +113,35 @@ public void messagesAdded(MessageCountEvent event) {
95113 }
96114 }
97115
98- private static class IdleThread extends Thread {
99- private final Folder folder ;
100- private volatile boolean running = true ;
116+ private static class KeepAliveRunnable implements Runnable {
101117
102- public IdleThread (Folder folder ) {
103- super ();
104- this .folder = folder ;
105- }
118+ private static final long KEEP_ALIVE_FREQ = 120000 ; // 2 minutes
106119
107- public synchronized void kill () {
120+ private IMAPFolder folder ;
108121
109- if (!running )
110- return ;
111- this .running = false ;
122+ public KeepAliveRunnable (IMAPFolder folder ) {
123+ this .folder = folder ;
112124 }
113125
114126 @ Override
115127 public void run () {
116- while (running ) {
128+ while (! Thread . interrupted () ) {
117129 try {
118- ensureOpen (folder );
119- log .info ("IMAP client: IDLE Listening state ..." );
120- ((IMAPFolder ) folder ).idle ();
121- } catch (Exception e ) {
122- e .printStackTrace ();
123- try {
124- Thread .sleep (100 );
125- } catch (InterruptedException e1 ) {
126- }
130+ Thread .sleep (KEEP_ALIVE_FREQ );
131+
132+ // Perform a NOOP just to keep alive the connection
133+ log .debug ("Performing a NOOP to keep alive the connection" );
134+ folder .doCommand (new IMAPFolder .ProtocolCommand () {
135+ public Object doCommand (IMAPProtocol protocol )
136+ throws ProtocolException {
137+ protocol .simpleCommand ("NOOP" , null );
138+ return null ;
139+ }
140+ });
141+ } catch (InterruptedException e ) {
142+ } catch (MessagingException e ) {
143+ log .warn ("Unexpected exception while keeping alive the IDLE connection" , e );
127144 }
128-
129145 }
130146 }
131147 }
@@ -151,7 +167,7 @@ public static void close(final Store store) {
151167 }
152168
153169 public static void ensureOpen (final Folder folder )
154- throws MessagingException {
170+ throws MessagingException {
155171
156172 if (folder != null ) {
157173 Store store = folder .getStore ();
@@ -168,7 +184,7 @@ public static void ensureOpen(final Folder folder)
168184 folder .open (Folder .READ_ONLY );
169185 if (!folder .isOpen ())
170186 throw new MessagingException ("Unable to open folder " +
171- folder .getFullName ());
187+ folder .getFullName ());
172188 }
173189
174190 }
0 commit comments