DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Join us tomorrow at 1 PM EST: "3-Step Approach to Comprehensive Runtime Application Security"
Save your seat
  1. DZone
  2. Data Engineering
  3. Data
  4. Lagom Scheduler Service Using SBT

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.

Yashpal Bhardwaj user avatar by
Yashpal Bhardwaj
·
Jan. 09, 19 · Tutorial
Like (3)
Save
Tweet
Share
6.54K Views

Join the DZone community and get the full member experience.

Join For Free

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!

job scheduling microservice

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Type Variance in Java and Kotlin
  • Using JSON Web Encryption (JWE)
  • Choosing the Best Cloud Provider for Hosting DevOps Tools
  • Unleashing the Power of JavaScript Modules: A Beginner’s Guide

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: