Created
May 24, 2016 19:11
-
-
Save martyychang/ff1c4fdb62ec9cedb06d7d3bff969000 to your computer and use it in GitHub Desktop.
How to map task names to task services
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Service | |
public class AdvertiserListingTaskService { | |
public void executeWithConfig(TaskConfig config) { | |
AdvertiserListingTask task = getTask(); | |
task.apply(config); | |
task.run(); // Or something like executor.execute(task) | |
} | |
@Lookup | |
public AdvertiserListingTask getTask() { | |
return null; // Spring will override this | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Controller | |
@RequestMapping("/messages") | |
public class MessagesController { | |
@Autowired | |
private Map<String, AbstractTaskService> taskServicesByTaskName; | |
@RequestMapping(/* POST */) | |
public void process(@RequestBody Message message) { | |
if (taskServicesByTaskName.containsKey(message.getTaskName())) { | |
taskServicesByTaskName.get( | |
message.getTaskName()).executeWithConfig( | |
message.getConfig()); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Configuration | |
public class TaskConfig { | |
@Autowired | |
private AdvertiserListingTaskService advertiserListingTaskService; | |
@Bean | |
public Map<String, AbstractTaskService> taskServicesByTaskName() { | |
return Collections.unmodifiableMap(Steam.of( | |
entry(AdvertiserListingTask.getName(), advertiserListingTaskService), | |
entry(...), | |
...)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment