-
Notifications
You must be signed in to change notification settings - Fork 645
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Jms per message destination #1181
Merged
ennru
merged 12 commits into
akka:master
from
andreas-schroeder:jms-per-message-destination
Sep 10, 2018
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
b41da8d
Extend Jms message model
andreas-sa-schroeder a12f348
Implement flow with per-message destinations
andreas-sa-schroeder 53bce20
Reduce synchronization overhead of destination cache
andreas-sa-schroeder f10cdce
Fix Scala 2.11 compilation issue of SoftReference
andreas-sa-schroeder 0467181
Fix Scala 2.11 compilation, add copyright headers
andreas-sa-schroeder ec1260f
Improve runtime of SoftReferenceCacheSpec
andreas-sa-schroeder 1c829ac
Parallelize Jms session creation
andreas-sa-schroeder c6dfc84
Pull in on session creation only if out was pulled
andreas-sa-schroeder 9f50733
Simplify and explain async JmsProducer startup logic
andreas-sa-schroeder 467d102
Simplify JmsMessage model and fallback to default destination
andreas-sa-schroeder bf30b5d
Improve PR according to review comments
andreas-sa-schroeder 050b3af
Remove synchronization on SoftReferenceCache
andreas-sa-schroeder File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,39 +6,38 @@ package akka.stream.alpakka.jms | |
|
||
import java.util.concurrent.ArrayBlockingQueue | ||
|
||
import javax.jms | ||
import javax.jms._ | ||
import akka.stream.{ActorAttributes, ActorMaterializer, Attributes} | ||
import akka.stream.alpakka.jms.impl.SoftReferenceCache | ||
import akka.stream.stage.{AsyncCallback, GraphStageLogic} | ||
import akka.stream.{ActorAttributes, ActorMaterializer, Attributes} | ||
import javax.jms | ||
|
||
import scala.concurrent.{ExecutionContext, Future} | ||
|
||
/** | ||
* Internal API | ||
*/ | ||
private[jms] trait JmsConnector { this: GraphStageLogic => | ||
private[jms] trait JmsConnector[S <: JmsSession] { this: GraphStageLogic => | ||
|
||
implicit protected var ec: ExecutionContext = _ | ||
|
||
protected var jmsConnection: Option[Connection] = None | ||
protected var jmsConnection: Option[jms.Connection] = None | ||
|
||
protected var jmsSessions = Seq.empty[JmsSession] | ||
protected var jmsSessions = Seq.empty[S] | ||
|
||
protected def jmsSettings: JmsSettings | ||
|
||
protected def onSessionOpened(jmsSession: JmsSession): Unit = {} | ||
protected def onSessionOpened(jmsSession: S): Unit = {} | ||
|
||
protected def fail: AsyncCallback[Throwable] = getAsyncCallback[Throwable](e => failStage(e)) | ||
protected val fail: AsyncCallback[Throwable] = getAsyncCallback[Throwable](e => failStage(e)) | ||
|
||
private def onConnection = getAsyncCallback[Connection] { c => | ||
private val onConnection: AsyncCallback[jms.Connection] = getAsyncCallback[jms.Connection] { c => | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for cleaning these up. |
||
jmsConnection = Some(c) | ||
} | ||
|
||
private def onSession = | ||
getAsyncCallback[JmsSession] { session => | ||
jmsSessions :+= session | ||
onSessionOpened(session) | ||
} | ||
private val onSession: AsyncCallback[S] = getAsyncCallback[S] { session => | ||
jmsSessions :+= session | ||
onSessionOpened(session) | ||
} | ||
|
||
protected def executionContext(attributes: Attributes): ExecutionContext = { | ||
val dispatcher = attributes.get[ActorAttributes.Dispatcher]( | ||
|
@@ -55,82 +54,119 @@ private[jms] trait JmsConnector { this: GraphStageLogic => | |
} | ||
} | ||
|
||
protected def initSessionAsync(executionContext: ExecutionContext): Future[Unit] = { | ||
protected def initSessionAsync(executionContext: ExecutionContext): Unit = { | ||
ec = executionContext | ||
val future = Future { | ||
val sessions = openSessions() | ||
sessions foreach { session => | ||
onSession.invoke(session) | ||
} | ||
} | ||
future.onFailure { | ||
case e: Exception => fail.invoke(e) | ||
} | ||
future | ||
val sessions: Seq[Future[S]] = openSessions() | ||
val allSessions = Future.sequence(sessions) | ||
allSessions.failed.foreach(fail.invoke) | ||
// wait for all sessions to successfully initialize before invoking the onSession callback. | ||
// reduces flakiness (start, consume, then crash) at the cost of increased latency of startup. | ||
allSessions.foreach(_.foreach(onSession.invoke)) | ||
} | ||
|
||
protected def createSession(connection: Connection, createDestination: jms.Session => jms.Destination): JmsSession | ||
def openSessions(): Seq[Future[S]] | ||
|
||
protected def openSessions(): Seq[JmsSession] = { | ||
def openConnection(): jms.Connection = { | ||
val factory = jmsSettings.connectionFactory | ||
val connection = jmsSettings.credentials match { | ||
case Some(Credentials(username, password)) => factory.createConnection(username, password) | ||
case _ => factory.createConnection() | ||
} | ||
connection.setExceptionListener(new ExceptionListener { | ||
override def onException(exception: JMSException) = | ||
connection.setExceptionListener(new jms.ExceptionListener { | ||
override def onException(exception: jms.JMSException): Unit = | ||
fail.invoke(exception) | ||
}) | ||
connection.start() | ||
onConnection.invoke(connection) | ||
connection | ||
} | ||
} | ||
|
||
private[jms] trait JmsConsumerConnector extends JmsConnector[JmsConsumerSession] { this: GraphStageLogic => | ||
|
||
protected def createSession(connection: jms.Connection, | ||
createDestination: jms.Session => jms.Destination): JmsConsumerSession | ||
|
||
def openSessions(): Seq[Future[JmsConsumerSession]] = { | ||
val connection = openConnection() | ||
connection.start() | ||
|
||
val createDestination = jmsSettings.destination match { | ||
case Some(destination) => | ||
destination.create | ||
case Some(destination) => destination.create | ||
case _ => throw new IllegalArgumentException("Destination is missing") | ||
} | ||
|
||
0 until jmsSettings.sessionCount map { _ => | ||
createSession(connection, createDestination) | ||
for (_ <- 0 until jmsSettings.sessionCount) | ||
yield Future(createSession(connection, createDestination)) | ||
} | ||
} | ||
|
||
private[jms] trait JmsProducerConnector extends JmsConnector[JmsProducerSession] { this: GraphStageLogic => | ||
|
||
private def createSession(connection: jms.Connection, | ||
createDestination: jms.Session => jms.Destination): JmsProducerSession = { | ||
val session = connection.createSession(false, AcknowledgeMode.AutoAcknowledge.mode) | ||
new JmsProducerSession(connection, session, createDestination(session)) | ||
} | ||
|
||
def openSessions(): Seq[Future[JmsProducerSession]] = { | ||
val connection = openConnection() | ||
|
||
val createDestination = jmsSettings.destination match { | ||
case Some(destination) => destination.create | ||
case _ => throw new IllegalArgumentException("Destination is missing") | ||
} | ||
|
||
for (_ <- 0 until jmsSettings.sessionCount) | ||
yield Future(createSession(connection, createDestination)) | ||
} | ||
} | ||
|
||
private[jms] object JmsMessageProducer { | ||
def apply(jmsSession: JmsSession, settings: JmsProducerSettings): JmsMessageProducer = { | ||
val producer = jmsSession.session.createProducer(jmsSession.jmsDestination) | ||
def apply(jmsSession: JmsProducerSession, settings: JmsProducerSettings): JmsMessageProducer = { | ||
val producer = jmsSession.session.createProducer(null) | ||
if (settings.timeToLive.nonEmpty) { | ||
producer.setTimeToLive(settings.timeToLive.get.toMillis) | ||
} | ||
new JmsMessageProducer(producer, jmsSession) | ||
} | ||
} | ||
|
||
private[jms] class JmsMessageProducer(jmsProducer: MessageProducer, jmsSession: JmsSession) { | ||
private[jms] class JmsMessageProducer(jmsProducer: jms.MessageProducer, jmsSession: JmsProducerSession) { | ||
|
||
private val defaultDestination = jmsSession.jmsDestination | ||
|
||
private val destinationCache = new SoftReferenceCache[Destination, jms.Destination]() | ||
|
||
def send(elem: JmsMessage): Unit = { | ||
val message: Message = createMessage(elem) | ||
val message: jms.Message = createMessage(elem) | ||
populateMessageProperties(message, elem) | ||
|
||
val (sendHeaders, headersBeforeSend: Set[JmsHeader]) = elem.headers.partition(_.usedDuringSend) | ||
populateMessageHeader(message, headersBeforeSend) | ||
|
||
val deliveryModeOption = findHeader(sendHeaders) { case x: JmsDeliveryMode => x.deliveryMode } | ||
val priorityOption = findHeader(sendHeaders) { case x: JmsPriority => x.priority } | ||
val timeToLiveInMillisOption = findHeader(sendHeaders) { case x: JmsTimeToLive => x.timeInMillis } | ||
val deliveryMode = sendHeaders | ||
.collectFirst { case x: JmsDeliveryMode => x.deliveryMode } | ||
.getOrElse(jmsProducer.getDeliveryMode) | ||
|
||
jmsProducer.send( | ||
message, | ||
deliveryModeOption.getOrElse(jmsProducer.getDeliveryMode), | ||
priorityOption.getOrElse(jmsProducer.getPriority), | ||
timeToLiveInMillisOption.getOrElse(jmsProducer.getTimeToLive) | ||
) | ||
val priority = sendHeaders | ||
.collectFirst { case x: JmsPriority => x.priority } | ||
.getOrElse(jmsProducer.getPriority) | ||
|
||
val timeToLive = sendHeaders | ||
.collectFirst { case x: JmsTimeToLive => x.timeInMillis } | ||
.getOrElse(jmsProducer.getTimeToLive) | ||
|
||
elem.destination match { | ||
case Some(messageDestination) => | ||
jmsProducer.send(lookup(messageDestination), message, deliveryMode, priority, timeToLive) | ||
case None => | ||
jmsProducer.send(defaultDestination, message, deliveryMode, priority, timeToLive) | ||
} | ||
} | ||
|
||
private def findHeader[T](headersDuringSend: Set[JmsHeader])(f: PartialFunction[JmsHeader, T]): Option[T] = | ||
headersDuringSend.collectFirst(f) | ||
private def lookup(dest: Destination) = destinationCache.lookup(dest, dest.create(jmsSession.session)) | ||
|
||
private[jms] def createMessage(element: JmsMessage): Message = | ||
private[jms] def createMessage(element: JmsMessage): jms.Message = | ||
element match { | ||
|
||
case textMessage: JmsTextMessage => jmsSession.session.createTextMessage(textMessage.body) | ||
|
@@ -150,7 +186,7 @@ private[jms] class JmsMessageProducer(jmsProducer: MessageProducer, jmsSession: | |
} | ||
|
||
private[jms] def populateMessageProperties(message: javax.jms.Message, jmsMessage: JmsMessage): Unit = | ||
jmsMessage.properties().foreach { | ||
jmsMessage.properties.foreach { | ||
case (key, v) => | ||
v match { | ||
case v: String => message.setStringProperty(key, v) | ||
|
@@ -190,10 +226,11 @@ private[jms] class JmsMessageProducer(jmsProducer: MessageProducer, jmsSession: | |
} | ||
} | ||
|
||
private[jms] class JmsSession(val connection: jms.Connection, | ||
val session: jms.Session, | ||
val jmsDestination: jms.Destination, | ||
val settingsDestination: Destination) { | ||
private[jms] sealed trait JmsSession { | ||
|
||
def connection: jms.Connection | ||
|
||
def session: jms.Session | ||
|
||
private[jms] def closeSessionAsync()(implicit ec: ExecutionContext): Future[Unit] = Future { closeSession() } | ||
|
||
|
@@ -202,11 +239,18 @@ private[jms] class JmsSession(val connection: jms.Connection, | |
private[jms] def abortSessionAsync()(implicit ec: ExecutionContext): Future[Unit] = Future { abortSession() } | ||
|
||
private[jms] def abortSession(): Unit = closeSession() | ||
} | ||
|
||
private[jms] def createProducer()(implicit ec: ExecutionContext): Future[jms.MessageProducer] = | ||
Future { | ||
session.createProducer(jmsDestination) | ||
} | ||
private[jms] class JmsProducerSession(val connection: jms.Connection, | ||
val session: jms.Session, | ||
val jmsDestination: jms.Destination) | ||
extends JmsSession | ||
|
||
private[jms] class JmsConsumerSession(val connection: jms.Connection, | ||
val session: jms.Session, | ||
val jmsDestination: jms.Destination, | ||
val settingsDestination: Destination) | ||
extends JmsSession { | ||
|
||
private[jms] def createConsumer( | ||
selector: Option[String] | ||
|
@@ -233,7 +277,7 @@ private[jms] class JmsAckSession(override val connection: jms.Connection, | |
override val jmsDestination: jms.Destination, | ||
override val settingsDestination: Destination, | ||
val maxPendingAcks: Int) | ||
extends JmsSession(connection, session, jmsDestination, settingsDestination) { | ||
extends JmsConsumerSession(connection, session, jmsDestination, settingsDestination) { | ||
|
||
private[jms] var pendingAck = 0 | ||
private[jms] val ackQueue = new ArrayBlockingQueue[() => Unit](maxPendingAcks + 1) | ||
|
@@ -254,7 +298,7 @@ private[jms] class JmsTxSession(override val connection: jms.Connection, | |
override val session: jms.Session, | ||
override val jmsDestination: jms.Destination, | ||
override val settingsDestination: Destination) | ||
extends JmsSession(connection, session, jmsDestination, settingsDestination) { | ||
extends JmsConsumerSession(connection, session, jmsDestination, settingsDestination) { | ||
|
||
private[jms] val commitQueue = new ArrayBlockingQueue[TxEnvelope => Unit](1) | ||
|
||
|
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great to keep the
jms
prefix to distinguish things.