Skip to content

Commit

Permalink
PACT-709: First implementation of stub
Browse files Browse the repository at this point in the history
  • Loading branch information
rwalpole committed Jan 31, 2023
1 parent 2f02c18 commit 9e63414
Show file tree
Hide file tree
Showing 10 changed files with 179 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/.bsp/
target/
/.idea/
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 The National Archives, UK

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,11 @@
JMS4s Request-Reply Stub
# JMS4s Request-Reply Stub

This stub service is designed to reply to JMS messages with stubbed data and is intended for use during development and testing of frontend services that rely on the response-reply message pattern.

To start ElasticMQ in Docker run..
```
docker compose up -d
```
..from the project root directory.

To start the EchoServer use `sbt run`.
30 changes: 30 additions & 0 deletions build.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
ThisBuild / scalaVersion := "2.13.8"
ThisBuild / version := "0.1.0-SNAPSHOT"
ThisBuild / organization := "uk.gov.nationalarchives"
ThisBuild / organizationName := "The National Archives"
ThisBuild / startYear := Some(2023)
ThisBuild / licenses := Seq("MIT" -> url("https://opensource.org/licenses/MIT"))
ThisBuild / description := "JMS4S Request-Reply Stub"

/* IMPORTANT NOTE >>>
* Until further notice you will need to sbt publishLocal the JMS4S library from the combined-features branch
* of this repository https://github.com/rwalpole/jms4s
*/

lazy val root = (project in file("."))
.enablePlugins(AutomateHeaderPlugin)
.settings(
name := "jms4s-request-reply-stub",
libraryDependencies ++= Seq(
"org.typelevel" %% "cats-effect" % "3.4.4",
"org.typelevel" %% "cats-core" % "2.9.0",
"org.typelevel" %% "cats-effect-kernel" % "3.4.4",
"org.typelevel" %% "log4cats-core" % "2.5.0",
"org.typelevel" %% "log4cats-slf4j" % "2.5.0",
"dev.fpinbo" %% "jms4s-active-mq-artemis" % "0.0.1-350e4fd-SNAPSHOT",
"dev.fpinbo" %% "jms4s-simple-queue-service" % "0.0.1-350e4fd-SNAPSHOT",
"org.slf4j" % "slf4j-simple" % "2.0.6" % Runtime
),
headerLicense := Some(HeaderLicense.MIT("2023", "The National Archives")),

)
7 changes: 7 additions & 0 deletions custom.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# the include should be done only once, at the beginning of the custom configuration file
include classpath("application.conf")

queues {
request-general { }
omega-editorial-web-application-instance-1 { }
}
12 changes: 12 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
version: "3.7"
services:
elasticmq-native:
container_name: elasticmq-container
ports:
- 9324:9324
- 9325:9325
volumes:
- type: bind
source: ./custom.conf
target: /opt/elasticmq.conf
image: softwaremill/elasticmq-native
5 changes: 5 additions & 0 deletions project/Dependencies.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import sbt._

object Dependencies {
lazy val scalaTest = "org.scalatest" %% "scalatest" % "3.2.11"
}
1 change: 1 addition & 0 deletions project/build.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
sbt.version=1.8.2
1 change: 1 addition & 0 deletions project/plugins.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
addSbtPlugin("de.heikoseeberger" % "sbt-header" % "5.9.0")
88 changes: 88 additions & 0 deletions src/main/scala/uk/gov/nationalarchives/omega/jms/EchoServer.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* Copyright (c) 2023 The National Archives
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

package uk.gov.nationalarchives.omega.jms

import cats.effect._
import jms4s.JmsAcknowledgerConsumer.AckAction
import jms4s.JmsClient
import jms4s.config.QueueName
import jms4s.sqs.simpleQueueService
import jms4s.sqs.simpleQueueService._
import org.typelevel.log4cats.SelfAwareStructuredLogger
import org.typelevel.log4cats.slf4j.Slf4jFactory

import scala.concurrent.duration.DurationInt

/**
* Just a simple JMS echo server that received a request returns a
* reply with a correlationId set to the messageId of the request message.
*
* Follows the "JMS Request/Reply Example" pattern set out
* in the Enterprise Integration Patterns book,
* see https://www.enterpriseintegrationpatterns.com/RequestReplyJmsExample.html.
*
* Can be used for testing.
*
* @author <a href="mailto:[email protected]">Adam Retter</a>
*/
object EchoServer extends IOApp {

private val logging = Slf4jFactory[IO]
private implicit val logger: SelfAwareStructuredLogger[IO] = logging.getLogger
private val clientId = "echo_server_1"
private val requestQueue = QueueName("request-general")
private val replyQueue = QueueName("omega-editorial-web-application-instance-1") //TODO(AR) note this is for the editorial web application
private val consumerConcurrencyLevel = 1 //0

private val jmsClient: Resource[IO, JmsClient[IO]] = simpleQueueService.makeJmsClient[IO](
Config(
endpoint = Endpoint(Some(DirectAddress(HTTP, "localhost", Some(9324))),"elasticmq"),
credentials = Some(Credentials("x","x")),
clientId = ClientId(clientId),
None
)
)

override def run(args: List[String]): IO[ExitCode] = {

val consumerRes = for {
_ <- Resource.liftK(IO.println("Starting EchoServer..."))
client <- jmsClient
consumer <- client.createAcknowledgerConsumer(requestQueue, concurrencyLevel = consumerConcurrencyLevel, pollingInterval = 50.millis)
} yield consumer

consumerRes.use(_.handle { (jmsMessage, mf) =>
for {
requestText <- jmsMessage.asTextF[IO]
_ <- IO.println(s"Echo Server received message: $requestText")
responseText <- IO.pure(s"Echo Server: $requestText")
responseMessage <- mf.makeTextMessage(responseText)
// PERFORM THE ACTUAL SERVICE HERE
// NOTE(AR) set correlationId on response message to the request message id
requestMessageId = jmsMessage.getJMSMessageId.get
_ = responseMessage.setJMSCorrelationId(requestMessageId)
_ <- IO.println(s"Echo Server sending response message: $responseText with correlationId: $requestMessageId")
} yield AckAction.send(responseMessage, replyQueue)
}).as(ExitCode.Success)
}

}

0 comments on commit 9e63414

Please sign in to comment.