# Running into an issue when creating namespace programmatically

**URL:** <https://community.temporal.io/t/running-into-an-issue-when-creating-namespace-programmatically/2783>\
**Category:** Community Support\
**Tags:** java-sdk, namespace, exception, web-ui\
**Created:** [August 24, 2021, 9:37pm UTC](https://community.temporal.io/t/running-into-an-issue-when-creating-namespace-programmatically/2783 "2021-08-24T21:37:53Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![nkatre](https://avatars.discourse-cdn.com/v4/letter/n/d6d6ee/32.png) [@nkatre](https://community.temporal.io/u/nkatre)\
**Post date:** [August 24, 2021, 9:37pm UTC](https://community.temporal.io/t/running-into-an-issue-when-creating-namespace-programmatically/2783/1 "2021-08-24T21:37:53Z")

</div>

I am creating namespace programmatically

```auto
       Worker workflowWorker = workerFactory.newWorker(WORKFLOW_TASK_QUEUE);
        workflowWorker.registerWorkflowImplementationTypes(WorkflowImpl.class);
        workflowWorker.registerActivitiesImplementations(activity);
        workerFactory.start();

        /**
         * Register namespace if not exists on the temporal server. This logic is behind a feature flag.
         */
        final boolean autoRegisterNamespace =
            (boolean)environment.getProperty("temporal.namespace.autoregister", Boolean.class, DEFAULT_NAMESPACE_AUTOREGISTER); // registration behind boolean flag
        if (autoRegisterNamespace) {
            RegisterNamespaceRequest request = RegisterNamespaceRequest.newBuilder()
                                                                   .setNamespace("hello-world")
                                                                   .setHistoryArchivalState(enableArchival
                                                                                   ? ArchivalState.ARCHIVAL_STATE_ENABLED
                                                                                   : ArchivalState.ARCHIVAL_STATE_DISABLED)
                                                                   .setDescription(NAMESPACE_DESCRIPTION)
                                                                   .setVisibilityArchivalState(enableArchival
                                                                                   ? ArchivalState.ARCHIVAL_STATE_ENABLED
                                                                                   : ArchivalState.ARCHIVAL_STATE_DISABLED)
                                                                   .setWorkflowExecutionRetentionPeriod(
                                                                                                        Durations.fromDays(workflowRetentionDays))
                                                                   .build();
        }

```

However I am getting this exception  
`io.grpc.StatusRuntimeException: NOT_FOUND: namespace: hello-world not found`

Stacktrace

```auto
{"@timestamp":"2021-08-24T21:26:00.920+00:00","@version":"1","message":"Failure in thread Host Local Workflow Poller: 3","

logger_name":"io.temporal.internal.worker.Poller","thread_name":"Host Local Workflow Poller: 3","level":"ERROR","level_val

ue":40000,"stack_trace":"io.grpc.StatusRuntimeException: NOT_FOUND: namespace: hello-world not found\n\tat io.grpc.stub.ClientC

alls.toStatusRuntimeException(ClientCalls.java:262)\n\tat io.grpc.stub.ClientCalls.getUnchecked(ClientCalls.java:243)\n\ta

t io.grpc.stub.ClientCalls.blockingUnaryCall(ClientCalls.java:156)\n\tat io.temporal.api.workflowservice.v1.WorkflowServic

eGrpc$WorkflowServiceBlockingStub.pollWorkflowTaskQueue(WorkflowServiceGrpc.java:2639)\n\tat io.temporal.internal.worker.W

orkflowPollTask.poll(WorkflowPollTask.java:81)\n\tat io.temporal.internal.worker.WorkflowPollTask.poll(WorkflowPollTask.ja

va:37)\n\tat io.temporal.internal.worker.Poller$PollExecutionTask.run(Poller.java:270)\n\tat io.temporal.internal.worker.P

oller$PollLoopTask.run(Poller.java:235)\n\tat datadog.trace.bootstrap.instrumentation.java.concurrent.Wrapper.run(Wrapper.

java:25)\n\tat java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)\n\tat java.base/j

ava.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)\n\tat java.base/java.lang.Thread.run(Thread

.java:829)\n"}

```

I wonder if this is due to the fact that the activities and workflow are registered prior to creating the namespace. If yes, when should one ideally call the logic to create namespace programmatically ?

---

<div class="post-metadata">

**Author:** ![maxim](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/maxim/32/8_2.png) [@maxim](https://community.temporal.io/u/maxim)\
**Post date:** [August 24, 2021, 9:42pm UTC](https://community.temporal.io/t/running-into-an-issue-when-creating-namespace-programmatically/2783/2 "2021-08-24T21:42:55Z")

</div>

You usually create a namespace from a separate process or `tctl` before starting your workers.

But I believe that the error you posted should go away after 10 seconds which is the time it takes for the namespace cache to be updated.

---

<div class="post-metadata">

**Author:** ![nkatre](https://avatars.discourse-cdn.com/v4/letter/n/d6d6ee/32.png) [@nkatre](https://community.temporal.io/u/nkatre)\
**Post date:** [August 24, 2021, 9:56pm UTC](https://community.temporal.io/t/running-into-an-issue-when-creating-namespace-programmatically/2783/3 "2021-08-24T21:56:29Z")

</div>

Strange… I saw this exception for a couple of hours on the temporal instance. @maxim

---

<div class="post-metadata">

**Author:** ![maxim](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/maxim/32/8_2.png) [@maxim](https://community.temporal.io/u/maxim)\
**Post date:** [August 24, 2021, 10:01pm UTC](https://community.temporal.io/t/running-into-an-issue-when-creating-namespace-programmatically/2783/4 "2021-08-24T22:01:45Z")

</div>

Did restarting workers help? Did you see the namespace in the `tctl namespace describe`?

---

<div class="post-metadata">

**Author:** ![Nitz](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/nitz/32/1510_2.png) [@Nitz](https://community.temporal.io/u/Nitz)\
**Post date:** [November 30, 2021, 2:07pm UTC](https://community.temporal.io/t/running-into-an-issue-when-creating-namespace-programmatically/2783/5 "2021-11-30T14:07:21Z")

</div>

Hi @maxim  
I have a similar requirement, where just after creating the namespace , I need to start the worker.  
but I am getting this error :

```auto
2021-11-30 19:32:59,390 [Host Local Workflow Poller: 3] ERROR io.temporal.internal.worker.Poller - Failure in thread Host Local Workflow Poller: 3
io.grpc.StatusRuntimeException: NOT_FOUND: namespace: tenant14 not found

```

How can that be done programmatically ? should I add Thread sleep after registering the namespace?  
Can I refresh the namespace cache manually ?

---

<div class="post-metadata">

**Author:** ![tihomir](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/tihomir/32/6580_2.png) [@tihomir](https://community.temporal.io/u/tihomir)\
**Post date:** [November 30, 2021, 5:11pm UTC](https://community.temporal.io/t/running-into-an-issue-when-creating-namespace-programmatically/2783/6 "2021-11-30T17:11:49Z")

</div>

Yes, there is a delay similar to when adding new search attributes, from when the request is made to when the namespace is created. I think the same recommendation applies as to search attribute creation, to wait 1 minute after request and then start using it. This is current limitation that we will address in the future.

You can use DescribeNamespaceRequest to check if the namespace exists (this should throw StatusRuntimeException if it does not):

```auto
try {
            DescribeNamespaceResponse resp = client.getWorkflowServiceStubs().blockingStub().describeNamespace(
                    DescribeNamespaceRequest.newBuilder()
                            .setNamespace("abc")
                            .build()
            );

            response.getNamespaces(0).getNamespaceInfo().getName();
        } catch(StatusRuntimeException e) {
            // ....
        }

```

To list namespaces, you can do for example:

```auto
ListNamespacesResponse response = client.getWorkflowServiceStubs().blockingStub().listNamespaces(
                ListNamespacesRequest.newBuilder().build()
        );
        List<DescribeNamespaceResponse> namespaces = response.getNamespacesList();
        for(DescribeNamespaceResponse nr : namespaces) {
            System.out.println(nr.getNamespaceInfo().getName());
        }

```

---

<div class="post-metadata">

**Author:** ![Siyang\_Xie](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/siyang_xie/32/2624_2.png) [@Siyang\_Xie](https://community.temporal.io/u/Siyang_Xie)\
**Post date:** [August 26, 2022, 11:09pm UTC](https://community.temporal.io/t/running-into-an-issue-when-creating-namespace-programmatically/2783/7 "2022-08-26T23:09:21Z")

</div>

Tried this but this doesn’t work, even after “describe namespace” request that successfully shows the new namespace info, the subsequent workflow execution failed due to namespace not found.

Looks like currently there is no better solution other than waiting 10sec or more after programmatically registering a new namespace.

---

<div class="post-metadata">

**Author:** ![tihomir](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/tihomir/32/6580_2.png) [@tihomir](https://community.temporal.io/u/tihomir)\
**Post date:** [August 26, 2022, 11:22pm UTC](https://community.temporal.io/t/running-into-an-issue-when-creating-namespace-programmatically/2783/8 "2022-08-26T23:22:55Z")

</div>

Yes, describe is going to show that the namespace exist as it got created, but the server namespace cache will not have it in its list of available namespaces until its updates (which should happen within 10s after)
