-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
234 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
...c/main/java/com/disposableemail/apache/james/mailet/producer/kafka/KafkaProducerMailet.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package com.disposableemail.apache.james.mailet.producer.kafka | ||
|
||
import org.apache.kafka.clients.admin.AdminClient | ||
import org.apache.kafka.clients.admin.AdminClientConfig | ||
import org.apache.kafka.clients.admin.NewTopic | ||
import org.apache.kafka.clients.producer.ProducerConfig | ||
import org.apache.kafka.common.serialization.StringSerializer | ||
import org.apache.mailet.Mail | ||
import org.apache.mailet.base.GenericMailet | ||
import org.slf4j.Logger | ||
import org.slf4j.LoggerFactory | ||
import reactor.core.publisher.Flux | ||
import reactor.kafka.sender.KafkaSender | ||
import reactor.kafka.sender.SenderOptions | ||
import reactor.kafka.sender.SenderRecord | ||
import java.util.* | ||
|
||
|
||
class KafkaProducerMailet : GenericMailet() { | ||
|
||
private val logger: Logger = LoggerFactory.getLogger(KafkaProducerMailet::class.java) | ||
private lateinit var bootstrapServer: String | ||
private lateinit var topic: String | ||
private lateinit var partitions: List<Int> | ||
private var replication: Short = 0 | ||
|
||
override fun init() { | ||
logger.info("${javaClass.simpleName} Initializing..") | ||
|
||
bootstrapServer = getInitParameter("bootstrapServer") | ||
topic = getInitParameter("topicName") | ||
partitions = (0..<getInitParameter("partitions").toInt()).toList() | ||
replication = getInitParameter("replication").toShort() | ||
val config = Properties().apply { | ||
this[AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG] = bootstrapServer | ||
} | ||
|
||
AdminClient.create(config).use { admin -> | ||
val topicNames = admin.listTopics().names().get() | ||
if (topic !in topicNames) { | ||
createTopic(admin) | ||
} | ||
} | ||
} | ||
|
||
private fun createTopic(admin: AdminClient) { | ||
val newTopic = NewTopic(topic, partitions.size, replication).configs(emptyMap()) | ||
admin.createTopics(listOf(newTopic)).apply { | ||
logger.info("Topic $topic not exists, created new") | ||
} | ||
} | ||
|
||
override fun service(mail: Mail?) { | ||
mail?.let { | ||
logger.info("Mail received: ${mail.message.messageID}") | ||
|
||
val senderOptions = SenderOptions.create<String, String>( | ||
mapOf( | ||
ProducerConfig.BOOTSTRAP_SERVERS_CONFIG to bootstrapServer, | ||
ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG to StringSerializer::class.java, | ||
ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG to StringSerializer::class.java | ||
) | ||
) | ||
|
||
KafkaSender.create(senderOptions).send(Flux.fromIterable(it.recipients) | ||
.map { address -> | ||
SenderRecord.create( | ||
topic, | ||
partitions.first(), | ||
it.message.sentDate.time, | ||
it.message.messageID, | ||
address.asString(), | ||
it.message.messageID | ||
) | ||
} | ||
) | ||
.doOnError { e: Any -> logger.info("Send message to Kafka failed: $e") } | ||
.doOnNext { _: Any -> logger.info("Message sent to Kafka: " + it.message.messageID) } | ||
.subscribe() | ||
} | ||
} | ||
|
||
override fun getMailetInfo(): String { | ||
return "Disposable email Kafka producer mailet" | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
...st/java/com/disposableemail/apache/james/mailet/producer/kafka/KafkaProducerMailetTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package com.disposableemail.apache.james.mailet.producer.kafka | ||
|
||
import org.apache.james.core.MailAddress | ||
import org.apache.mailet.base.test.FakeMail | ||
import org.apache.mailet.base.test.FakeMailetConfig | ||
import org.junit.jupiter.api.Assertions.* | ||
import org.junit.jupiter.api.BeforeEach | ||
import org.junit.jupiter.api.Disabled | ||
import org.junit.jupiter.api.Test | ||
import java.io.FileInputStream | ||
import javax.mail.MessagingException | ||
import javax.mail.internet.MimeMessage | ||
|
||
@Disabled | ||
class KafkaProducerMailetTest { | ||
|
||
lateinit var mailetConfig: FakeMailetConfig | ||
lateinit var mailet: KafkaProducerMailet | ||
|
||
@BeforeEach | ||
@Throws(Exception::class) | ||
fun setUp() { | ||
mailetConfig = FakeMailetConfig.builder() | ||
.mailetName("KafkaProducerMailet") | ||
.setProperty("bootstrapServer", "localhost:9092") | ||
.setProperty("topicName", "disposable-email-received") | ||
.setProperty("partitions", "2") | ||
.setProperty("replication", "1") | ||
.build() | ||
|
||
mailet = KafkaProducerMailet() | ||
mailet.init(mailetConfig) | ||
|
||
} | ||
|
||
@Test | ||
@Throws(MessagingException::class) | ||
fun mailetShouldNotCreateDocumentWhenMailIsEmpty() { | ||
val mimeMessage = MimeMessage(null, FileInputStream("src/test/resources/test_mail_html_no_attachments.eml")) | ||
val mail = FakeMail.defaultFakeMail() | ||
mail.recipients.add(MailAddress("test@gmail.com")) | ||
mail.recipients.add(MailAddress("test1@gmail.com")) | ||
mail.message = mimeMessage | ||
val mailet = KafkaProducerMailet() | ||
|
||
mailet.init() | ||
mailet.service(mail) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters