Skip to content

Instantly share code, notes, and snippets.

@samuelorji
Last active June 5, 2019 11:16
Show Gist options
  • Save samuelorji/b69caf26ce84de9964c335322fddcac3 to your computer and use it in GitHub Desktop.
Save samuelorji/b69caf26ce84de9964c335322fddcac3 to your computer and use it in GitHub Desktop.
object MessagingService {
case class QueueElement[T](data : T , numRetry : Int = 1)
case class SendMessageRequest(phoneNumber : String, msg : String, enqueue : Boolean, queueName : String)
case class SendMessageResponse(status : Boolean)
}
trait MessagingService extends Actor
with ActorLogging
with JsonHelper // for Json Serialization
{
val redisClient = getRedisClient.getRedisInstance
def getRedisClient : RedisDbT
private def sendMsg(number: String, message: String ): Future[Boolean] = {
Future.failed(new Exception("Messaging Gateway Not found"))
}
import MessagingService._
override def receive: Receive = {
case req : SendMessageRequest =>
val currentSender = sender()
val sendMsgFut = sendMsg(
number = req.phoneNumber,
message = req.msg
)
sendMsgFut onComplete{
case Success(res) =>
res match {
case true => // we don't really care about this :)
case false =>
val msgJson = QueueElement(req).toJson.toString()
if(req.enqueue) {
redisClient ! EnqueueElementRequest(req.queueName, msgJson)
}
}
currentSender ! SendMessageResponse(res)
case Failure(ex) =>
log.error("Error occured while sending a message, error Message : {} ", ex.getMessage)
val msgJson = QueueElement(req).toJson.toString()
if(req.enqueue) {
redisClient ! EnqueueElementRequest(req.queueName, msgJson)
}
currentSender ! SendMessageResponse(false)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment