-
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
1 parent
852c121
commit 9b88197
Showing
2 changed files
with
74 additions
and
0 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
70 changes: 70 additions & 0 deletions
70
latte/src/main/java/gg/beemo/latte/broker/rabbitmq/RabbitConnection.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,70 @@ | ||
package gg.beemo.latte.broker.rabbitmq | ||
|
||
import com.rabbitmq.client.Address | ||
import com.rabbitmq.client.Channel | ||
import com.rabbitmq.client.Connection | ||
import com.rabbitmq.client.ConnectionFactory | ||
import gg.beemo.latte.broker.BrokerConnection | ||
import gg.beemo.latte.broker.BrokerMessageHeaders | ||
import gg.beemo.latte.broker.MessageId | ||
import gg.beemo.latte.logging.Log | ||
import java.util.Collections | ||
|
||
// TODO Implementation considerations: | ||
// - Channels are not thread-safe (for sending); need to create a coroutine-wrapper around them | ||
// - "Consuming in one thread and publishing in another thread on a shared channel can be safe." | ||
|
||
class RabbitConnection( | ||
rabbitHosts: Array<String>, | ||
override val serviceName: String, | ||
override val instanceId: String, | ||
private val useTls: Boolean = false, | ||
) : BrokerConnection() { | ||
|
||
override val supportsTopicHotSwap = true | ||
private val rabbitAddresses = rabbitHosts.map(Address::parseAddress) | ||
private val log by Log | ||
|
||
private var connection: Connection? = null | ||
private val channels = Collections.synchronizedMap(HashMap<String, Channel>()) | ||
|
||
override suspend fun start() { | ||
connection = ConnectionFactory().apply { | ||
if (useTls) { | ||
// TODO This will trust every cert, even self-signed ones | ||
useSslProtocol() | ||
} | ||
}.newConnection(rabbitAddresses, instanceId) | ||
// TODO Create exchange | ||
} | ||
|
||
override suspend fun abstractSend( | ||
topic: String, | ||
key: String, | ||
value: String, | ||
headers: BrokerMessageHeaders | ||
): MessageId { | ||
TODO() | ||
} | ||
|
||
override fun destroy() { | ||
log.debug("Destroying RabbitConnection") | ||
connection?.close() | ||
connection = null | ||
super.destroy() | ||
} | ||
|
||
override fun createTopic(topic: String) { | ||
val channel = channels.computeIfAbsent(topic) { | ||
val connection = checkNotNull(connection) { "Connection not open" } | ||
connection.createChannel() | ||
} | ||
// TODO Consume | ||
} | ||
|
||
override fun removeTopic(topic: String) { | ||
val channel = channels.remove(topic) | ||
channel?.close() | ||
} | ||
|
||
} |