Implemented listenForNewReceivedEInvoices()

This commit is contained in:
dankito 2024-11-21 16:50:49 +01:00
parent a6ac33d8ed
commit 07fdbec5d7
3 changed files with 60 additions and 2 deletions

View File

@ -8,6 +8,8 @@ kotlin {
} }
val kotlinCoroutinesVersion: String by project
val mustangVersion: String by project val mustangVersion: String by project
val angusMailVersion: String by project val angusMailVersion: String by project
@ -19,6 +21,8 @@ val xunitVersion: String by project
val logbackVersion: String by project val logbackVersion: String by project
dependencies { dependencies {
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:$kotlinCoroutinesVersion")
implementation("org.mustangproject:library:$mustangVersion") implementation("org.mustangproject:library:$mustangVersion")
implementation("org.mustangproject:validator:$mustangVersion") implementation("org.mustangproject:validator:$mustangVersion")

View File

@ -2,29 +2,81 @@ package net.codinux.invoicing.mail
import jakarta.mail.BodyPart import jakarta.mail.BodyPart
import jakarta.mail.Folder import jakarta.mail.Folder
import jakarta.mail.Message
import jakarta.mail.Part import jakarta.mail.Part
import jakarta.mail.Session import jakarta.mail.Session
import jakarta.mail.Store import jakarta.mail.Store
import jakarta.mail.event.MessageCountAdapter
import jakarta.mail.event.MessageCountEvent
import jakarta.mail.internet.MimeMultipart import jakarta.mail.internet.MimeMultipart
import kotlinx.coroutines.*
import net.codinux.invoicing.model.Invoice import net.codinux.invoicing.model.Invoice
import net.codinux.invoicing.reader.EInvoiceReader import net.codinux.invoicing.reader.EInvoiceReader
import net.codinux.log.logger import net.codinux.log.logger
import org.eclipse.angus.mail.imap.IMAPFolder
import java.io.File import java.io.File
import java.time.LocalDate import java.time.LocalDate
import java.time.ZoneId import java.time.ZoneId
import java.util.* import java.util.*
import java.util.concurrent.Executors
class MailReader( class MailReader(
private val eInvoiceReader: EInvoiceReader = EInvoiceReader() private val eInvoiceReader: EInvoiceReader = EInvoiceReader()
) { ) {
private val mailDispatcher = Executors.newCachedThreadPool().asCoroutineDispatcher()
private val log by logger() private val log by logger()
fun listAllMessagesWithEInvoice(account: MailAccount): List<MailWithInvoice> { fun listenForNewReceivedEInvoices(account: MailAccount, emailFolderName: String = "INBOX", eInvoiceReceived: (MailWithInvoice) -> Unit) = runBlocking {
try { try {
connect(account) { store -> connect(account) { store ->
val inbox = store.getFolder("INBOX") val folder = store.getFolder(emailFolderName)
folder.open(Folder.READ_ONLY)
folder.addMessageCountListener(object : MessageCountAdapter() {
override fun messagesAdded(event: MessageCountEvent) {
event.messages.forEach { message ->
findEInvoice(message)?.let {
eInvoiceReceived(it)
}
}
}
})
launch(mailDispatcher) {
keepConnectionOpen(account, folder)
}
}
} catch (e: Throwable) {
log.error(e) { "Listening to new received eInvoices of '${account.username}' failed" }
}
log.info { "Stopped listening to new received eInvoices of '${account.username}'" }
}
private suspend fun keepConnectionOpen(account: MailAccount, folder: Folder) {
log.info { "Listening to new mails of ${account.username}" }
// Use IMAP IDLE to keep the connection alive
while (true) {
if (!folder.isOpen) {
log.info { "Reopening inbox of ${account.username} ..." }
folder.open(Folder.READ_ONLY)
}
(folder as IMAPFolder).idle()
delay(250)
}
}
fun listAllMessagesWithEInvoice(account: MailAccount, emailFolderName: String = "INBOX"): List<MailWithInvoice> {
try {
connect(account) { store ->
val inbox = store.getFolder(emailFolderName)
inbox.open(Folder.READ_ONLY) inbox.open(Folder.READ_ONLY)
listAllMessagesWithEInvoiceInFolder(inbox).also { listAllMessagesWithEInvoiceInFolder(inbox).also {

View File

@ -5,7 +5,9 @@ org.gradle.parallel=true
# Quarkus 3.12 requires Kotlin 2.0 # Quarkus 3.12 requires Kotlin 2.0
#kotlinVersion=1.9.25 #kotlinVersion=1.9.25
#kotlinCoroutinesVersion=1.8.1
kotlinVersion=2.0.20 kotlinVersion=2.0.20
kotlinCoroutinesVersion=1.9.0
quarkusVersion=3.16.3 quarkusVersion=3.16.3