Skip to content

Instantly share code, notes, and snippets.

@martyychang
Created May 24, 2016 19:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save martyychang/ff1c4fdb62ec9cedb06d7d3bff969000 to your computer and use it in GitHub Desktop.
Save martyychang/ff1c4fdb62ec9cedb06d7d3bff969000 to your computer and use it in GitHub Desktop.
How to map task names to task services
@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
}
}
@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());
}
}
}
@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