|
16 | 16 | */
|
17 | 17 | package com.uber.cadence.samples.fileprocessing;
|
18 | 18 |
|
19 |
| -import com.amazonaws.services.s3.AmazonS3; |
20 |
| -import com.uber.cadence.WorkflowService; |
21 |
| -import com.uber.cadence.samples.common.ConfigHelper; |
22 | 19 | import com.uber.cadence.worker.Worker;
|
23 |
| -import com.uber.cadence.worker.WorkerOptions; |
24 | 20 |
|
25 |
| -import java.io.IOException; |
26 |
| -import java.net.InetAddress; |
27 |
| -import java.net.UnknownHostException; |
28 |
| -import java.time.Duration; |
| 21 | +import java.lang.management.ManagementFactory; |
| 22 | + |
| 23 | +import static com.uber.cadence.samples.common.SampleConstants.DOMAIN; |
29 | 24 |
|
30 | 25 | /**
|
31 |
| - * This is the process which hosts all workflows and activities in this sample |
| 26 | + * This is the process which hosts all workflows and activities in this sample. |
| 27 | + * Run multiple instances of the worker in different windows. Then start workflow |
| 28 | + * by running FileProcessingStarter. Note that all activities always execute on the same worker. |
| 29 | + * But each time they might end up on a different worker as the first activity is dispatched to the common task list. |
32 | 30 | */
|
33 | 31 | public class FileProcessingWorker {
|
34 | 32 |
|
35 | 33 | static final String TASK_LIST = "FileProcessing";
|
36 | 34 |
|
37 |
| - public static void main(String[] args) throws Exception { |
38 |
| - ConfigHelper configHelper = ConfigHelper.createConfig(); |
39 |
| - WorkflowService.Iface swfService = configHelper.createWorkflowClient(); |
40 |
| - AmazonS3 s3Client = configHelper.createS3Client(); |
41 |
| - String domain = configHelper.getDomain(); |
| 35 | + public static void main(String[] args) { |
42 | 36 |
|
43 |
| - String localFolder = configHelper.getValueFromConfig(FileProcessingConfigKeys.ACTIVITY_WORKER_LOCALFOLDER); |
| 37 | + String hostSpecifiTaskList = ManagementFactory.getRuntimeMXBean().getName(); |
44 | 38 |
|
45 | 39 | // Start worker to poll the common task list
|
46 |
| - final Worker workerForCommonTaskList = new Worker(swfService, domain, TASK_LIST, null); |
47 |
| - SimpleStoreActivitiesS3Impl storeActivityImpl = new SimpleStoreActivitiesS3Impl(s3Client, localFolder, getHostName()); |
| 40 | + final Worker workerForCommonTaskList = new Worker(DOMAIN, TASK_LIST); |
| 41 | + workerForCommonTaskList.registerWorkflowImplementationTypes(FileProcessingWorkflowImpl.class); |
| 42 | + StoreActivitiesImpl storeActivityImpl = new StoreActivitiesImpl(hostSpecifiTaskList); |
48 | 43 | workerForCommonTaskList.registerActivitiesImplementations(storeActivityImpl);
|
49 |
| - workerForCommonTaskList.registerWorkflowImplementationTypes(FileProcessingWorkflowZipImpl.class); |
50 |
| - |
51 | 44 | workerForCommonTaskList.start();
|
52 |
| - System.out.println("Worker tarted for task list: " + TASK_LIST); |
| 45 | + System.out.println("Worker started for task list: " + TASK_LIST); |
53 | 46 |
|
54 | 47 | // Start worker to poll the host specific task list
|
55 |
| - WorkerOptions hostSpecificOptions = new WorkerOptions.Builder().build(); |
56 |
| - final Worker workerForHostSpecificTaskList = new Worker(swfService, domain, getHostName(), hostSpecificOptions); |
57 |
| - FileProcessingActivitiesZipImpl processorActivityImpl = new FileProcessingActivitiesZipImpl(localFolder); |
58 |
| - workerForHostSpecificTaskList.registerActivitiesImplementations(storeActivityImpl, processorActivityImpl); |
| 48 | + final Worker workerForHostSpecificTaskList = new Worker(DOMAIN, hostSpecifiTaskList); |
| 49 | + workerForHostSpecificTaskList.registerActivitiesImplementations(storeActivityImpl); |
59 | 50 | workerForHostSpecificTaskList.start();
|
60 |
| - System.out.println("Worker Started for activity and workflow task List: " + getHostName()); |
61 |
| - |
62 |
| - Runtime.getRuntime().addShutdownHook(new Thread() { |
63 |
| - |
64 |
| - public void run() { |
65 |
| - workerForCommonTaskList.shutdown(Duration.ofSeconds(5)); |
66 |
| - workerForHostSpecificTaskList.shutdown(Duration.ofSeconds(5)); |
67 |
| - System.out.println("Activity Workers Exited."); |
68 |
| - } |
69 |
| - }); |
70 |
| - |
71 |
| - System.out.println("Please press any key to terminate service."); |
72 |
| - |
73 |
| - try { |
74 |
| - System.in.read(); |
75 |
| - } catch (IOException e) { |
76 |
| - e.printStackTrace(); |
77 |
| - } |
78 |
| - System.exit(0); |
79 |
| - |
| 51 | + System.out.println("Worker Started for activity task List: " + hostSpecifiTaskList); |
80 | 52 | }
|
81 |
| - |
82 |
| - static String getHostName() { |
83 |
| - try { |
84 |
| - InetAddress addr = InetAddress.getLocalHost(); |
85 |
| - return addr.getHostName(); |
86 |
| - } catch (UnknownHostException e) { |
87 |
| - throw new Error(e); |
88 |
| - } |
89 |
| - } |
90 |
| - |
91 | 53 | }
|
0 commit comments