0

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?

1
  • What do you mean by "good" and "correct?" Note: Please don't respond with a tautology like "best" or "best practice." State your specific criteria. Dec 25, 2017 at 22:55

1 Answer 1

0

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.

1
  • Cool I didn't know the name of the pattern. I did implement dto-to-entity mapper. My problem is that a give DTO can result in 2 types of entities. I want to understand what should be given to a worker task at the time of creation - a list of converters and a list of entityServices or a single converter that results in a list of compound objects [<entityType1, entityType2>, <entityType1, entityType2>, ... ] and a single EntityService which takes in this list and saves it using <entityRepository1, entityRepository2>
    – comiventor
    Dec 28, 2017 at 4:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.