new·Earn with mozg — 20% of every monthSend somebody here and take a fifth of every plan payment they make, for as long as they keep paying — not a bounty on the first invoice. Your handle is the link, the window is thirty days, and the commission lands on your balance the second they pay. Free to join: if you have signed in, you already have the link. mozg.sh/earnall news →
mozg.beta
Sign in

Temporal · Develop · all subjects

dynamic-activities

8 notes, read out of this brain and free to use. Each one was extracted from a source and is re-checked against its exam.

Dynamic Activity implementation in Java

A dynamic Activity implementation uses the DynamicActivity interface. The execute method takes in EncodedValues that are inputs to the Activity Execution. The method can access the activity type using Activity.getExecutionContext().getInfo().getActivityType(). The execute method can return type Object and should ensure the Workflow or Client can handle the Object type return or convert the response.

Dynamic Activity example - using EncodedValues

Example of a dynamic Activity implementation: ```java public static class DynamicActivityImpl implements DynamicActivity { @Override public Object execute(EncodedValues args) { String activityType = Activity.getExecutionContext().getInfo().getActivityType(); return activityType + ": " + args.get(0, String.class) + " " + args.get(1, String.class) + " from: " + args.get(2, String.class); } } ``` This example shows how to retrieve the activity type and access individual arguments from EncodedValues.

Dynamic Workflow and Activity handlers in Java

A Worker can register one implementation of DynamicWorkflow and one of DynamicActivity alongside any number of type-specific implementations. The dynamic implementation handles Workflow and Activity Types that no registered type matches.

DynamicActivity in Java

Use DynamicActivity to handle Activity types that do not have an explicitly registered handler. When registered, it is called for any Activity type invocation without an explicit handler. Implement the execute method which takes EncodedValues. Use Activity.getExecutionContext() to get information about the Activity type that should be implemented dynamically.

Example: DynamicActivity in Java

public static class DynamicGreetingActivityImpl implements DynamicActivity { @Override public Object execute(EncodedValues args) { String activityType = Activity.getExecutionContext().getInfo().getActivityType(); return activityType + ": " + args.get(0, String.class) + " " + args.get(1, String.class) + " from: " + args.get(2, String.class); } } This example shows how to implement DynamicActivity and use Activity.getExecutionContext() to get the Activity type.

Dynamic Activity definition in Ruby SDK

A Dynamic Activity in Temporal is an Activity that is invoked dynamically at runtime if no other Activity with the same name is registered. An Activity can be made dynamic by invoking the activity_dynamic class method at the top of the definition. Only one Dynamic Activity can be present on a Worker.

activity_raw_args with dynamic activities in Ruby

The activity_raw_args method can be used in conjunction with activity_dynamic. When activity_raw_args is used, arguments are not converted but instead passed through as a splatted array of Temporalio::Converters::RawValue instances. The payload converter can then be used to convert individual payloads as needed.

Ruby dynamic activity example with raw args

class MyDynamicActivity < Temporalio::Activity::Definition # Make this the dynamic activity and accept raw args activity_dynamic activity_raw_args def execute(*raw_args) raise Temporalio::Error::ApplicationError, 'One arg expected' unless raw_args.size == 1 # Use payload converter to convert it input = Temporalio::Activity::Context.current.payload_converter.from_payload(raw_args.first.payload) "#{input['greeting']}, #{input['name']}!" end end This example shows how to define a dynamic activity in Ruby SDK that accepts raw arguments and uses the payload converter to convert them.

Give your agent this brain