Over a million developers have joined DZone.

Integrating Unmanaged Services in Lagom With Scala

DZone's Guide to

Integrating Unmanaged Services in Lagom With Scala

When working with external or unmanaged services, let's see how you can tie them into your Lagom projects using Scala as our language of choice.

· Java Zone ·
Free Resource

Delivering modern software? Atomist automates your software delivery experience.

So, you want to integrate an unmanaged service in Lagom using Scala.

Let’s first understand what an unmanaged service is 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 external service that exposes some data over HTTP. Any external REST API that will be used in a Lagom-based service is an unmanaged service.

For example, there's integrating Google Maps, Openweather, or any other kind of service 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 an 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 the exact same route as the 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 the 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()
        result.map(response => response)


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

lagomUnmanagedServices in ThisBuild := Map("external-service" -> "https://jsonplaceholder.typicode.com:80")

Step 4: Bind the external service in ServiceModule:

lazy val externalService = serviceClient.implement[ExternalService]

Data Modeling for Unmanaged Services

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 modeling 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!

Start automating your delivery right there on your own laptop, today! Get the open source Atomist Software Delivery Machine.

java ,lagom ,scala ,unmanaged services ,tutorial

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}