Skip to content
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 1.1 and AWS SQS SimpleQueueService #359

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 20 additions & 9 deletions active-mq-artemis/src/main/scala/jms4s/activemq/activeMQ.scala
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,12 @@ import cats.effect.{ Async, Resource, Sync }
import cats.syntax.all._
import jms4s.JmsClient
import jms4s.jms.JmsContext
import jms4s.jms.utils.SharedConnection
import org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory
import org.typelevel.log4cats.Logger

import javax.jms.Session

object activeMQ {

case class Config(
Expand All @@ -50,17 +53,25 @@ object activeMQ {
val factory = new ActiveMQConnectionFactory(hosts(config.endpoints))
factory.setClientID(config.clientId.value)

config.username.fold(factory.createContext())(username =>
factory.createContext(username.value, config.password.map(_.value).getOrElse(""))
val connection = config.username.fold(factory.createConnection())(username =>
factory.createConnection(username.value, config.password.map(_.value).getOrElse(""))
)

// NOTE: start the connection before sharing it, it will be stopped in SharedConnection.close()
connection.start()

val session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE)

Tuple2(new SharedConnection(connection), session)
}
)(c =>
Logger[F].info(s"Closing context $c to MQ at ${hosts(config.endpoints)}...") *>
Sync[F].blocking(c.close()) *>
Logger[F].info(s"Closed context $c to MQ at ${hosts(config.endpoints)}.")
)
_ <- Resource.eval(Logger[F].info(s"Opened context $context."))
} yield new JmsClient[F](new JmsContext[F](context))
) {
case (sharedConnection, session) =>
Logger[F].info(s"Closing context $session to MQ at ${hosts(config.endpoints)}...") *>
Sync[F].blocking { session.close(); sharedConnection.close() } *>
Logger[F].info(s"Closed context $session to MQ at ${hosts(config.endpoints)}.")
}
_ <- Resource.eval(Logger[F].info(s"Opened context ${context._2}."))
} yield new JmsClient[F](new JmsContext[F](context._1, context._2))

private def hosts(endpoints: NonEmptyList[Endpoint]): String =
endpoints.map(e => s"tcp://${e.host}:${e.port}").toList.mkString(",")
Expand Down
29 changes: 23 additions & 6 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,12 @@ ThisBuild / githubWorkflowAddedJobs += WorkflowJob(
)

val catsV = "2.9.0"
val jmsV = "2.0.1"
//val jmsV = "2.0.1"
val jmsV = "1.1-rev-1"
val ibmMQV = "9.3.1.0"
val activeMQV = "2.19.1"
val sqsV = "2.0.2"
val awsV = "2.18.1"
val catsEffectV = "3.4.4"
val catsEffectScalaTestV = "1.5.0"
val fs2V = "3.4.0"
Expand Down Expand Up @@ -148,27 +151,39 @@ lazy val activeMQArtemis = project
.settings(Test / parallelExecution := false)
.dependsOn(core)

lazy val simpleQueueService = project
.in(file("sqs"))
.settings(commonSettings, releaseSettings)
.settings(name := "jms4s-simple-queue-service")
.settings(libraryDependencies ++= Seq(
"com.amazonaws" % "amazon-sqs-java-messaging-lib" % sqsV,
"software.amazon.awssdk" % "auth" %awsV,
"software.amazon.awssdk" % "sqs" %awsV
))
.settings(Test / parallelExecution := false)
.dependsOn(core)

lazy val tests = project
.in(file("tests"))
.settings(commonSettings, releaseSettings)
.enablePlugins(NoPublishPlugin)
.settings(libraryDependencies += "org.apache.logging.log4j" % "log4j-slf4j-impl" % log4jSlf4jImplV % Runtime)
.settings(Test / parallelExecution := false)
.dependsOn(ibmMQ, activeMQArtemis)
.dependsOn(ibmMQ, activeMQArtemis, simpleQueueService)

lazy val examples = project
.in(file("examples"))
.settings(commonSettings, releaseSettings)
.enablePlugins(NoPublishPlugin)
.dependsOn(ibmMQ, activeMQArtemis)
.dependsOn(ibmMQ, activeMQArtemis, simpleQueueService)

lazy val site = project
.in(file("site"))
.enablePlugins(MicrositesPlugin)
.enablePlugins(MdocPlugin)
.enablePlugins(NoPublishPlugin)
.settings(commonSettings)
.dependsOn(core, ibmMQ, activeMQArtemis)
.dependsOn(core, ibmMQ, activeMQArtemis, simpleQueueService)
.settings {
import microsites._
Seq(
Expand Down Expand Up @@ -216,7 +231,8 @@ lazy val commonSettings = Seq(
addCompilerPlugin("org.typelevel" %% "kind-projector" % kindProjectorV cross CrossVersion.full),
addCompilerPlugin("com.olegpy" %% "better-monadic-for" % betterMonadicForV),
libraryDependencies ++= Seq(
"javax.jms" % "javax.jms-api" % jmsV,
// "javax.jms" % "javax.jms-api" % jmsV,
"javax.jms" % "jms-api" % jmsV,
"org.typelevel" %% "cats-core" % catsV,
"org.typelevel" %% "cats-effect" % catsEffectV,
"co.fs2" %% "fs2-core" % fs2V,
Expand All @@ -241,7 +257,8 @@ lazy val releaseSettings = {
Developer("azanin", "Alessandro Zanin", "[email protected]", url("https://github.com/azanin")),
Developer("al333z", "Alessandro Zoffoli", "[email protected]", url("https://github.com/al333z")),
Developer("faustin0", "Fausto Di Natale", "[email protected]", url("https://github.com/faustin0")),
Developer("r-tomassetti", "Renato Tomassetti", "[email protected]", url("https://github.com/r-tomassetti"))
Developer("r-tomassetti", "Renato Tomassetti", "[email protected]", url("https://github.com/r-tomassetti")),
Developer("adamretter", "Adam Retter", "[email protected]", url("https://github.com/adamretter"))
)
)
}
Expand Down
22 changes: 11 additions & 11 deletions core/src/main/scala/jms4s/JmsAcknowledgerConsumer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ object JmsAcknowledgerConsumer {
send.messages.messagesAndDestinations.traverse_ {
case (message, (name, delay)) =>
delay.fold(ifEmpty = context.send(name, message))(
f = d => context.send(name, message, d)
f = _ => context.send(name, message)
)
} *> Sync[F].blocking(message.wrapped.acknowledge())
)
Expand Down Expand Up @@ -106,16 +106,16 @@ object JmsAcknowledgerConsumer {
): AckAction[F] =
Send[F](ToSend[F](messages.map { case (message, name) => (message, (name, None)) }))

def sendNWithDelay[F[_]](
messages: NonEmptyList[(JmsMessage, (DestinationName, Option[FiniteDuration]))]
): AckAction[F] = Send[F](ToSend(messages))

def sendWithDelay[F[_]](
message: JmsMessage,
destination: DestinationName,
duration: Option[FiniteDuration]
): AckAction[F] =
Send[F](ToSend[F](NonEmptyList.one((message, (destination, duration)))))
// def sendNWithDelay[F[_]](
// messages: NonEmptyList[(JmsMessage, (DestinationName, Option[FiniteDuration]))]
// ): AckAction[F] = Send[F](ToSend(messages))
//
// def sendWithDelay[F[_]](
// message: JmsMessage,
// destination: DestinationName,
// duration: Option[FiniteDuration]
// ): AckAction[F] =
// Send[F](ToSend[F](NonEmptyList.one((message, (destination, duration)))))

def send[F[_]](
message: JmsMessage,
Expand Down
24 changes: 12 additions & 12 deletions core/src/main/scala/jms4s/JmsAutoAcknowledgerConsumer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ object JmsAutoAcknowledgerConsumer {
ifSend = (send: Send[F]) =>
send.messages.messagesAndDestinations.traverse_ {
case (message, (name, delay)) =>
delay.fold(context.send(name, message))(delay => context.send(name, message, delay))
delay.fold(context.send(name, message))(_ => context.send(name, message))
}
)
} yield ()
Expand Down Expand Up @@ -88,17 +88,17 @@ object JmsAutoAcknowledgerConsumer {
): AutoAckAction[F] =
Send[F](ToSend[F](messages.map { case (message, name) => (message, (name, None)) }))

def sendNWithDelay[F[_]](
messages: NonEmptyList[(JmsMessage, (DestinationName, Option[FiniteDuration]))]
): AutoAckAction[F] =
Send[F](ToSend[F](messages.map { case (message, (name, delay)) => (message, (name, delay)) }))

def sendWithDelay[F[_]](
message: JmsMessage,
destination: DestinationName,
duration: Option[FiniteDuration]
): AutoAckAction[F] =
Send[F](ToSend[F](NonEmptyList.one((message, (destination, duration)))))
// def sendNWithDelay[F[_]](
// messages: NonEmptyList[(JmsMessage, (DestinationName, Option[FiniteDuration]))]
// ): AutoAckAction[F] =
// Send[F](ToSend[F](messages.map { case (message, (name, delay)) => (message, (name, delay)) }))
//
// def sendWithDelay[F[_]](
// message: JmsMessage,
// destination: DestinationName,
// duration: Option[FiniteDuration]
// ): AutoAckAction[F] =
// Send[F](ToSend[F](NonEmptyList.one((message, (destination, duration)))))

def send[F[_]](
message: JmsMessage,
Expand Down
70 changes: 34 additions & 36 deletions core/src/main/scala/jms4s/JmsProducer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,19 @@ import jms4s.config.DestinationName
import jms4s.jms._
import jms4s.model.SessionType

import scala.concurrent.duration.FiniteDuration

trait JmsProducer[F[_]] {

def sendN(
messageFactory: MessageFactory[F] => F[NonEmptyList[(JmsMessage, DestinationName)]]
): F[Unit]

def sendNWithDelay(
messageFactory: MessageFactory[F] => F[NonEmptyList[(JmsMessage, (DestinationName, Option[FiniteDuration]))]]
): F[Unit]

def sendWithDelay(
messageFactory: MessageFactory[F] => F[(JmsMessage, (DestinationName, Option[FiniteDuration]))]
): F[Unit]
// def sendNWithDelay(
// messageFactory: MessageFactory[F] => F[NonEmptyList[(JmsMessage, (DestinationName, Option[FiniteDuration]))]]
// ): F[Unit]
//
// def sendWithDelay(
// messageFactory: MessageFactory[F] => F[(JmsMessage, (DestinationName, Option[FiniteDuration]))]
// ): F[Unit]

def send(messageFactory: MessageFactory[F] => F[(JmsMessage, DestinationName)]): F[Unit]

Expand Down Expand Up @@ -97,33 +95,33 @@ object JmsProducer {
} yield ()
}

override def sendNWithDelay(
f: MessageFactory[F] => F[NonEmptyList[(JmsMessage, (DestinationName, Option[FiniteDuration]))]]
): F[Unit] =
pool.acquireAndUseContext {
case (ctx, mf) =>
for {
messagesWithDestinationsAndDelayes <- f(mf)
_ <- messagesWithDestinationsAndDelayes.traverse_ {
case (message, (destinatioName, duration)) =>
duration.fold(ctx.send(destinatioName, message))(delay =>
ctx.send(destinatioName, message, delay)
)
}

} yield ()
}

override def sendWithDelay(
f: MessageFactory[F] => F[(JmsMessage, (DestinationName, Option[FiniteDuration]))]
): F[Unit] =
pool.acquireAndUseContext {
case (ctx, mf) =>
for {
(message, (destinationName, delay)) <- f(mf)
_ <- delay.fold(ctx.send(destinationName, message))(delay => ctx.send(destinationName, message, delay))
} yield ()
}
// override def sendNWithDelay(
// f: MessageFactory[F] => F[NonEmptyList[(JmsMessage, (DestinationName, Option[FiniteDuration]))]]
// ): F[Unit] =
// pool.acquireAndUseContext {
// case (ctx, mf) =>
// for {
// messagesWithDestinationsAndDelayes <- f(mf)
// _ <- messagesWithDestinationsAndDelayes.traverse_ {
// case (message, (destinatioName, duration)) =>
// duration.fold(ctx.send(destinatioName, message))(delay =>
// ctx.send(destinatioName, message, delay)
// )
// }
//
// } yield ()
// }
//
// override def sendWithDelay(
// f: MessageFactory[F] => F[(JmsMessage, (DestinationName, Option[FiniteDuration]))]
// ): F[Unit] =
// pool.acquireAndUseContext {
// case (ctx, mf) =>
// for {
// (message, (destinationName, delay)) <- f(mf)
// _ <- delay.fold(ctx.send(destinationName, message))(delay => ctx.send(destinationName, message, delay))
// } yield ()
// }

override def send(f: MessageFactory[F] => F[(JmsMessage, DestinationName)]): F[Unit] =
pool.acquireAndUseContext {
Expand Down
24 changes: 12 additions & 12 deletions core/src/main/scala/jms4s/JmsTransactedConsumer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ object JmsTransactedConsumer {
case (message, (name, delay)) =>
delay.fold(
context.send(name, message)
)(d => context.send(name, message, d))
)(_ => context.send(name, message))
} *> context.commit
)
} yield ()
Expand Down Expand Up @@ -103,17 +103,17 @@ object JmsTransactedConsumer {
): TransactionAction[F] =
Send[F](ToSend[F](messages.map { case (message, name) => (message, (name, None)) }))

def sendNWithDelay[F[_]](
messages: NonEmptyList[(JmsMessage, (DestinationName, Option[FiniteDuration]))]
): TransactionAction[F] =
Send[F](ToSend[F](messages.map { case (message, (name, delay)) => (message, (name, delay)) }))

def sendWithDelay[F[_]](
message: JmsMessage,
destination: DestinationName,
duration: Option[FiniteDuration]
): TransactionAction[F] =
Send[F](ToSend[F](NonEmptyList.one((message, (destination, duration)))))
// def sendNWithDelay[F[_]](
// messages: NonEmptyList[(JmsMessage, (DestinationName, Option[FiniteDuration]))]
// ): TransactionAction[F] =
// Send[F](ToSend[F](messages.map { case (message, (name, delay)) => (message, (name, delay)) }))
//
// def sendWithDelay[F[_]](
// message: JmsMessage,
// destination: DestinationName,
// duration: Option[FiniteDuration]
// ): TransactionAction[F] =
// Send[F](ToSend[F](NonEmptyList.one((message, (destination, duration)))))

def send[F[_]](
message: JmsMessage,
Expand Down
Loading