Skip to content

Commit

Permalink
javamail connection handling optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
j-dimension committed Jan 25, 2025
1 parent 8c93e9d commit 0a9153c
Show file tree
Hide file tree
Showing 15 changed files with 375 additions and 328 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -932,6 +932,7 @@ private static boolean openFolder(Folder f) throws Exception {
closed = !f.isOpen();
}
if (closed && f != null) {
System.out.println("open 19");
f.open(Folder.READ_WRITE);
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -872,6 +872,7 @@ Message[] getUncachedMessages(Message[] messages) {

try {
if (!(this.folder.isOpen())) {
System.out.println("open 20");
this.folder.open(Folder.READ_WRITE);
}
} catch (Exception ex) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -685,6 +685,9 @@ public IDLEThread(Folder f) {

public void cancel() {
this.cancel = true;
System.out.println("close 21a");
EmailUtils.closeIfIMAP(f);

}

@Override
Expand All @@ -697,14 +700,15 @@ public void run() {
while (!this.cancel) {
try {
if (!f.isOpen()) {
System.out.println("open 21");
f.open(Folder.READ_WRITE);
}
} catch (Throwable t) {
log.error("folder already open?", t);
}
try {
((IMAPFolder) f).idle();
Thread.sleep(500);
//Thread.sleep(500);
} catch (MessagingException mex) {
log.error("IMAP IDLE not supported - cancelling IDLE thread: " + mex.getMessage());
return;
Expand All @@ -713,6 +717,9 @@ public void run() {
}

}
log.info("IDLE thread cancelled");
System.out.println("close 21b");
EmailUtils.closeIfIMAP(f);

} catch (Throwable t) {
log.error("Could not enter IDLE mode for folder", t);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -698,12 +698,14 @@ public void messageChanged(MessageChangedEvent mce) {

this.lastUpdateTimeUnreadMessageCount=now;
try {
final int unread = this.parent.getInboxUnread();
EventBroker eb = EventBroker.getInstance();
eb.publishEvent(new EmailStatusEvent(unread));
this.parent.renderInboxNode(this.ms);
if(mce.getMessageChangeType()!=MessageChangedEvent.FLAGS_CHANGED)
if(mce.getMessageChangeType()==MessageChangedEvent.FLAGS_CHANGED) {
final int unread = this.parent.getInboxUnread();
EventBroker eb = EventBroker.getInstance();
eb.publishEvent(new EmailStatusEvent(unread));
this.parent.renderInboxNode(this.ms);
} else {
this.parent.reloadInboxNode(ms);
}
} catch (Exception ex) {
log.error("Unable to handle messageChanged event", ex);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -744,6 +744,7 @@ public int getMin() {
public boolean execute() throws Exception {
try {
if (!(f.getFolder().isOpen())) {
System.out.println("open 22");
f.getFolder().open(Folder.READ_WRITE);
}

Expand Down Expand Up @@ -867,6 +868,7 @@ public boolean execute() throws Exception {
}

if (!(f.getFolder().isOpen())) {
System.out.println("open 23");
f.getFolder().open(Folder.READ_WRITE);
}

Expand Down Expand Up @@ -948,20 +950,21 @@ public boolean execute() throws Exception {
}
ComponentUtils.autoSizeColumns(table, 0);
});

} catch (Throwable ex) {
log.error(ex);
return false;
} finally {
new Thread(() -> {
try {
Thread.sleep(5000);
if (f.getFolder().isOpen()) {
//Thread.sleep(5000);
if (!EmailUtils.isInbox(f.getFolder()) && f.getFolder().isOpen()) {
System.out.println("close 22");
EmailUtils.closeIfIMAP(f.getFolder());
}
} catch (Throwable t) {
log.error(t);
}
}).start();
} catch (Throwable ex) {
log.error(ex);
return false;
}
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -732,6 +732,7 @@ private int getMessageCount() {
public void run() {
try {
if (!(f.getFolder().isOpen())) {
System.out.println("open 24");
f.getFolder().open(Folder.READ_WRITE);
}

Expand Down Expand Up @@ -805,6 +806,7 @@ public void run() {
.toArray(Message[]::new);

if (!(f.getFolder().isOpen())) {
System.out.println("open 25");
f.getFolder().open(Folder.READ_WRITE);
}
if (f.getFolder() instanceof UIDFolder) {
Expand All @@ -815,11 +817,13 @@ public void run() {
}

if (!(f.getFolder().isOpen())) {
System.out.println("open 26");
f.getFolder().open(Folder.READ_WRITE);
}
Message[] uncachedMessages = f.getUncachedMessages(messages);

if (!(f.getFolder().isOpen())) {
System.out.println("open 27");
f.getFolder().open(Folder.READ_WRITE);
}
FetchProfile fp = new FetchProfile();
Expand Down Expand Up @@ -851,6 +855,7 @@ public void run() {
}

if (!(f.getFolder().isOpen())) {
System.out.println("open 28");
f.getFolder().open(Folder.READ_WRITE);
}

Expand Down Expand Up @@ -925,26 +930,37 @@ public void run() {

SwingUtilities.invokeLater(() -> {
try {
table.getRowSorter().toggleSortOrder(this.sortCol);
if(this.sortCol<0) {
log.info("invalid sort column: " + this.sortCol + " using column 3");
this.sortCol=3;
}
if(this.sortCol+1>table.getModel().getColumnCount()) {
log.info("invalid sort column: " + this.sortCol + " - skip sorting");
} else {
table.getRowSorter().toggleSortOrder(this.sortCol);
}


} catch (Throwable t) {
log.error("Error sorting mails", t);
log.error("Error sorting mails by column " + this.sortCol + " with " + table.getModel().getColumnCount() + " columns available", t);
}
ComponentUtils.autoSizeColumns(table, 0);
});

} catch (Throwable ex) {
log.error(ex);
} finally {
new Thread(() -> {
try {
Thread.sleep(5000);
if (f.getFolder().isOpen()) {
//Thread.sleep(5000);
if (!EmailUtils.isInbox(f.getFolder()) && f.getFolder().isOpen()) {
System.out.println("close 22.b");
EmailUtils.closeIfIMAP(f.getFolder());
}
} catch (Throwable t) {
log.error(t);
}
}).start();
} catch (Throwable ex) {
log.error(ex);
}
}
}
Loading

0 comments on commit 0a9153c

Please sign in to comment.