Skip to content

Commit a30bcf6

Browse files
chuck-dbosdevhawk
andauthored
Portable JSON serializer (Java) (#280)
Allow Java to interoperate with TS/Py via simple JSON serialization to the workflow_status and other tables --------- Co-authored-by: Harry Pierson <harry.pierson@dbos.dev>
1 parent 50e19b6 commit a30bcf6

43 files changed

Lines changed: 3613 additions & 269 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

transact/src/main/java/dev/dbos/transact/DBOS.java

Lines changed: 64 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import dev.dbos.transact.workflow.ForkOptions;
2020
import dev.dbos.transact.workflow.ListWorkflowsInput;
2121
import dev.dbos.transact.workflow.Queue;
22+
import dev.dbos.transact.workflow.SerializationStrategy;
2223
import dev.dbos.transact.workflow.StepInfo;
2324
import dev.dbos.transact.workflow.StepOptions;
2425
import dev.dbos.transact.workflow.Workflow;
@@ -144,7 +145,13 @@ private void registerClassWorkflows(
144145

145146
String name = wfTag.name().isEmpty() ? method.getName() : wfTag.name();
146147
workflowRegistry.register(
147-
className, name, target, instanceName, method, wfTag.maxRecoveryAttempts());
148+
className,
149+
name,
150+
target,
151+
instanceName,
152+
method,
153+
wfTag.maxRecoveryAttempts(),
154+
wfTag.serializationStrategy());
148155
return name;
149156
}
150157

@@ -545,6 +552,17 @@ public static <T, E extends Exception> T getResult(@NonNull String workflowId) t
545552
return executor("getWorkflowStatus").getWorkflowStatus(workflowId);
546553
}
547554

555+
/**
556+
* Get the serialization format of the current workflow context.
557+
*
558+
* @return the serialization format name (e.g., "portable_json", "java_jackson"), or null if not
559+
* in a workflow context or using default serialization
560+
*/
561+
public static @Nullable SerializationStrategy getSerialization() {
562+
var ctx = DBOSContextHolder.get();
563+
return ctx != null ? ctx.getSerialization() : null;
564+
}
565+
548566
/**
549567
* Send a message to a workflow
550568
*
@@ -558,8 +576,33 @@ public static void send(
558576
@NonNull Object message,
559577
@NonNull String topic,
560578
@Nullable String idempotencyKey) {
579+
send(destinationId, message, topic, idempotencyKey, null);
580+
}
581+
582+
/**
583+
* Send a message to a workflow with serialization strategy
584+
*
585+
* @param destinationId recipient of the message
586+
* @param message message to be sent
587+
* @param topic topic to which the message is send
588+
* @param idempotencyKey optional idempotency key for exactly-once send
589+
* @param serialization serialization strategy to use (null for default)
590+
*/
591+
public static void send(
592+
@NonNull String destinationId,
593+
@NonNull Object message,
594+
@NonNull String topic,
595+
@Nullable String idempotencyKey,
596+
@Nullable SerializationStrategy serialization) {
597+
if (serialization == null) serialization = SerializationStrategy.DEFAULT;
561598
executor("send")
562-
.send(destinationId, message, topic, instance().internalWorkflowsService, idempotencyKey);
599+
.send(
600+
destinationId,
601+
message,
602+
topic,
603+
instance().internalWorkflowsService,
604+
idempotencyKey,
605+
serialization);
563606
}
564607

565608
/**
@@ -571,7 +614,7 @@ public static void send(
571614
*/
572615
public static void send(
573616
@NonNull String destinationId, @NonNull Object message, @NonNull String topic) {
574-
DBOS.send(destinationId, message, topic, null);
617+
DBOS.send(destinationId, message, topic, null, null);
575618
}
576619

577620
/**
@@ -586,13 +629,29 @@ public static void send(
586629
}
587630

588631
/**
589-
* Call within a workflow to publish a key value pair
632+
* Call within a workflow to publish a key value pair. Uses the workflow's serialization format.
590633
*
591634
* @param key identifier for published data
592635
* @param value data that is published
593636
*/
594637
public static void setEvent(@NonNull String key, @NonNull Object value) {
595-
executor("setEvent").setEvent(key, value);
638+
setEvent(key, value, null);
639+
}
640+
641+
/**
642+
* Call within a workflow to publish a key value pair with a specific serialization strategy.
643+
*
644+
* @param key identifier for published data
645+
* @param value data that is published
646+
* @param serialization serialization strategy to use (null to use workflow's default)
647+
*/
648+
public static void setEvent(
649+
@NonNull String key, @NonNull Object value, @Nullable SerializationStrategy serialization) {
650+
// If no explicit serialization specified, use the workflow context's serialization
651+
if (serialization == null) {
652+
serialization = getSerialization();
653+
}
654+
executor("setEvent").setEvent(key, value, serialization);
596655
}
597656

598657
/**

0 commit comments

Comments
 (0)