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

PACT-709: First implementation of stub #1

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
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.
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,14 @@
JMS4s Request-Reply Stub
# JMS4s Request-Reply Stub
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we add some instructions for demonstrating this, using the restful API?

See this comment.


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`.

## 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
25 changes: 25 additions & 0 deletions build.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
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"

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 { }
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why 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"
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is file used at all?
Also, are we going to be using scalatest or Munit?

}
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
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps it's clear enough without the comment?

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")),
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could these creds be anything?

clientId = ClientId(clientId),
None
)
)

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

val consumerRes = for {
_ <- Resource.liftK(IO.println("Starting EchoServer..."))
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not 100% sure how cats effects IO class works, but do we want to use the logger here instead of println?

client <- jmsClient
consumer <- client.createAcknowledgerConsumer(requestQueue, concurrencyLevel = consumerConcurrencyLevel, pollingInterval = 50.millis)
} yield consumer

consumerRes.use(_.handle { (jmsMessage, mf) =>
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps rename mf to messageFactory?

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
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this note still needed (ACTUAL SERVICE)?

// NOTE(AR) set correlationId on response message to the request message id
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps it's clear enough without the comment?

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)
}

}