4
4
import io .github .trashemail .EmailsToTelegramService .Configuration .ImapClientServiceConfig ;
5
5
import com .sun .mail .imap .IMAPFolder ;
6
6
import com .sun .mail .imap .IMAPStore ;
7
+ import com .sun .mail .imap .protocol .IMAPProtocol ;
8
+ import com .sun .mail .iap .ProtocolException ;
7
9
8
10
import org .slf4j .Logger ;
9
11
import org .slf4j .LoggerFactory ;
@@ -31,13 +33,13 @@ public class ImapClient {
31
33
32
34
private static final Logger log = LoggerFactory .getLogger (ImapClient .class );
33
35
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 ;
38
40
39
41
@ PostConstruct
40
- public void init (){
42
+ public void init () {
41
43
username = imapClientServiceConfig .getImap ().getEmail ();
42
44
password = imapClientServiceConfig .getImap ().getPassword ();
43
45
imapHost = imapClientServiceConfig .getImap ().getHost ();
@@ -82,11 +84,27 @@ public void messagesAdded(MessageCountEvent event) {
82
84
}
83
85
});
84
86
85
- IdleThread idleThread = new IdleThread ( inbox );
86
- idleThread . setDaemon ( false );
87
+ Thread idleThread = new Thread ( new KeepAliveRunnable (( IMAPFolder ) inbox ) );
88
+
87
89
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
+ }
88
107
89
- idleThread .join ();
90
108
} catch (Exception e ) {
91
109
e .printStackTrace ();
92
110
} finally {
@@ -95,37 +113,35 @@ public void messagesAdded(MessageCountEvent event) {
95
113
}
96
114
}
97
115
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 {
101
117
102
- public IdleThread (Folder folder ) {
103
- super ();
104
- this .folder = folder ;
105
- }
118
+ private static final long KEEP_ALIVE_FREQ = 120000 ; // 2 minutes
106
119
107
- public synchronized void kill () {
120
+ private IMAPFolder folder ;
108
121
109
- if (!running )
110
- return ;
111
- this .running = false ;
122
+ public KeepAliveRunnable (IMAPFolder folder ) {
123
+ this .folder = folder ;
112
124
}
113
125
114
126
@ Override
115
127
public void run () {
116
- while (running ) {
128
+ while (! Thread . interrupted () ) {
117
129
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 );
127
144
}
128
-
129
145
}
130
146
}
131
147
}
@@ -151,7 +167,7 @@ public static void close(final Store store) {
151
167
}
152
168
153
169
public static void ensureOpen (final Folder folder )
154
- throws MessagingException {
170
+ throws MessagingException {
155
171
156
172
if (folder != null ) {
157
173
Store store = folder .getStore ();
@@ -168,7 +184,7 @@ public static void ensureOpen(final Folder folder)
168
184
folder .open (Folder .READ_ONLY );
169
185
if (!folder .isOpen ())
170
186
throw new MessagingException ("Unable to open folder " +
171
- folder .getFullName ());
187
+ folder .getFullName ());
172
188
}
173
189
174
190
}
0 commit comments