use of io.temporal.client.WorkflowExecutionAlreadyStarted in project sdk-java by temporalio.
the class ChildWorkflowStateMachine method notifyStartFailed.
private void notifyStartFailed() {
StartChildWorkflowExecutionFailedEventAttributes attributes = currentEvent.getStartChildWorkflowExecutionFailedEventAttributes();
Exception failure = new ChildWorkflowTaskFailedException(currentEvent.getEventId(), WorkflowExecution.newBuilder().setWorkflowId(attributes.getWorkflowId()).build(), attributes.getWorkflowType(), RetryState.RETRY_STATE_NON_RETRYABLE_FAILURE, null);
failure.initCause(new WorkflowExecutionAlreadyStarted(WorkflowExecution.newBuilder().setWorkflowId(attributes.getWorkflowId()).build(), attributes.getWorkflowType().getName(), null));
completionCallback.apply(Optional.empty(), failure);
}
use of io.temporal.client.WorkflowExecutionAlreadyStarted in project samples-java by temporalio.
the class HelloPeriodic method main.
/**
* With our Workflow and Activities defined, we can now start execution. The main method starts
* the worker and then the workflow.
*/
@SuppressWarnings(// in this simple example advanced error logging is not required
"CatchAndPrintStackTrace")
public static void main(String[] args) throws InterruptedException {
// 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(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();
// Create the workflow client stub. It is used to start our workflow execution.
GreetingWorkflow workflow = client.newWorkflowStub(GreetingWorkflow.class, // At most one instance.
WorkflowOptions.newBuilder().setWorkflowId(WORKFLOW_ID).setTaskQueue(TASK_QUEUE).build());
// Execute our workflow.
try {
WorkflowClient.start(workflow::greetPeriodically, "World");
System.out.println("Started a new GreetingWorkflow.");
} catch (WorkflowExecutionAlreadyStarted e) {
// If the workflow is already running, we cannot start it. Instead, we will connect to the
// existing instance.
workflow = client.newWorkflowStub(GreetingWorkflow.class, WORKFLOW_ID);
System.out.println("Connected to an existing GreetingWorkflow.");
}
// The workflow is running now. In this example, we pause for a few seconds to observe its
// output.
final int ObservationPeriodSecs = 20;
System.out.println("Observing the workflow execution for " + ObservationPeriodSecs + " seconds.");
try {
Thread.sleep(ObservationPeriodSecs * 1000);
} catch (InterruptedException intEx) {
intEx.printStackTrace();
}
// Signal the workflow to exit.
System.out.println("Requesting the workflow to exit.");
workflow.requestExit();
// Allow the workflow to finish before the worker is shut down.
WorkflowStub.fromTyped(workflow).getResult(void.class);
System.out.println("Good bye.");
System.exit(0);
}
use of io.temporal.client.WorkflowExecutionAlreadyStarted in project samples-java by temporalio.
the class DynamicSleepWorkflowStarter method main.
public static void main(String[] args) {
WorkflowServiceStubs service = WorkflowServiceStubs.newInstance();
WorkflowClient client = WorkflowClient.newInstance(service);
DynamicSleepWorkflow workflow = client.newWorkflowStub(DynamicSleepWorkflow.class, WorkflowOptions.newBuilder().setTaskQueue(TASK_QUEUE).setWorkflowId(DYNAMIC_SLEEP_WORKFLOW_ID).setWorkflowIdReusePolicy(WorkflowIdReusePolicy.WORKFLOW_ID_REUSE_POLICY_ALLOW_DUPLICATE).build());
try {
// Start asynchronously
WorkflowExecution execution = WorkflowClient.start(workflow::execute, System.currentTimeMillis() + 60000);
logger.info("Workflow started: " + execution);
} catch (WorkflowExecutionAlreadyStarted e) {
logger.info("Workflow already running: " + e.getExecution());
}
}
use of io.temporal.client.WorkflowExecutionAlreadyStarted in project sdk-java by temporalio.
the class WorkflowStubImpl method wrapStartException.
private RuntimeException wrapStartException(String workflowId, String workflowType, StatusRuntimeException e) {
WorkflowExecution.Builder executionBuilder = WorkflowExecution.newBuilder().setWorkflowId(workflowId);
WorkflowExecutionAlreadyStartedFailure f = StatusUtils.getFailure(e, WorkflowExecutionAlreadyStartedFailure.class);
if (f != null) {
WorkflowExecution exe = executionBuilder.setRunId(f.getRunId()).build();
execution.set(exe);
return new WorkflowExecutionAlreadyStarted(exe, workflowType, e);
} else {
WorkflowExecution exe = executionBuilder.build();
return new WorkflowServiceException(exe, workflowType, e);
}
}
use of io.temporal.client.WorkflowExecutionAlreadyStarted in project samples-java by temporalio.
the class HelloCron method main.
/**
* With the 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 the 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 the workflow activity implementation with the worker. Since workflow activities are
* stateless and thread-safe, we need to register 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. Note that the cron definition is not part of the
* core workflow definition. Workflow options allow you to execute the same
* workflow in different ways (for example with or without a cron, etc).
*
* Here we use setCronSchedule to define a cron for our workflow execution.
* The cron format is parsed by the https://github.com/robfig/cron" library.
* In addition to the standard "* * * * *" format Temporal also supports the "@every" as well as
* other cron definition extensions. For example you could define "@every 2s" to define a cron definition
* which executes our workflow every two seconds.
*
* The defined cron expression "* * * * *" means that our workflow should execute every minute.
*
* We also use setWorkflowExecutionTimeout to define the workflow execution total time (set to three minutes).
* After this time, our workflow execution ends (and our cron will stop executing as well).
*
* The setWorkflowRunTimeout defines the amount of time after which a single workflow instance is terminated.
*
* So given all our settings in the WorkflowOptions we define the following:
* "Execute our workflow once every minute for three minutes.
* Once a workflow instance is started, terminate it after one minute (if its still running)"
*
*/
WorkflowOptions workflowOptions = WorkflowOptions.newBuilder().setWorkflowId(WORKFLOW_ID).setTaskQueue(TASK_QUEUE).setCronSchedule("* * * * *").setWorkflowExecutionTimeout(Duration.ofMinutes(3)).setWorkflowRunTimeout(Duration.ofMinutes(1)).build();
// Create the workflow client stub. It is used to start our workflow execution.
GreetingWorkflow workflow = client.newWorkflowStub(GreetingWorkflow.class, workflowOptions);
try {
// start workflow execution
WorkflowExecution execution = WorkflowClient.start(workflow::greet, "World");
System.out.println("Started " + execution);
} catch (WorkflowExecutionAlreadyStarted e) {
// Thrown when a workflow with the same WORKFLOW_ID is currently running
System.out.println("Already running as " + e.getExecution());
} catch (Throwable e) {
e.printStackTrace();
System.exit(1);
}
}
Aggregations