Question

I have various types of tasks, each task involves the following -

  1. Use an XyzAPIProvider to query and obtain a payload
  2. convert the payload into a list of XyzDTOs (ModelMapper)
  3. convert the list of DTOs into list of entities (XyzDto2EntityConverter)
  4. store the entities into database using JPA repository (XyzRepository)

I plan to use Java's ExecutorService to run the task in its own thread.

Each Task will implement Runnable and override the run method.

I want a particular task to be instantiated and queued to executorService depending upon the task type my application gets from a job queue.

I want to understand what is the correct design pattern applicable here? With my limited knowledge, I see some kind of TaskFactory which takes in a job type and returns instance of a given task. But within that TaskFactory, I see the factory method having lots of if then else. Is there a better way? Does java 8 provides any constructs for this use case?

Was it helpful?

Solution

Your design already outlines Queue-Based Load Leveling pattern. You have

  • tasks in the database
  • a service executor
  • a queue that decouples incoming jobs from the executor.

What needs to be done is to take the tasks from the database and convert them to messages which can be stored in the queue. The executor then processes the queue items at its own pace.

In step 2 you create some DTOs. Maybe these could also be the messages that are put into the queue? The DTOs need to have a common parent class so they can be stored in the queue. With this design you need to implement entity-to-dto mapper in case you already don't have that.

Licensed under: CC-BY-SA with attribution
scroll top