So, you want to integrate an unmanaged service in Lagom using Scala.
Let’s first understand what an unmanaged service first and then we will see How we can integrate an unmanaged service with our lagom service.

What is an unmanaged service?

An unmanaged service is basically an exernal service which exposes some data over HTTP. Any external REST API which will be used in Lagom based service is an unmanaged service.

Any external REST API which will be used in Lagom based service is an unmanaged service.
For example : Integration of GoogleMap, Openweather and any other for that matter.

Communicating with external(unmanaged) services in Lagom

  • It is possible to enable communication between the Lagom services defined in your build, and an unbounded number of external services (which could either be running locally or on a different machine).
  • To do this, we need to register the API as external service in the Service Locator, which allows services to discover and communicate with each other.
  • The first thing you will have to do is to register each external service in the Service Locator. Assume we want to register an external service named weather that is running on http://localhost:3333, here is what we would add to the build.
    lagomUnmanagedServices in ThisBuild := Map(“weather” -> “http://localhost:3333”)
    where http://localhost is the hostname and 3333 is port number
  • The above ensures that the Service Locator knows about the weather service. Then, if you need a Lagom service to communicate with it, simply @Inject the ServiceLocator and use it to either locate the weather service’s URI, or perform some arbitrary work with it.

Now Let’s see How  we can consume data from the unmanaged service.

Consume data from unmanaged service

Step 1. Create a new ‘trait’ for the external service which will have exact same route as external service has.

trait ExternalService extends Service {

def getUser():ServiceCall[NotUsed, UserData]

override final def descriptor = {
  import Service._
  // @formatter:off
      pathCall("/posts/1", getUser _)
  // @formatter:on

Step 2.  Injecting external service into our service

class UserServiceImpl(externalService: ExternalService)(implicit ec: ExecutionContext) extends UserService {

  override def greetUser(name: String) = ServiceCall { _ =>
    Future.successful("Hi, " + name)

  override def testUser() = ServiceCall { _ =>
    val result: Future[UserData] = externalService.getUser().invoke() => response)


Step  3. Define external service’s host and port in build.sbt

lagomUnmanagedServices in ThisBuild := Map("external-service" -> "")

Step 4.  Bind the external service in ServiceModule.

  lazy val externalService = serviceClient.implement[ExternalService]

Data modelling for unmanaged service

Now we know that the external service has exposed some APIs and we are consuming the data from the API. So we must know the type of the response. Now we will map the response with a Data model.

How do we do the data modelling or map the response with the case class?

case class UserData(userId: Int, id: Int, title:String, body: String)

object UserData {
    * Format for converting user messages to and from JSON.
    * This will be picked up by a Lagom implicit conversion from Play's JSON format to Lagom's message serializer.
  implicit val format: Format[UserData] = Json.format[UserData]

Alright, That was it. We are done writing our service which will consume data from an external(unmanaged) service.

If you find any challenge, Do let me know in the comments.If you enjoyed this post, I’d be very grateful if you’d help it spread.Keep smiling, Keep coding!



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.