-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathDBOSAutoConfiguration.java
More file actions
193 lines (170 loc) · 6.76 KB
/
DBOSAutoConfiguration.java
File metadata and controls
193 lines (170 loc) · 6.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package dev.dbos.transact.spring;
import dev.dbos.transact.DBOS;
import dev.dbos.transact.config.DBOSConfig;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import java.util.Objects;
import javax.sql.DataSource;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.SmartLifecycle;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
/**
* Spring Boot auto-configuration for DBOS Transact. Creates a {@link DBOSConfig} and {@link DBOS}
* bean from {@code dbos.*} application properties and manages the DBOS lifecycle alongside the
* Spring application context.
*
* <p>The {@link DBOS} instance is started (via {@link DBOS#launch()}) after all other beans have
* been initialized, so workflows and queues may be registered in {@code @PostConstruct} methods
* before launch occurs.
*
* <p>To customize the auto-configured {@link DBOSConfig} without replacing it, declare one or more
* {@link DBOSConfigCustomizer} beans. To replace it entirely, declare your own {@code @Bean
* DBOSConfig}.
*
* <p>If a {@link DataSource} bean is present in the context and no {@code dbos.datasource.*}
* properties are set, that datasource will be used automatically.
*/
@AutoConfiguration
@ConditionalOnClass(DBOS.class)
@EnableConfigurationProperties(DBOSProperties.class)
@EnableAspectJAutoProxy
public class DBOSAutoConfiguration {
@Bean
@ConditionalOnMissingBean
public DBOSConfig dbosConfig(
DBOSProperties props,
@Value("${spring.application.name:#{null}}") String springAppName,
ObjectProvider<DBOSConfigCustomizer> customizers) {
DBOSConfig config = buildConfig(props, springAppName);
for (DBOSConfigCustomizer customizer : customizers.orderedStream().toList()) {
config = customizer.customize(config);
}
return config;
}
@Bean
@ConditionalOnMissingBean
public DBOS dbos(DBOSConfig config, ObjectProvider<DataSource> dataSourceProvider) {
if (config.databaseUrl() == null && config.dataSource() == null) {
DataSource dataSource = dataSourceProvider.getIfAvailable();
if (dataSource != null) {
validatePostgresDataSource(dataSource);
config = config.withDataSource(dataSource);
}
} else if (config.dataSource() != null) {
validatePostgresDataSource(config.dataSource());
}
return new DBOS(config);
}
private static void validatePostgresDataSource(DataSource dataSource) {
try (Connection conn = dataSource.getConnection()) {
String productName = conn.getMetaData().getDatabaseProductName();
if (!productName.toLowerCase().contains("postgresql")) {
throw new IllegalStateException(
"DBOS requires a PostgreSQL datasource, but the provided datasource reports: "
+ productName);
}
} catch (SQLException e) {
throw new IllegalStateException("Failed to validate DBOS datasource", e);
}
}
@Bean
@ConditionalOnMissingBean
public DBOSLifecycle dbosLifecycle(DBOS dbos) {
return new DBOSLifecycle(dbos);
}
@Bean
@ConditionalOnMissingBean
public DBOSAspect dbosAspect(DBOS dbos, ApplicationContext applicationContext) {
return new DBOSAspect(dbos, applicationContext);
}
@Bean
@ConditionalOnMissingBean
public DBOSWorkflowRegistrar dbosWorkflowRegistrar(
DBOS dbos, ApplicationContext applicationContext) {
return new DBOSWorkflowRegistrar(dbos, applicationContext);
}
@SuppressWarnings("removal")
private DBOSConfig buildConfig(DBOSProperties props, String springAppName) {
String appName =
props.getApplication().getName() != null ? props.getApplication().getName() : springAppName;
Objects.requireNonNull(
appName, "neither dbos.application.name nor spring.application.name are set");
DBOSConfig config = DBOSConfig.defaults(appName);
DBOSProperties.Datasource ds = props.getDatasource();
if (ds.getUrl() != null) {
config = config.withDatabaseUrl(ds.getUrl());
}
if (ds.getUsername() != null) {
config = config.withDbUser(ds.getUsername());
}
if (ds.getPassword() != null) {
config = config.withDbPassword(ds.getPassword());
}
if (props.getConductor().getKey() != null) {
config = config.withConductorKey(props.getConductor().getKey());
}
if (props.getConductor().getDomain() != null) {
config = config.withConductorDomain(props.getConductor().getDomain());
}
if (props.getApplication().getVersion() != null) {
config = config.withAppVersion(props.getApplication().getVersion());
}
if (props.getExecutorId() != null) {
config = config.withExecutorId(props.getExecutorId());
}
if (props.getDatasource().getSchema() != null) {
config = config.withDatabaseSchema(props.getDatasource().getSchema());
}
if (props.getSchedulerPollingInterval() != null) {
config = config.withSchedulerPollingInterval(props.getSchedulerPollingInterval());
}
config = config.withAdminServer(props.getAdminServer().isEnabled());
config = config.withAdminServerPort(props.getAdminServer().getPort());
config = config.withMigrate(props.getDatasource().isMigrate());
config = config.withUseListenNotify(props.getDatasource().isUseListenNotify());
config = config.withEnablePatching(props.isEnablePatching());
List<String> listenQueues = props.getListenQueues();
if (!listenQueues.isEmpty()) {
config = config.withListenQueues(listenQueues.toArray(String[]::new));
}
return config;
}
/**
* Manages the {@link DBOS} lifecycle within the Spring application context. {@link DBOS#launch()}
* is called after all beans are initialized; {@link DBOS#shutdown()} is called on context close.
*/
public static class DBOSLifecycle implements SmartLifecycle {
private final DBOS dbos;
private volatile boolean running = false;
public DBOSLifecycle(DBOS dbos) {
this.dbos = dbos;
}
@Override
public void start() {
dbos.launch();
running = true;
}
@Override
public void stop() {
dbos.shutdown();
running = false;
}
@Override
public boolean isRunning() {
return running;
}
/** High phase value ensures DBOS starts last (after all beans are ready) and stops first. */
@Override
public int getPhase() {
return DEFAULT_PHASE;
}
}
}