I would like to create a library that does auditing and sends audit data via NATS. There is an issue, however. By default, Quarkus makes an attempt to connect to NATS server on startup every time if this dependency is present:
<dependency>
<groupId>io.quarkiverse.reactivemessaging.nats-jetstream</groupId>
<artifactId>quarkus-reactive-messsaging-nats-jetstream</artifactId>
<version>1.10.4</version>
</dependency>
I would love to configure it so it works based on my config like this:
quarkus.audit.nats.enabled=false
And then have a bean that does this upon creation and deletion:
@ApplicationScoped
@RequiredArgsConstructor
@Slf4j
public class NatsConnectionFactory {
private final AuditNatsConfig auditNatsConfig;
@Getter
private Connection natsConnection;
@PostConstruct
void init() {
try {
if(auditNatsConfig.enabled()) {
natsConnection = Nats.connect(auditNatsConfig.url());
log.info("CONNECTED TO NATS SUCCESSFULLY AT {}",
natsConnection.getConnectedUrl());
}
} catch (Exception ex) {
log.error("COULD NOT DISCONNECT FROM NATS AT {}",
auditNatsConfig.url());
ex.printStackTrace();
}
}
@PreDestroy
void cleanup() throws InterruptedException {
if(natsConnection == null) { return; }
final Connection.Status status =
natsConnection.getStatus();
if(status == CONNECTED) {
natsConnection.close();
log.info("NATS CONNECTION TO {} " +
"CLOSED SUCCESSFULLY.",auditNatsConfig.url());
}
}
}
Can that be attained?
I have tried disabling testcontainers and devservices and deleting my config regarding NATS but no luck so far.