This repository has been archived by the owner on Jul 1, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #21 from wellcometrust/use-storage-type-classes
Use the new storage lib; more type classes
- Loading branch information
Showing
27 changed files
with
695 additions
and
590 deletions.
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
messaging/src/main/scala/uk/ac/wellcome/messaging/BigMessageReader.scala
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,27 @@ | ||
package uk.ac.wellcome.messaging | ||
|
||
import grizzled.slf4j.Logging | ||
import io.circe.Decoder | ||
import uk.ac.wellcome.json.JsonUtil.fromJson | ||
import uk.ac.wellcome.messaging.message.{ | ||
InlineNotification, | ||
MessageNotification, | ||
RemoteNotification | ||
} | ||
import uk.ac.wellcome.storage.ObjectStore | ||
|
||
import scala.util.Try | ||
|
||
trait BigMessageReader[T] extends Logging { | ||
val objectStore: ObjectStore[T] | ||
|
||
implicit val decoder: Decoder[T] | ||
|
||
def read(notification: MessageNotification): Try[T] = | ||
notification match { | ||
case inlineNotification: InlineNotification => | ||
fromJson[T](inlineNotification.jsonString) | ||
case remoteNotification: RemoteNotification => | ||
objectStore.get(remoteNotification.location) | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
messaging/src/main/scala/uk/ac/wellcome/messaging/BigMessageSender.scala
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,62 @@ | ||
package uk.ac.wellcome.messaging | ||
|
||
import java.text.SimpleDateFormat | ||
import java.util.Date | ||
|
||
import grizzled.slf4j.Logging | ||
import io.circe.Encoder | ||
import uk.ac.wellcome.json.JsonUtil._ | ||
import uk.ac.wellcome.messaging.message.{ | ||
InlineNotification, | ||
MessageNotification, | ||
RemoteNotification | ||
} | ||
import uk.ac.wellcome.storage.{KeyPrefix, ObjectStore} | ||
|
||
import scala.util.{Success, Try} | ||
|
||
trait BigMessageSender[Destination, T] extends Logging { | ||
val messageSender: MessageSender[Destination] | ||
val objectStore: ObjectStore[T] | ||
|
||
val namespace: String | ||
|
||
implicit val encoder: Encoder[T] | ||
|
||
val maxMessageSize: Int | ||
|
||
private val dateFormat = new SimpleDateFormat("YYYY/MM/dd") | ||
|
||
protected def getKeyPrefix: String = { | ||
val currentTime = new Date() | ||
s"${messageSender.destination}/${dateFormat.format(currentTime)}/${currentTime.getTime.toString}" | ||
} | ||
|
||
def sendT(t: T): Try[MessageNotification] = | ||
for { | ||
jsonString <- toJson(t) | ||
inlineNotification = InlineNotification(jsonString) | ||
|
||
encodedInlineNotification <- toJson(inlineNotification) | ||
|
||
notification <- if (encodedInlineNotification | ||
.getBytes("UTF-8") | ||
.length > maxMessageSize) { | ||
createRemoteNotification(t) | ||
} else { | ||
Success(inlineNotification) | ||
} | ||
|
||
_ <- messageSender.sendT[MessageNotification](notification) | ||
} yield notification | ||
|
||
private def createRemoteNotification(t: T): Try[RemoteNotification] = | ||
for { | ||
location <- objectStore.put(namespace)( | ||
t, | ||
keyPrefix = KeyPrefix(getKeyPrefix) | ||
) | ||
_ = info(s"Successfully stored message in location: $location") | ||
notification = RemoteNotification(location = location) | ||
} yield notification | ||
} |
27 changes: 27 additions & 0 deletions
27
messaging/src/main/scala/uk/ac/wellcome/messaging/MessageSender.scala
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,27 @@ | ||
package uk.ac.wellcome.messaging | ||
|
||
import io.circe.Encoder | ||
import uk.ac.wellcome.json.JsonUtil.toJson | ||
|
||
import scala.util.Try | ||
|
||
trait IndividualMessageSender[Destination] { | ||
def send(body: String)(subject: String, destination: Destination): Try[Unit] | ||
|
||
def sendT[T](t: T)(subject: String, destination: Destination)( | ||
implicit encoder: Encoder[T]): Try[Unit] = | ||
toJson(t).flatMap { send(_)(subject, destination) } | ||
} | ||
|
||
trait MessageSender[Destination] { | ||
protected val underlying: IndividualMessageSender[Destination] | ||
|
||
val subject: String | ||
val destination: Destination | ||
|
||
def send(body: String): Try[Unit] = | ||
underlying.send(body)(subject, destination) | ||
|
||
def sendT[T](t: T)(implicit encoder: Encoder[T]): Try[Unit] = | ||
underlying.sendT[T](t)(subject, destination) | ||
} |
28 changes: 28 additions & 0 deletions
28
messaging/src/main/scala/uk/ac/wellcome/messaging/memory/MemoryBigMessageSender.scala
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,28 @@ | ||
package uk.ac.wellcome.messaging.memory | ||
|
||
import io.circe.Encoder | ||
import uk.ac.wellcome.messaging.BigMessageSender | ||
import uk.ac.wellcome.storage.{ObjectStore, SerialisationStrategy} | ||
import uk.ac.wellcome.storage.memory.MemoryObjectStore | ||
|
||
class MemoryBigMessageSender[T]( | ||
maxSize: Int = 100, | ||
storeNamespace: String = "MemoryBigMessageSender", | ||
destination: String = "MemoryBigMessageSender" | ||
)( | ||
implicit | ||
val encoder: Encoder[T], | ||
serialisationStrategy: SerialisationStrategy[T] | ||
) extends BigMessageSender[String, T] { | ||
override val messageSender: MemoryMessageSender = new MemoryMessageSender( | ||
destination = destination, | ||
subject = "Sent from MemoryBigMessageSender" | ||
) | ||
|
||
override val objectStore: ObjectStore[T] = new MemoryObjectStore[T]() | ||
override val namespace: String = storeNamespace | ||
override val maxMessageSize: Int = maxSize | ||
|
||
def messages: List[messageSender.underlying.MemoryMessage] = | ||
messageSender.messages | ||
} |
30 changes: 30 additions & 0 deletions
30
messaging/src/main/scala/uk/ac/wellcome/messaging/memory/MemoryMessageSender.scala
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,30 @@ | ||
package uk.ac.wellcome.messaging.memory | ||
|
||
import uk.ac.wellcome.messaging.{IndividualMessageSender, MessageSender} | ||
|
||
import scala.util.Try | ||
|
||
class MemoryIndividualMessageSender extends IndividualMessageSender[String] { | ||
case class MemoryMessage( | ||
body: String, | ||
subject: String, | ||
destination: String | ||
) | ||
|
||
var messages: List[MemoryMessage] = List.empty | ||
|
||
override def send(body: String)(subject: String, | ||
destination: String): Try[Unit] = Try { | ||
messages = messages :+ MemoryMessage(body, subject, destination) | ||
} | ||
} | ||
|
||
class MemoryMessageSender( | ||
val destination: String, | ||
val subject: String | ||
) extends MessageSender[String] { | ||
override val underlying: MemoryIndividualMessageSender = | ||
new MemoryIndividualMessageSender() | ||
|
||
def messages: List[underlying.MemoryMessage] = underlying.messages | ||
} |
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
3 changes: 0 additions & 3 deletions
3
messaging/src/main/scala/uk/ac/wellcome/messaging/sns/PublishAttempt.scala
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.