use of io.temporal.worker.WorkerFactory in project samples-java by temporalio.
the class HelloAsyncLambda method main.
/**
* With our Workflow and Activities defined, we can now start execution. The main method starts
* the worker and then the workflow.
*/
public static void main(String[] args) {
// Get a Workflow service stub.
WorkflowServiceStubs service = WorkflowServiceStubs.newInstance();
/*
* Get a Workflow service client which can be used to start, Signal, and Query Workflow Executions.
*/
WorkflowClient client = WorkflowClient.newInstance(service);
/*
* Define the workflow factory. It is used to create workflow workers for a specific task queue.
*/
WorkerFactory factory = WorkerFactory.newInstance(client);
/*
* Define the workflow worker. Workflow workers listen to a defined task queue and process
* workflows and activities.
*/
Worker worker = factory.newWorker(TASK_QUEUE);
/*
* Register our workflow implementation with the worker.
* Workflow implementations must be known to the worker at runtime in
* order to dispatch workflow tasks.
*/
worker.registerWorkflowImplementationTypes(GreetingWorkflowImpl.class);
/**
* Register our Activity Types with the Worker. Since Activities are stateless and thread-safe,
* the Activity Type is a shared instance.
*/
worker.registerActivitiesImplementations(new GreetingActivitiesImpl());
/*
* Start all the workers registered for a specific task queue.
* The started workers then start polling for workflows and activities.
*/
factory.start();
// Define our workflow options
WorkflowOptions workflowOptions = WorkflowOptions.newBuilder().setWorkflowId(WORKFLOW_ID).setTaskQueue(TASK_QUEUE).build();
// Create the workflow client stub. It is used to start our workflow execution.
GreetingWorkflow workflow = client.newWorkflowStub(GreetingWorkflow.class, workflowOptions);
/*
* Execute our workflow and wait for it to complete. The call to our getGreeting method is
* synchronous.
*/
String greeting = workflow.getGreeting("World");
// Display workflow execution results
System.out.println(greeting);
System.exit(0);
}
use of io.temporal.worker.WorkerFactory in project samples-java by temporalio.
the class HelloChild method main.
/**
* With the workflow, and child workflow defined, we can now start execution. The main method is
* the workflow starter.
*/
public static void main(String[] args) {
// Get a Workflow service stub.
WorkflowServiceStubs service = WorkflowServiceStubs.newInstance();
/*
* Get a Workflow service client which can be used to start, Signal, and Query Workflow Executions.
*/
WorkflowClient client = WorkflowClient.newInstance(service);
/*
* Define the workflow factory. It is used to create workflow workers for a specific task queue.
*/
WorkerFactory factory = WorkerFactory.newInstance(client);
/*
* Define the workflow worker. Workflow workers listen to a defined task queue and process
* workflows and activities.
*/
Worker worker = factory.newWorker(TASK_QUEUE);
/*
* Register the parent and child workflow implementation with the worker.
* Since workflows are stateful in nature,
* we need to register the workflow types.
*/
worker.registerWorkflowImplementationTypes(GreetingWorkflowImpl.class, GreetingChildImpl.class);
/*
* Start all the workers registered for a specific task queue.
* The started workers then start polling for workflows and activities.
*/
factory.start();
// Start a workflow execution. Usually this is done from another program.
// Uses task queue from the GreetingWorkflow @WorkflowMethod annotation.
// Create our parent workflow client stub. It is used to start the parent workflow execution.
GreetingWorkflow workflow = client.newWorkflowStub(GreetingWorkflow.class, WorkflowOptions.newBuilder().setWorkflowId(WORKFLOW_ID).setTaskQueue(TASK_QUEUE).build());
// Execute our parent workflow and wait for it to complete.
String greeting = workflow.getGreeting("World");
// Display the parent workflow execution results
System.out.println(greeting);
System.exit(0);
}
use of io.temporal.worker.WorkerFactory in project samples-java by temporalio.
the class HelloDynamic method main.
/**
* With our dynamic Workflow and Activities defined, we can now start execution. The main method
* starts the worker and then the workflow.
*/
public static void main(String[] arg) {
// Get a Workflow service stub.
WorkflowServiceStubs service = WorkflowServiceStubs.newInstance();
/**
* Get a Workflow service client which can be used to start, Signal, and Query Workflow
* Executions.
*/
WorkflowClient client = WorkflowClient.newInstance(service);
/**
* Define the workflow factory. It is used to create workflow workers for a specific task queue.
*/
WorkerFactory factory = WorkerFactory.newInstance(client);
/**
* Define the workflow worker. Workflow workers listen to a defined task queue and process
* workflows and activities.
*/
Worker worker = factory.newWorker(TASK_QUEUE);
/**
* Register our dynamic workflow implementation with the worker. Workflow implementations must
* be known to the worker at runtime in order to dispatch workflow tasks.
*/
worker.registerWorkflowImplementationTypes(DynamicGreetingWorkflowImpl.class);
/**
* Register our dynamic workflow activity implementation with the worker. Since workflow
* activities are stateless and thread-safe, we need to register a shared instance.
*/
worker.registerActivitiesImplementations(new DynamicGreetingActivityImpl());
/**
* Start all the Workers that are in this process. The Workers will then start polling for
* Workflow Tasks and Activity Tasks.
*/
factory.start();
/**
* Create the workflow stub Note that the Workflow type is not explicitly registered with the
* Worker
*/
WorkflowOptions workflowOptions = WorkflowOptions.newBuilder().setTaskQueue(TASK_QUEUE).setWorkflowId(WORKFLOW_ID).build();
WorkflowStub workflow = client.newUntypedWorkflowStub("DynamicWF", workflowOptions);
/**
* Start workflow execution and signal right after Pass in the workflow args and signal args
*/
workflow.signalWithStart("greetingSignal", new Object[] { "John" }, new Object[] { "Hello" });
// Wait for workflow to finish getting the results
String result = workflow.getResult(String.class);
System.out.println(result);
System.exit(0);
}
use of io.temporal.worker.WorkerFactory in project samples-java by temporalio.
the class HelloException method main.
/**
* With our Workflow and Activities defined, we can now start execution. The main method starts
* the worker and then the workflow.
*/
public static void main(String[] args) {
// Define the workflow service.
WorkflowServiceStubs service = WorkflowServiceStubs.newInstance();
/*
* Get a Workflow service client which can be used to start, Signal, and Query Workflow Executions.
*/
WorkflowClient client = WorkflowClient.newInstance(service);
/*
* Define the workflow factory. It is used to create workflow workers for a specific task queue.
*/
WorkerFactory factory = WorkerFactory.newInstance(client);
/*
* Define the workflow worker. Workflow workers listen to a defined task queue and process
* workflows and activities.
*/
Worker worker = factory.newWorker(TASK_QUEUE);
/*
* Register our workflow parent and child implementations with the worker.
* Since workflows are stateful in nature, we need to register our workflow types.
*/
worker.registerWorkflowImplementationTypes(GreetingWorkflowImpl.class, GreetingChildImpl.class);
/**
* Register our Activity Types with the Worker. Since Activities are stateless and thread-safe,
* the Activity Type is a shared instance.
*/
worker.registerActivitiesImplementations(new GreetingActivitiesImpl());
/*
* Start all the workers registered for a specific task queue.
* The started workers then start polling for workflows and activities.
*/
factory.start();
// Create our workflow options
WorkflowOptions workflowOptions = WorkflowOptions.newBuilder().setWorkflowId(WORKFLOW_ID).setTaskQueue(TASK_QUEUE).build();
// Create the workflow client stub. It is used to start our workflow execution.
GreetingWorkflow workflow = client.newWorkflowStub(GreetingWorkflow.class, workflowOptions);
try {
// Execute our parent workflow. This will call the child workflow, which then calls the
// defined workflow activity. The workflow activity throws the exception.
workflow.getGreeting("World");
throw new IllegalStateException("unreachable");
} catch (WorkflowException e) {
/*
* This stack trace should help you better understand
* how exception propagation works with Temporal.
*
* Looking at the stack trace from bottom-up (to understand the propagation) we first have:
* 1) Caused by: io.temporal.failure.ApplicationFailure: message='Hello World!', type='java.io.IOException'
* this is the IOException thrown by our activity.
* 2) Caused by: io.temporal.failure.ActivityFailure - indicates the execution failure of our activity
* 3) Caused by: io.temporal.failure.ChildWorkflowFailure - indicates the failure of our child workflow execution
* 4) io.temporal.client.WorkflowFailedException - indicates the failure of our workflow execution
*/
System.out.println("\nStack Trace:\n" + Throwables.getStackTraceAsString(e));
/* Now let's see if our original IOException was indeed propagated all the way to our
* WorkflowException which we caught in our code.
* To do this let's print out its root cause:
*/
Throwable cause = Throwables.getRootCause(e);
// here we should get our originally thrown IOException and the message "Hello World"
System.out.println("\n Root cause: " + cause.getMessage());
}
System.exit(0);
}
use of io.temporal.worker.WorkerFactory in project samples-java by temporalio.
the class HelloParallelActivity method main.
/**
* With our Workflow and Activities defined, we can now start execution. The main method starts
* the worker and then the workflow.
*/
public static void main(String[] args) {
// Define the workflow service.
WorkflowServiceStubs service = WorkflowServiceStubs.newInstance();
/*
* Get a Workflow service client which can be used to start, Signal, and Query Workflow Executions.
*/
WorkflowClient client = WorkflowClient.newInstance(service);
/*
* Define the workflow factory. It is used to create workflow workers for a specific task queue.
*/
WorkerFactory factory = WorkerFactory.newInstance(client);
/*
* Define the workflow worker. Workflow workers listen to a defined task queue and process
* workflows and activities.
*/
Worker worker = factory.newWorker(TASK_QUEUE);
/*
* Register our workflow implementation with the worker.
* Workflow implementations must be known to the worker at runtime in
* order to dispatch workflow tasks.
*/
worker.registerWorkflowImplementationTypes(MultiGreetingWorkflowImpl.class);
/**
* Register our Activity Types with the Worker. Since Activities are stateless and thread-safe,
* the Activity Type is a shared instance.
*/
worker.registerActivitiesImplementations(new GreetingActivitiesImpl());
/*
* Start all the workers registered for a specific task queue.
* The started workers then start polling for workflows and activities.
*/
factory.start();
// Create the workflow client stub. It is used to start our workflow execution.
MultiGreetingWorkflow workflow = client.newWorkflowStub(MultiGreetingWorkflow.class, WorkflowOptions.newBuilder().setWorkflowId(WORKFLOW_ID).setTaskQueue(TASK_QUEUE).build());
/*
* Execute our workflow and wait for it to complete. The call to our getGreetings method is
* synchronous.
*
*/
List<String> results = workflow.getGreetings(Arrays.asList("John", "Mary", "Michael", "Jennet"));
// Display workflow execution results
for (String result : results) {
System.out.println(result);
}
System.exit(0);
}
Aggregations