Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Lagom Scheduler Service Using SBT

DZone 's Guide to

Lagom Scheduler Service Using SBT

A developer walks us through an issue he came across when creating a scheduler microservice with Lagom, and how he got around it.

· Microservices Zone ·
Free Resource

We know how to write a Scheduler and how to take work from it, already. We also have an idea how to start it, just give it in the starting point of the program and it will start. You may also know how to do it when we are using Lagom to create microservices, but I didn't. I struggled to start this scheduler using AbstractModule and asEagerSingleton but it didn't work. Then I researched a lot on the internet, and yet still had no success.

Then I discussed it with a friend and he came up with this beautiful keyword — wire. And that's it — it's done. Let me give you some insights of what I was trying and what solved my problem, so that if someone in your life gets stuck on a similar problem, you can be that friend to them.

Here's what I was trying:

class LMSActor extends Actor {
  override def receive: Receive = {
    case LMSActorInvoke => println("\n\nSome Statement!\n\n")
  }
}

object LMSActor {
  def props: Props = Props(new LMSActor)
  case object LMSActorInvoke
}

class LFScheduler {
  implicit val system: ActorSystem = ActorSystem("LMSActorSystem")
  implicit val materializer: ActorMaterializer = ActorMaterializer()
  implicit val executionContext: ExecutionContextExecutor = system.dispatcher
  implicit val timeout: Timeout = 3.minute
  val concurrency = Runtime.getRuntime.availableProcessors() * 10
  val lmsActor = system.actorOf(RoundRobinPool(concurrency).props(LMSActor.props), "LMS Actor")
  val lmsScheduler = system.scheduler.schedule(2.seconds, 4.seconds)(lmsActor ? LMSActorInvoke)
}

object LFScheduler extends LFScheduler

class LFLMSLoader extends LagomApplicationLoader {
  override def load(context: LagomApplicationContext): LagomApplication = new LFLMSApplication(context) {
    override def serviceLocator: ServiceLocator = NoServiceLocator
  }

  override def loadDevMode(context: LagomApplicationContext): LagomApplication = new LFLMSApplication(context) with LagomDevModeComponents
  override def describeService = Some(readDescriptor[LFLMSService])
}

class LFServiceModule extends AbstractModule {
  override def configure() = {
    bind(classOf[LFScheduler]).asEagerSingleton()
  }
}

In `application.conf`:

play.application.loader = com.lf.lflms.impl.LFLMSLoader

play.application.module = com.lf.lflms.impl.LFServiceModule

As per my understanding LFServiceModule, we should have loaded asEagerSingleton and should have invoked the scheduler. But it did not work. I tried to explore more into the documentation but couldn’t get any good references. I don’t know what's wrong in this approach.

What worked for me is:

class LFAppLoader extends LagomApplicationLoader {
  override def load(context: LagomApplicationContext): LagomApplication = new LFApp(context) {
    override def serviceLocator: ServiceLocator = NoServiceLocator
  }

  override def loadDevMode(context: LagomApplicationContext): LagomApplication = new LFApp(context) with LagomDevModeComponents
  override def describeService = Some(readDescriptor[LFLMSService])
}

abstract class LFApp(context: LagomApplicationContext) extends LagomApplication(context) with AhcWSComponents {
  // Bind the service that this server provides
  override lazy val lagomServer = serverFor[LFLMSService](wire[LFLMSServiceImpl])
  //Bind the external service in ServiceModule.
  lazy val externalService = serviceClient.implement[LMSService]

  wire[LFScheduler]
}

Another thing, I had the need of instance of Unmanaged Service in my scheduler, so that the scheduler can invoke the unmanaged service periodically. So I created an independent class for the scheduler code and injected the Unmanaged service into the Scheduler class constructor.

For instance:

class LFScheduler(lmsService: LMSService)(implicit ec: ExecutionContext) {

  val userProfile = UserProfile("X", "", "X", "+X", "X.X@gmail.com", "X", "X")
  val concurrency = Runtime.getRuntime.availableProcessors() * 10

  implicit val timeout: Timeout = 3.minute

  implicit val system: ActorSystem = ActorSystem("LMSActorSystem")

  implicit val materializer: ActorMaterializer = ActorMaterializer()

  val lmsActor = system.actorOf(RoundRobinPool(concurrency).props(LMSActor.props), "LMSActor")

  def hitLMSAPI = {
    lmsService.createUserProfile.invoke(userProfile).map(res => println(s">>>>>>>>>>>>>>>>>>>>>>: ${res}"))
  }


  system.scheduler.schedule(2.seconds, 2.seconds)(hitLMSAPI)

}

Please visit the GitHub repo and feel free to contribute in the running example of this Service.

Keep coding!

Topics:
microservices ,scala tutorial for java programmers ,microservices tutorial ,lagom

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}