Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ to docs, or any other relevant information.
but change for calls after a workflow reset point (#3238).
- Client request metrics (`temporal_long_request_latency` etc.) now always carry the `update_name` tag (default
`none`), fixing Prometheus tag-key conflicts when Workflow Updates are used (#3210).
- Spring Boot now applies `TemporalOptionsCustomizer` beans in the same order as an injected
list of beans, honoring `Ordered`, `PriorityOrdered`, and ordering annotations (#3244).

## [1.41.1] - 2026-11-16

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@
* Beans of this class can be added to Spring context to get a fine control over *Options objects
* that are created by Temporal Spring Boot Autoconfigure module.
*
* <p>Multiple beans of each generic type can be added to Spring context. They will be ordered,
* taking into account {@link org.springframework.core.Ordered Ordered} and {@link
* org.springframework.core.annotation.Order @Order} values of the target
* <p>Multiple beans of each generic type can be added to Spring context. They are applied in the
* same order Spring uses for an injected list of beans, including support for {@link
* org.springframework.core.PriorityOrdered PriorityOrdered}, {@link
* org.springframework.core.Ordered Ordered}, and {@link
* org.springframework.core.annotation.Order @Order} on a bean class or factory method.
*
* @param <T> Temporal Options Builder to customize. Respected types: {@link
* WorkflowServiceStubsOptions.Builder}, {@link WorkflowClientOptions.Builder}, {@link
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,19 @@
import io.temporal.spring.boot.TemporalOptionsCustomizer;
import io.temporal.spring.boot.autoconfigure.properties.NonRootNamespaceProperties;
import io.temporal.spring.boot.autoconfigure.properties.TemporalProperties;
import java.lang.reflect.Method;
import java.util.*;
import java.util.Map.Entry;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import javax.annotation.Nullable;
import org.springframework.beans.factory.ListableBeanFactory;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.NoUniqueBeanDefinitionException;
import org.springframework.core.OrderComparator;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.AnnotationAwareOrderComparator;
import org.springframework.core.annotation.Order;

class AutoConfigurationUtils {

Expand Down Expand Up @@ -97,42 +99,44 @@ static List<WorkerInterceptor> chooseWorkerInterceptors(
return workerInterceptor;
}

/**
* Create a comparator that can extract @Order and @Priority from beans in the given bean factory.
* This is needed because the default OrderComparator doesn't know about the bean factory and
* therefore can't look up annotations on beans.
*/
private static Comparator<Object> beanFactoryAwareOrderComparator(
ListableBeanFactory beanFactory) {
return OrderComparator.INSTANCE.withSourceProvider(
o -> {
if (!(o instanceof Map.Entry)) {
throw new IllegalStateException("Unexpected object type: " + o);
}
Map.Entry<String, TemporalOptionsCustomizer<?>> entry =
(Map.Entry<String, TemporalOptionsCustomizer<?>>) o;
// Check if the bean itself has a Priority annotation
Integer priority = AnnotationAwareOrderComparator.INSTANCE.getPriority(entry.getValue());
if (priority != null) {
return (Ordered) () -> priority;
}

// Check if the bean factory method or the bean has an Order annotations
String beanName = entry.getKey();
if (beanName != null) {
Order order = beanFactory.findAnnotationOnBean(beanName, Order.class);
if (order != null) {
return (Ordered) order::value;
}
}
// Provide the same order metadata Spring supplies when injecting a List of beans.
@Nullable
private static Object[] beanOrderSources(
ConfigurableListableBeanFactory beanFactory, String beanName, Object bean) {
BeanDefinition definition;
try {
definition = beanFactory.getMergedBeanDefinition(beanName);
} catch (NoSuchBeanDefinitionException e) {
// Manually registered singletons may not have a bean definition.
return null;
}

// Nothing present
return null;
});
List<Object> sources = new ArrayList<>(3);
// AbstractBeanDefinition.ORDER_ATTRIBUTE is only available starting in Spring Framework 6.2.
Object order = definition.getAttribute("order");
if (order != null) {
if (!(order instanceof Integer)) {
throw new IllegalStateException(
"The 'order' attribute on bean '" + beanName + "' must be an Integer.");
}
sources.add((Ordered) () -> (Integer) order);
}
if (definition instanceof RootBeanDefinition) {
RootBeanDefinition rootDefinition = (RootBeanDefinition) definition;
Method factoryMethod = rootDefinition.getResolvedFactoryMethod();
if (factoryMethod != null) {
sources.add(factoryMethod);
}
Class<?> targetType = rootDefinition.getTargetType();
if (targetType != null && targetType != bean.getClass()) {
sources.add(targetType);
}
}
return sources.toArray();
}

static <T> List<TemporalOptionsCustomizer<T>> chooseTemporalCustomizerBeans(
ListableBeanFactory beanFactory,
ConfigurableListableBeanFactory beanFactory,
Map<String, TemporalOptionsCustomizer<T>> customizerMap,
Class<T> genericOptionsBuilderClass,
TemporalProperties properties) {
Expand All @@ -155,10 +159,21 @@ static <T> List<TemporalOptionsCustomizer<T>> chooseTemporalCustomizerBeans(
customizerStream =
customizerStream.filter(entry -> !nonRootBeanNames.contains(entry.getKey()));
}
return customizerStream
.sorted(beanFactoryAwareOrderComparator(beanFactory))
.map(Entry::getValue)
.collect(Collectors.toList());

// Keep the filtered bean names for metadata lookup, but compare the beans themselves so that
// Spring can distinguish PriorityOrdered from Ordered.
Map<Object, String> beanNames = new IdentityHashMap<>();
List<TemporalOptionsCustomizer<T>> customizers = new ArrayList<>();
customizerStream.forEach(
entry -> {
TemporalOptionsCustomizer<T> customizer = entry.getValue();
beanNames.put(customizer, entry.getKey());
customizers.add(customizer);
});
customizers.sort(
AnnotationAwareOrderComparator.INSTANCE.withSourceProvider(
bean -> beanOrderSources(beanFactory, beanNames.get(bean), bean)));
return customizers;
}

static String temporalCustomizerBeanName(String beanPrefix, Class<?> optionsBuilderClass) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,18 @@
import io.temporal.opentracing.OpenTracingActivityClientInterceptor;
import io.temporal.spring.boot.TemporalOptionsCustomizer;
import io.temporal.spring.boot.autoconfigure.template.ClientTemplate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.core.Ordered;
import org.springframework.core.PriorityOrdered;
import org.springframework.core.annotation.Order;

class ActivityClientAutoConfigurationTest {
Expand Down Expand Up @@ -103,6 +107,39 @@ void appliesRootOptionsAndOrderedCustomizers(boolean testServer) {
});
}

@Test
void appliesCustomizersThatImplementOrderedInAscendingOrder() {
List<Integer> invocationOrder = new ArrayList<>();
contextRunner
.withBean(
"later", OrderedCustomizer.class, () -> new OrderedCustomizer(20, invocationOrder))
.withBean(
"earlier", OrderedCustomizer.class, () -> new OrderedCustomizer(10, invocationOrder))
.run(
context -> {
assertThat(context).hasNotFailed().hasSingleBean(ActivityClient.class);
assertThat(invocationOrder).containsExactly(10, 20);
});
}

@Test
void appliesCustomizersInSpringOrder() {
contextRunner
.withUserConfiguration(CustomizerOrderingConfiguration.class)
.run(
context -> {
assertThat(context).hasNotFailed().hasSingleBean(ActivityClient.class);
List<Integer> springOrder = new ArrayList<>();
context
.getBeanProvider(OrderedCustomizer.class)
.orderedStream()
.forEach(customizer -> springOrder.add(customizer.getOrder()));
assertThat(springOrder).containsExactly(40, 50, 20, 30);
CustomizerOrder order = context.getBean(CustomizerOrder.class);
assertThat(order.invoked).containsExactlyElementsOf(springOrder);
});
}

@ParameterizedTest
@ValueSource(booleans = {false, true})
void registersTracingBeforeExplicitInterceptors(boolean testServer) {
Expand Down Expand Up @@ -160,6 +197,73 @@ void appliesNonRootOptionsByAliasOrNamespace() {
});
}

private static class OrderedCustomizer
implements TemporalOptionsCustomizer<ActivityClientOptions.Builder>, Ordered {
private final int order;
private final List<Integer> invocationOrder;

OrderedCustomizer(int order, List<Integer> invocationOrder) {
this.order = order;
this.invocationOrder = invocationOrder;
}

@Override
public int getOrder() {
return order;
}

@Override
public ActivityClientOptions.Builder customize(ActivityClientOptions.Builder builder) {
invocationOrder.add(order);
return builder;
}
}

@Order(0)
static class AnnotatedOrderedCustomizer extends OrderedCustomizer {
AnnotatedOrderedCustomizer(int order, List<Integer> invocationOrder) {
super(order, invocationOrder);
}
}

static class PriorityOrderedCustomizer extends OrderedCustomizer implements PriorityOrdered {
PriorityOrderedCustomizer(int order, List<Integer> invocationOrder) {
super(order, invocationOrder);
}
}

static class CustomizerOrder {
final List<Integer> invoked = new ArrayList<>();
}

static class CustomizerOrderingConfiguration {
@Bean
CustomizerOrder customizerOrder() {
return new CustomizerOrder();
}

@Bean
AnnotatedOrderedCustomizer classAnnotatedCustomizer(CustomizerOrder order) {
return new AnnotatedOrderedCustomizer(30, order.invoked);
}

@Bean
PriorityOrderedCustomizer priorityOrderedCustomizer(CustomizerOrder order) {
return new PriorityOrderedCustomizer(40, order.invoked);
}

@Bean
@Order(10)
AnnotatedOrderedCustomizer methodAnnotatedCustomizer(CustomizerOrder order) {
return new AnnotatedOrderedCustomizer(50, order.invoked);
}

@Bean
OrderedCustomizer plainOrderedCustomizer(CustomizerOrder order) {
return new OrderedCustomizer(20, order.invoked);
}
}

static class ActivityClientConsumer {
private final ActivityClient client;

Expand Down
Loading