CyborgBeta
Commander
- Registriert
- Jan. 2021
- Beiträge
- 3.060
Hi, ich hab den folgenden Code, um E-Mails mehrerer Accounts zu lesen und darzustellen:
Dependencies:
Ich möchte den gerne de-synchronisieren, es soll also alles sequenziell, anstatt parallel ablaufen. Wie bekomme ich denn am besten die
Danke
Java:
import jakarta.mail.*;
import jakarta.mail.internet.MimeMultipart;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.concurrent.CyclicBarrier;
import javax.swing.*;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.safety.Safelist;
public class Main {
record Account(
int index, String host, int port, String user, String password, String inboxName) {}
private static final List<Account> accounts =
List.of(
new Account(1, "", 993, "", "", "INBOX"),
new Account(2, "", 993, "", "", "INBOX"));
private static final Map<Account, List<Message>> messages = new LinkedHashMap<>();
private static final int max_mails = 5;
private static volatile int state = 1;
private static final CyclicBarrier barrier =
new CyclicBarrier(
accounts.size(),
() -> {
try {
int i1 = 1;
List<Message> tempList = new ArrayList<>();
for (Map.Entry<Account, List<Message>> e : messages.entrySet()) {
System.out.println(e.getKey().index() + " " + e.getKey().host() + ":");
for (Message m : e.getValue()) {
System.out.println(
i1++ + " " + getDate(m) + " " + getFrom(m) + " " + getSubject(m));
// System.out.println(getContent(m).lines().findFirst().orElse(""));
tempList.add(m);
}
System.out.println();
}
System.out.println("a or a-b or empty or r or all:");
while (true) {
String line = new Scanner(System.in, StandardCharsets.UTF_8).nextLine();
if (line == null || line.isBlank()) {
state = 0;
return;
}
if (line.equals("r")) {
state = 1;
return;
}
if (line.equals("all")) {
for (Message m : tempList) {
display1(m);
}
continue;
}
if (line.contains("-")) {
int a = Integer.parseInt(line.split("-")[0]);
int b = Integer.parseInt(line.split("-")[1]);
for (int i = a; i <= b; i++) {
display1(tempList.get(i - 1));
}
continue;
}
int i2 = Integer.parseInt(line);
display1(tempList.get(i2 - 1));
}
} catch (Exception e) {
throw new RuntimeException(e);
}
});
public static void main(String[] args) {
for (Account ac : accounts) {
new Thread(
() -> {
try {
receive10(ac);
} catch (Exception e) {
throw new RuntimeException(e);
}
})
.start();
}
}
private static void receive10(Account ac) throws Exception {
Store emailStore = null;
Folder emailFolder = null;
Properties properties = new Properties();
properties.put("mail.store.protocol", "imap");
properties.put("mail.imap.ssl.enable", true);
properties.put("mail.imap.host", ac.host());
properties.put("mail.imap.port", ac.port());
Session emailSession = Session.getInstance(properties);
try {
emailStore = emailSession.getStore();
emailStore.connect(ac.user(), ac.password());
System.out.println("Connected: " + ac.host());
emailFolder = emailStore.getFolder(ac.inboxName());
emailFolder.open(Folder.READ_ONLY);
while (state != 0) {
System.out.println("Reading: " + ac.host());
List<Message> l = new ArrayList<>();
int c1 = emailFolder.getMessageCount();
int c2 = 0;
for (int i = c1; i > 0 && c2 < max_mails; i--, c2++) {
l.add(emailFolder.getMessage(i));
}
messages.put(ac, l);
barrier.await();
}
} finally {
if (emailFolder != null && emailFolder.isOpen()) {
emailFolder.close(false);
}
if (emailStore != null && emailStore.isConnected()) {
emailStore.close();
}
System.out.println("Disconnected: " + ac.host());
}
}
private static void display1(Message message) throws Exception {
String sb =
getDate(message)
+ "\n\n"
+ getFrom(message)
+ "\n\n"
+ getSubject(message)
+ "\n\n"
+ getContent(message);
JFrame f = new JFrame();
JTextArea ta = new JTextArea(sb);
ta.setLineWrap(true);
f.getContentPane().add(new JScrollPane(ta));
f.setSize(600, 400);
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.setVisible(true);
}
private static String getDate(Message message) throws Exception {
return message.getSentDate().toString();
}
private static String getFrom(Message message) throws Exception {
StringBuilder sb = new StringBuilder();
for (Address a : message.getFrom()) {
sb.append(a.toString());
}
return sb.toString();
}
private static String getSubject(Message message) throws Exception {
return message.getSubject();
}
private static String getContent(Message message) throws Exception {
StringBuilder sb = new StringBuilder();
if (message.isMimeType("text/plain")) {
sb.append(message.getContent().toString());
}
if (message.isMimeType("multipart/*")) {
MimeMultipart mimeMultipart = (MimeMultipart) message.getContent();
for (int i = 0; i < mimeMultipart.getCount(); i++) {
Document document = Jsoup.parse(mimeMultipart.getBodyPart(i).getContent().toString());
document.outputSettings(new Document.OutputSettings().prettyPrint(false));
document.select("br").append("\\n");
document.select("p").prepend("\\n\\n");
String s = document.html().replaceAll("\\\\n", "\n");
sb.append(
Jsoup.clean(s, "", Safelist.none(), new Document.OutputSettings().prettyPrint(false)));
}
}
return sb.toString();
}
}
Dependencies:
Code:
dependencies {
// https://mvnrepository.com/artifact/com.sun.mail/jakarta.mail
implementation 'com.sun.mail:jakarta.mail:2.0.1'
// https://mvnrepository.com/artifact/org.jsoup/jsoup
implementation 'org.jsoup:jsoup:1.18.1'
}
Ich möchte den gerne de-synchronisieren, es soll also alles sequenziell, anstatt parallel ablaufen. Wie bekomme ich denn am besten die
CyclicBarrier
"weg"?Danke