Skip to content

Commit d80545e

Browse files
authored
Merge pull request #421 from Saphri/tlscontext-on-main
Eager TLS resolution to avoid CDI lookups on worker threads
2 parents f2d3eca + 94060a3 commit d80545e

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

runtime/src/main/java/io/quarkiverse/reactive/messaging/nats/jetstream/client/connection/ConnectionFactoryImpl.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import java.util.concurrent.atomic.AtomicReference;
66

7+
import jakarta.annotation.PostConstruct;
78
import jakarta.annotation.PreDestroy;
89
import jakarta.enterprise.context.ApplicationScoped;
910
import jakarta.enterprise.inject.Produces;
@@ -23,6 +24,16 @@ public class ConnectionFactoryImpl implements ConnectionFactory {
2324
private final TlsContext tlsContext;
2425
private final AtomicReference<Connection> connection = new AtomicReference<>();
2526

27+
@PostConstruct
28+
void init() {
29+
// Force TLS context resolution on CDI/main thread to avoid CDI lookups from worker threads
30+
try {
31+
tlsContext.sslContext();
32+
} catch (Exception e) {
33+
log.warn("Failed to eagerly initialize TLS context", e);
34+
}
35+
}
36+
2637
@Produces
2738
@Override
2839
public Connection create() {

runtime/src/main/java/io/quarkiverse/reactive/messaging/nats/jetstream/client/connection/TlsContextFactoryImpl.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.util.Optional;
44

5+
import jakarta.annotation.PostConstruct;
56
import jakarta.enterprise.context.ApplicationScoped;
67
import jakarta.enterprise.inject.Produces;
78

@@ -17,24 +18,30 @@
1718
public class TlsContextFactoryImpl implements TlsContextFactory {
1819
private final ConnectorConfiguration configuration;
1920
private final TlsConfigurationRegistry registry;
21+
private volatile TlsContext cached;
2022

21-
@Produces
22-
@Override
23-
public TlsContext create() {
23+
@PostConstruct
24+
void init() {
2425
try {
2526
if (configuration.connection().sslEnabled().orElse(false)) {
2627
final var tlsConfiguration = configuration.connection().tlsConfigurationName()
2728
.flatMap(registry::get)
2829
.orElseGet(this::getDefaultTlsConfiguration);
29-
return new TlsContextImpl(Optional.of(tlsConfiguration.createSSLContext()));
30+
cached = new TlsContextImpl(Optional.of(tlsConfiguration.createSSLContext()));
3031
} else {
31-
return new TlsContextImpl(Optional.empty());
32+
cached = new TlsContextImpl(Optional.empty());
3233
}
3334
} catch (Exception e) {
3435
throw new RuntimeException(e);
3536
}
3637
}
3738

39+
@Produces
40+
@Override
41+
public TlsContext create() {
42+
return cached;
43+
}
44+
3845
private TlsConfiguration getDefaultTlsConfiguration() {
3946
return registry.getDefault().orElseThrow(
4047
() -> new IllegalStateException("No Quarkus TLS configuration found for NATS JetStream connection"));

0 commit comments

Comments
 (0)