first version doiing forward with attached original mail
This commit is contained in:
@@ -2,7 +2,6 @@ package de.srsoftware.widerhall.mail;
|
||||
|
||||
import com.sun.mail.imap.IMAPFolder;
|
||||
import de.srsoftware.widerhall.Constants;
|
||||
import org.json.simple.JSONObject;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
@@ -15,26 +14,38 @@ public class ImapClient {
|
||||
private static final Logger LOG = LoggerFactory.getLogger(ImapClient.class);
|
||||
private final int port;
|
||||
private final String host, username, password, folderName;
|
||||
private boolean stopped = true;
|
||||
private IMAPFolder inbox;
|
||||
private HashSet<MessageHandler> listeners = new HashSet<>();
|
||||
private ListeningThread listeningThread;
|
||||
private Heartbeat heartbeat;
|
||||
|
||||
private class ListeningThread extends Thread {
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(ListeningThread.class);
|
||||
private HashSet<MessageHandler> listeners = new HashSet<>();
|
||||
private boolean stopped = false;
|
||||
|
||||
public ListeningThread addListener(MessageHandler messageHandler) {
|
||||
listeners.add(messageHandler);
|
||||
return this;
|
||||
}
|
||||
|
||||
public ListeningThread dropListeners() {
|
||||
listeners.clear();
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
while (!stopped) {
|
||||
try {
|
||||
sleep(5000);
|
||||
} catch (InterruptedException interruptedException) {
|
||||
interruptedException.printStackTrace();
|
||||
}
|
||||
try {
|
||||
openInbox();
|
||||
} catch (MessagingException e){
|
||||
LOG.warn("Connection problem:",e);
|
||||
try {
|
||||
sleep(1000);
|
||||
} catch (InterruptedException interruptedException) {
|
||||
interruptedException.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -78,11 +89,18 @@ public class ImapClient {
|
||||
props.put(Constants.PROTOCOL,Constants.IMAPS);
|
||||
return props;
|
||||
}
|
||||
|
||||
public ListeningThread doStop() {
|
||||
stopped = true;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
private class Heartbeat extends Thread{
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(Heartbeat.class);
|
||||
private boolean stopped = false;
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
while (!stopped){
|
||||
@@ -99,6 +117,11 @@ public class ImapClient {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public Heartbeat doStop() {
|
||||
stopped = true;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
public ImapClient(String host, int port, String username, String password, String folderName) {
|
||||
@@ -109,9 +132,9 @@ public class ImapClient {
|
||||
this.folderName = folderName;
|
||||
}
|
||||
|
||||
|
||||
public ImapClient addListener(MessageHandler messageHandler) {
|
||||
listeners.add(messageHandler);
|
||||
if (listeningThread == null) listeningThread = new ListeningThread();
|
||||
listeningThread.addListener(messageHandler);
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -135,15 +158,28 @@ public class ImapClient {
|
||||
return folderName;
|
||||
}
|
||||
|
||||
public void start() {
|
||||
stopped = false;
|
||||
public ImapClient start() {
|
||||
stop();
|
||||
|
||||
LOG.debug("Creating ListeningThread…");
|
||||
new ListeningThread().start();
|
||||
(listeningThread = new ListeningThread()).start();
|
||||
|
||||
LOG.debug("Creating Heartbeat…");
|
||||
new Heartbeat().start();
|
||||
(heartbeat = new Heartbeat()).start();
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
public void stop(){
|
||||
stopped = true;
|
||||
public ImapClient stop(){
|
||||
if (listeningThread != null) {
|
||||
LOG.debug("Stopping old ListeningThread…");
|
||||
listeningThread.dropListeners().doStop();
|
||||
listeningThread = null;
|
||||
}
|
||||
if (heartbeat != null){
|
||||
heartbeat.doStop();
|
||||
heartbeat = null;
|
||||
}
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user