|
1 | 1 | package io.temporal.spring.boot.autoconfigure.properties; |
2 | 2 |
|
| 3 | +import java.util.ArrayList; |
3 | 4 | import java.util.List; |
4 | 5 | import javax.annotation.Nullable; |
5 | 6 | import org.springframework.boot.context.properties.ConstructorBinding; |
| 7 | +import org.springframework.boot.context.properties.DeprecatedConfigurationProperty; |
6 | 8 |
|
7 | 9 | public class WorkersAutoDiscoveryProperties { |
8 | | - private final @Nullable List<String> packages; |
| 10 | + /** |
| 11 | + * When {@code true}, enables auto-registration of all {@code @ActivityImpl}-, |
| 12 | + * {@code @NexusServiceImpl}-, and {@code @WorkflowImpl}-annotated classes that are Spring-managed |
| 13 | + * beans. This is the recommended option for simple use cases where all implementations are |
| 14 | + * Spring-managed beans. It implies {@link #registerActivityBeans} and {@link |
| 15 | + * #registerNexusServiceBeans} default to {@code true}, and also enables auto-registration of |
| 16 | + * {@code @WorkflowImpl}-annotated classes that are Spring-managed beans, without needing {@link |
| 17 | + * #workflowPackages}. |
| 18 | + * |
| 19 | + * <p>Classpath-scanning via {@link #workflowPackages} (for non-bean workflow classes) still |
| 20 | + * requires explicit package configuration regardless of this flag. |
| 21 | + */ |
| 22 | + private final @Nullable Boolean enabled; |
| 23 | + |
| 24 | + private final @Nullable List<String> workflowPackages; |
| 25 | + private final @Nullable Boolean registerActivityBeans; |
| 26 | + private final @Nullable Boolean registerNexusServiceBeans; |
| 27 | + |
| 28 | + /** |
| 29 | + * @deprecated Use {@link #workflowPackages} instead. If set and non-empty, this property causes |
| 30 | + * {@link #registerActivityBeans} and {@link #registerNexusServiceBeans} to default to {@code |
| 31 | + * true}, and its entries to be considered as if they were provided through {@link |
| 32 | + * #workflowPackages}. Setting both {@link #packages} and any of the other new properties is |
| 33 | + * unsupported and will result in an exception. |
| 34 | + */ |
| 35 | + @Deprecated private final @Nullable List<String> packages; |
9 | 36 |
|
10 | 37 | @ConstructorBinding |
11 | | - public WorkersAutoDiscoveryProperties(@Nullable List<String> packages) { |
| 38 | + public WorkersAutoDiscoveryProperties( |
| 39 | + @Nullable Boolean enabled, |
| 40 | + @Nullable List<String> workflowPackages, |
| 41 | + @Nullable Boolean registerActivityBeans, |
| 42 | + @Nullable Boolean registerNexusServiceBeans, |
| 43 | + @Nullable List<String> packages) { |
| 44 | + if (packages != null |
| 45 | + && !packages.isEmpty() |
| 46 | + && (enabled != null |
| 47 | + || workflowPackages != null |
| 48 | + || registerActivityBeans != null |
| 49 | + || registerNexusServiceBeans != null)) { |
| 50 | + throw new IllegalStateException( |
| 51 | + "spring.temporal.workers-auto-discovery.packages is deprecated and cannot be combined " |
| 52 | + + "with enabled, workflow-packages, register-activity-beans, or register-nexus-service-beans. " |
| 53 | + + "Migrate to the new properties and remove packages."); |
| 54 | + } |
| 55 | + this.enabled = enabled; |
| 56 | + this.workflowPackages = workflowPackages; |
| 57 | + this.registerActivityBeans = registerActivityBeans; |
| 58 | + this.registerNexusServiceBeans = registerNexusServiceBeans; |
12 | 59 | this.packages = packages; |
13 | 60 | } |
14 | 61 |
|
| 62 | + /** |
| 63 | + * Returns whether {@code @WorkflowImpl}-annotated classes that are also Spring-managed beans |
| 64 | + * should be automatically registered with matching workers (without classpath scanning). Defaults |
| 65 | + * to {@code true} when {@link #enabled} is {@code true}, {@code false} otherwise. Workflow |
| 66 | + * implementation classes that are not Spring-managed beans still require {@link |
| 67 | + * #workflowPackages} for classpath scanning. |
| 68 | + */ |
| 69 | + public boolean isRegisterWorkflowManagedBeans() { |
| 70 | + return Boolean.TRUE.equals(enabled); |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Returns whether {@code @ActivityImpl}-annotated beans should be automatically registered with |
| 75 | + * matching workers. Defaults to {@code true} when {@link #enabled} is {@code true} or when the |
| 76 | + * deprecated {@link #packages} property is set and non-empty; {@code false} otherwise. |
| 77 | + */ |
| 78 | + public boolean isRegisterActivityBeans() { |
| 79 | + if (registerActivityBeans != null) return registerActivityBeans; |
| 80 | + return Boolean.TRUE.equals(enabled) || (packages != null && !packages.isEmpty()); |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Returns whether {@code @NexusServiceImpl}-annotated beans should be automatically registered |
| 85 | + * with matching workers. Defaults to {@code true} when {@link #enabled} is {@code true} or when |
| 86 | + * the deprecated {@link #packages} property is set and non-empty; {@code false} otherwise. |
| 87 | + */ |
| 88 | + public boolean isRegisterNexusServiceBeans() { |
| 89 | + if (registerNexusServiceBeans != null) return registerNexusServiceBeans; |
| 90 | + return Boolean.TRUE.equals(enabled) || (packages != null && !packages.isEmpty()); |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Returns the list of packages to scan for {@code @WorkflowImpl} classes. When the deprecated |
| 95 | + * {@link #packages} property is set, its entries are used; otherwise returns the entries of |
| 96 | + * {@link #workflowPackages}. The two properties cannot be set simultaneously. |
| 97 | + */ |
| 98 | + public List<String> getEffectiveWorkflowPackages() { |
| 99 | + List<String> result = new ArrayList<>(); |
| 100 | + if (packages != null) result.addAll(packages); |
| 101 | + if (workflowPackages != null) result.addAll(workflowPackages); |
| 102 | + return result; |
| 103 | + } |
| 104 | + |
| 105 | + @Nullable |
| 106 | + public List<String> getWorkflowPackages() { |
| 107 | + return workflowPackages; |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * @deprecated `packages` has unclear semantics with regard to registration of activity and nexus |
| 112 | + * service beans; use {@link #getWorkflowPackages()} instead. |
| 113 | + */ |
| 114 | + @Deprecated |
| 115 | + @DeprecatedConfigurationProperty( |
| 116 | + replacement = "spring.temporal.workers-auto-discovery.workflow-packages", |
| 117 | + reason = |
| 118 | + "'packages' has unclear semantics with regard to registration of activity and nexus service beans; use 'workflow-packages' instead") |
15 | 119 | @Nullable |
16 | 120 | public List<String> getPackages() { |
17 | 121 | return packages; |
|
0 commit comments