new·The score now tells you which way it movedA brain's exam only ever grows: its own material writes questions, and so does every question a real caller asked and did not get answered. The score is a percentage over that growing set, so a brain that learned more could post a smaller number — and this week three did. One of them answered two MORE questions than the week before and showed eighteen points less. Printed as a single percentage, that reads as decline to a reader and as punishment to anyone who contributes material.all news →
mozg.beta
Sign in

Temporal · Concepts · all subjects

design-patterns/java

66 notes in this subject, read out of this brain and free to use. This is page 2 of 2.

Retry Alerting via Metrics implementation in Java

```java import io.temporal.activity.Activity; import io.temporal.activity.ActivityExecutionContext; public class CallDownstreamActivityImpl implements CallDownstreamActivity { private static final int ALERT_THRESHOLD = 5; @Override public String callDownstreamService(String endpoint) { ActivityExecutionContext ctx = Activity.getExecutionContext(); if (ctx.getInfo().getAttempt() > ALERT_THRESHOLD) { ctx.getMetricsScope() .counter("HighActivityErrorCount") .inc(1); } return downstream.call(endpoint).getData(); } } ``` This example shows reading the attempt number from Activity execution context and incrementing a counter metric via getMetricsScope.

Retry Alerting with dimension tags in Java

```java if (ctx.getInfo().getAttempt() > ALERT_THRESHOLD) { ctx.getMetricsScope() .tagged(ImmutableMap.of( "activity_type", ctx.getInfo().getActivityType(), "endpoint", endpoint )) .counter("HighActivityErrorCount") .inc(1); } ``` Add tags to the metric using tagged() to identify which Activity type and endpoint are producing high attempt counts.

Parallel Execution in Java

In Java, use Async.function() to schedule Activities that return Promise objects. Use Promise.allOf() to wait for all of them to complete.

Java parallel Activities example

@WorkflowInterface public interface ParallelWorkflow { @WorkflowMethod List<String> processInParallel(List<String> items); } public class ParallelWorkflowImpl implements ParallelWorkflow { private final ProcessingActivity activity = Workflow.newActivityStub(ProcessingActivity.class, ActivityOptions.newBuilder() .setStartToCloseTimeout(Duration.ofSeconds(30)) .build()); @Override public List<String> processInParallel(List<String> items) { List<Promise<String>> promises = new ArrayList<>(); for (String item : items) { Promise<String> promise = Async.function(activity::process, item); promises.add(promise); } Promise.allOf(promises).get(); return promises.stream().map(Promise::get).collect(Collectors.toList()); } } This example starts one Activity per item in a list and waits for all of them to complete.

Java batch processing example

public class BatchWorkflowImpl implements BatchWorkflow { private final ProcessingActivity activity = Workflow.newActivityStub(ProcessingActivity.class, ActivityOptions.newBuilder() .setStartToCloseTimeout(Duration.ofSeconds(30)) .build()); @Override public BatchResult processBatch(List<String> items, int maxParallel) { List<String> results = new ArrayList<>(); for (int i = 0; i < items.size(); i += maxParallel) { int end = Math.min(i + maxParallel, items.size()); List<String> batch = items.subList(i, end); List<Promise<String>> promises = batch.stream() .map(item -> Async.function(activity::process, item)) .collect(Collectors.toList()); Promise.allOf(promises).get(); results.addAll(promises.stream().map(Promise::get).collect(Collectors.toList())); } return new BatchResult(results); } } This example implements controlled parallelism by processing items in batches.

Error handling in parallel execution - Java

public class ResilientParallelWorkflowImpl implements ParallelWorkflow { private final ProcessingActivity activity = Workflow.newActivityStub(ProcessingActivity.class, ActivityOptions.newBuilder() .setStartToCloseTimeout(Duration.ofSeconds(30)) .build()); @Override public ProcessingReport processWithErrorHandling(List<String> items) { List<Promise<Result>> promises = new ArrayList<>(); for (String item : items) { Promise<Result> promise = Async.function(() -> { try { return activity.process(item); } catch (Exception e) { return Result.failed(item, e.getMessage()); } }); promises.add(promise); } Promise.allOf(promises).get(); List<Result> results = promises.stream().map(Promise::get).collect(Collectors.toList()); return new ProcessingReport(results); } } This example wraps each Activity in error handling so that individual failures do not prevent other Activities from completing.

Give your agent this brain