首页  

temporal Java 例子说明     所属分类 temporal 浏览量 264
temporal Java  SDK 
io.temporal:temporal-sdk:1.5.0

注意不要用 最新的 1.8.0 ,maven中央库上还没有

HelloActivity
Sample Temporal Workflow executes a single Activity

The Workflow Definition's Interface must contain one method annotated with @WorkflowMethod.

@WorkflowInterface
public interface GreetingWorkflow {
  @WorkflowMethod
  String getGreeting(String name);
}

Workflow Definitions should not contain any heavyweight computations, non-deterministic code, network calls, database operations, etc. 
Those things should be handled by the Activities.
   
工作流定义不应该包含任何重量级的计算、非确定性代码、网络调用、数据库操作等
这些事情应该由活动来处理。


  
@ActivityInterface
public interface GreetingActivities {
  @ActivityMethod(name = "greet")
  String composeGreeting(String greeting, String name);
}

Activity stubs are proxies for activity invocations that are executed outside of the workflow thread on the activity worker, 
that can be on a different host. Temporal is going to dispatch the activity results back to the workflow 
and unblock the stub as soon as activity is completed on the activity worker.
    

// gRPC connection stubs , 默认 127.0.0.1:7233 WorkflowServiceStubs service = WorkflowServiceStubs.newInstance(); // start, Signal, and Query Workflow Executions WorkflowClient client = WorkflowClient.newInstance(service); // create workflow workers for a specific task queue WorkerFactory factory = WorkerFactory.newInstance(client); Worker worker = factory.newWorker(TASK_QUEUE); Workflow workers listen to a defined task queue and process workflows and activities worker.registerWorkflowImplementationTypes(GreetingWorkflowImpl.class); worker.registerActivitiesImplementations(new GreetingActivitiesImpl()); factory.start(); // Create the workflow client stub. It is used to start our workflow execution. GreetingWorkflow workflow = client.newWorkflowStub(GreetingWorkflow.class, WorkflowOptions.newBuilder().setWorkflowId(WORKFLOW_ID).setTaskQueue(TASK_QUEUE).build());
使用 docker-compose 搭建 temporal server HelloActivity 改造 ,模拟执行超时的情况 , 一直会运行 HelloSingleActivity 可设置 超时重试策略 final GreetingActivities activities = Workflow.newActivityStub(GreetingActivities.class, ActivityOptions.newBuilder().setStartToCloseTimeout(Duration.ofSeconds(2)).build()); 超时等待时间2秒 final GreetingActivities activities = Workflow.newActivityStub(GreetingActivities.class, ActivityOptions.newBuilder().setStartToCloseTimeout(Duration.ofSeconds(2)) // 设置最大重试次数 ,否则会重试直到最大次数 .setRetryOptions(RetryOptions.newBuilder().setMaximumAttempts(1).build()) .build()); public final class RetryOptions { private static final double DEFAULT_BACKOFF_COEFFICIENT = 2.0; private static final int DEFAULT_MAXIMUM_MULTIPLIER = 100; private Builder(RetryOptions options) { if (options == null) {return;} this.backoffCoefficient = options.getBackoffCoefficient(); this.maximumAttempts = options.getMaximumAttempts(); this.initialInterval = options.getInitialInterval(); this.maximumInterval = options.getMaximumInterval(); this.doNotRetry = options.getDoNotRetry(); } getDoNotRetry List of application failures types to not retry. initialInterval Interval of the first retry. If coefficient is 1.0 then it is used for all retries backoffCoefficient Coefficient used to calculate the next retry interval. The next retry interval is previousinterval multiplied by this coefficient. Must be 1 or larger. Default is 2.0. WORKFLOW_ID 很关键 , 超时的工作流一直在重试 运行 ,状态是 running 然后修改 代码 ,WORKFLOW_ID 改成原来的 ,并把 sleep 时间 改成1秒 (超时等待时间2秒) 再度运行后 ,工作流 成功运行 结束 , 状态变为 Completed 会根据 WORKFLOW_ID 关联 未 Completed 的 工作流 ,继续运行
例子代码 https://gitee.com/dyyx/hellocode/tree/master/demo/temporal/sdkdemo
参考资料 Temporal 本地环境搭建及测试 https://docs.temporal.io/docs/java/ https://github.com/temporalio/samples-java https://github.com/temporalio/samples-java/blob/main/src/main/java/io/temporal/samples/hello/HelloActivity.java

上一篇     下一篇
docker 部署 MySQL

temporal部署连接外部的postgres

使用 Dockerfile 构建 gohttphello server 镜像

go 包管理工具 go mod

temporal 核心概念

git 分支操作