Skip to content

Sample for NexusSerializationContext - #802

Merged
JoshuaFrenchwood merged 8 commits into
mainfrom
nexus-serialization-context-sample
Oct 1, 2026
Merged

JoshuaFrenchwood merged 8 commits into
mainfrom
nexus-serialization-context-sample

Conversation

@JoshuaFrenchwood

Copy link
Copy Markdown
Contributor

What was changed

Adding a sample for NexusSerializationContext

Why?

Show users how to use NexusSerializationContext for per endpoint encryption

Checklist

  1. Closes

  2. How was this tested:

Tested locally with temporal dev server

  1. Any docs updates needed?

import javax.crypto.spec.SecretKeySpec;

/** Selects a payload encoding from the Nexus endpoint, service, and operation. */
public final class NexusCodec implements PayloadCodec {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would structure this a bit differently, I would make the NexusCodec take a map of endpoints and PayloadCodec and then pick the right one based on the context. I think it would be easier for a user to copy it and apply it to their unique situations

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would also want to show encryption here since that is the main reason customer want this feature, per endpoint encryption, so I would maybe have two endpoints one that uses compression and encryption with key A, and one that just does encryption with key B. Does that make sense?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, updated to have this.


```bash
temporal operator namespace create --namespace nexus-serialization-handler
temporal operator namespace create --namespace nexus-serialization-caller

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would lean towards separating this out more so it is a more realistic sample, we have gotten some feedback from product on our samples not being reflective of what the customer would actually do. So I would suggest having 3 namespaces here like 1 caller and two handler and each handler has its own endpoint and worker

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Created 3 in different namespaces

import io.temporal.common.converter.DefaultDataConverter;
import java.util.Collections;

public final class SampleConfig {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would remove most things here except for the endpoints since you wouldn't want to expose the handlers or callers task queue that breaks part of the abstraction of Nexus

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated

WorkflowClient client =
ClientOptions.getWorkflowClient(
args,
WorkflowClientOptions.newBuilder().setDataConverter(SampleConfig.dataConverter()));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the handler should have its own Data converter, if the caller and handler can share a data converter then it kind of removes the value of doing per endpoint stuff

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would make the caller endpoint aware and then the handlers actually don't need to be

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved the creation of Data converts to be with 3 different workers. Only the caller endpoint is aware of all the codecs

@Evanthx Evanthx left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

Promise<Void> timer = Workflow.newTimer(MAX_AWAIT_TIME);
Workflow.await(
() -> timer.isCompleted() || !unprocessedGreetings.isEmpty() || exitRequested);
boolean timedout = timer.isCompleted() && unprocessedGreetings.isEmpty() && !exitRequested;

@JoshuaFrenchwood JoshuaFrenchwood Oct 1, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a fix for unit tests, it was stalling after this SDK version change

can decode the final result before printing it.

`NexusSerializationContext` works end to end for synchronous Nexus operations.
For an asynchronous operation, the handler's final result is serialized as a workflow

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd mention this is something we are going to improve

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated

`NexusCodec` uses `NexusSerializationContext` to select the `PayloadCodec`
registered for each endpoint. The sample uses these keys:

- Key A: compresses with zlib, then encrypts the first synchronous endpoint's payloads.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of first or seconds can we just say the endpoints name?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep

Comment thread core/src/main/java/io/temporal/samples/nexusserializationcontext/README.md Outdated
temporal operator namespace create --namespace nexus-serialization-key-a-handler
temporal operator namespace create --namespace nexus-serialization-key-b-handler
temporal operator namespace create --namespace nexus-serialization-async-handler
temporal operator nexus endpoint create \

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need 4 endpoints? I was thinking the async call could just be on the same nexus endpoint?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Made this to be 2 services for Async and Sync, and two endpoints for encrypt and compress+encrypt

@JoshuaFrenchwood
JoshuaFrenchwood merged commit 4c46b45 into main Oct 1, 2026
14 of 16 checks passed
@JoshuaFrenchwood
JoshuaFrenchwood deleted the nexus-serialization-context-sample branch October 1, 2026 22:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants