Search code examples
temporal-workflow

Temporal: How to create a namespace programatically?


It is possible to create a namespace via the CLI, but how to do it using Java SDK?


Solution

  • Programmatically it is done through gRPC API exposed by the service.

    In Java the generated gRPC client is accessible through WorkflowServiceStubs:

    WorkflowServiceStubs service =
        WorkflowServiceStubs.newInstance(
            WorkflowServiceStubsOptions.newBuilder().setTarget(serviceAddress).build());
    RegisterNamespaceRequest request =
        RegisterNamespaceRequest.newBuilder()
            .setNamespace(NAMESPACE)
            .setWorkflowExecutionRetentionPeriod(Durations.fromDays(7))
            .build();
    service.blockingStub().registerNamespace(request);
    

    In Go SDK you can use higher-level NamespaceClient:

    client, err := client.NewNamespaceClient(client.Options{HostPort: ts.config.ServiceAddr})
        ...
    err = client.Register(ctx, &workflowservice.RegisterNamespaceRequest{
        Name:                             name,
        WorkflowExecutionRetentionPeriod: &retention,
    })
    

    OP and additional discussion here.