-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
support lazy, fail-fast and background initialization of asyncapi (#297)
* support lazy, fail-fast and background initialization of asyncapi * optimize imports. * asyncapi creation fix order of invocation of channelservice and schemasservice. * spotless format fixes * fix SpringwolfAmqpProducerConfigurationIntegrationTest * Apply suggestions from code review Co-authored-by: Timon Back <[email protected]> * rename LoadingMode into InitMode. InitModes knows only to modes 'background' and 'fail_fast'. Moved Initialization completley into AsyncApiInitApplicationListener. Fixed some tests. Took 47 minutes * feat(core): Ensure initMode=lazy stops application start --------- Co-authored-by: Timon Back <[email protected]> Co-authored-by: Timon Back <[email protected]>
- Loading branch information
1 parent
7158bd5
commit 06069cb
Showing
17 changed files
with
431 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 67 additions & 21 deletions
88
...f-core/src/main/java/io/github/stavshamir/springwolf/asyncapi/DefaultAsyncApiService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,100 @@ | ||
package io.github.stavshamir.springwolf.asyncapi; | ||
|
||
import com.asyncapi.v2._6_0.model.channel.ChannelItem; | ||
import io.github.stavshamir.springwolf.asyncapi.types.AsyncAPI; | ||
import io.github.stavshamir.springwolf.asyncapi.types.Components; | ||
import io.github.stavshamir.springwolf.configuration.AsyncApiDocket; | ||
import io.github.stavshamir.springwolf.configuration.AsyncApiDocketService; | ||
import io.github.stavshamir.springwolf.schemas.SchemasService; | ||
import jakarta.annotation.PostConstruct; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
@Slf4j | ||
@Service | ||
@RequiredArgsConstructor | ||
public class DefaultAsyncApiService implements AsyncApiService { | ||
|
||
/** | ||
* Record holding the result of AsyncAPI creation. | ||
* | ||
* @param asyncAPI | ||
* @param exception | ||
*/ | ||
private record AsyncAPIResult(AsyncAPI asyncAPI, Throwable exception) {} | ||
|
||
private final AsyncApiDocketService asyncApiDocketService; | ||
private final ChannelsService channelsService; | ||
private final SchemasService schemasService; | ||
private final List<AsyncApiCustomizer> customizers; | ||
|
||
private AsyncAPI asyncAPI; | ||
private volatile AsyncAPIResult asyncAPIResult = null; | ||
|
||
@PostConstruct | ||
void buildAsyncApi() { | ||
log.debug("Building AsyncAPI document"); | ||
@Override | ||
public AsyncAPI getAsyncAPI() { | ||
if (isNotInitialized()) { | ||
initAsyncAPI(); | ||
} | ||
|
||
AsyncApiDocket docket = asyncApiDocketService.getAsyncApiDocket(); | ||
if (asyncAPIResult.asyncAPI != null) { | ||
return asyncAPIResult.asyncAPI; | ||
} else { | ||
throw new RuntimeException("Error occured during creation of AsyncAPI", asyncAPIResult.exception); | ||
} | ||
} | ||
|
||
Components components = | ||
Components.builder().schemas(schemasService.getDefinitions()).build(); | ||
/** | ||
* Does the 'heavy work' of building the AsyncAPI documents once. Stores the resulting | ||
* AsyncAPI document or alternativly a catched exception/error in the instance variable asyncAPIResult. | ||
* | ||
* @return | ||
*/ | ||
protected synchronized void initAsyncAPI() { | ||
if (this.asyncAPIResult != null) { | ||
return; // Double Check Idiom | ||
} | ||
|
||
asyncAPI = AsyncAPI.builder() | ||
.info(docket.getInfo()) | ||
.id(docket.getId()) | ||
.defaultContentType(docket.getDefaultContentType()) | ||
.servers(docket.getServers()) | ||
.channels(channelsService.getChannels()) | ||
.components(components) | ||
.build(); | ||
try { | ||
log.debug("Building AsyncAPI document"); | ||
|
||
for (AsyncApiCustomizer customizer : customizers) { | ||
customizer.customize(asyncAPI); | ||
AsyncApiDocket docket = asyncApiDocketService.getAsyncApiDocket(); | ||
|
||
// ChannelsService must be invoked before accessing SchemasService, | ||
// because during channel scanning, all detected schemas are registered with | ||
// SchemasService. | ||
Map<String, ChannelItem> channels = channelsService.findChannels(); | ||
|
||
Components components = Components.builder() | ||
.schemas(schemasService.getDefinitions()) | ||
.build(); | ||
|
||
AsyncAPI asyncAPI = AsyncAPI.builder() | ||
.info(docket.getInfo()) | ||
.id(docket.getId()) | ||
.defaultContentType(docket.getDefaultContentType()) | ||
.servers(docket.getServers()) | ||
.channels(channels) | ||
.components(components) | ||
.build(); | ||
|
||
for (AsyncApiCustomizer customizer : customizers) { | ||
customizer.customize(asyncAPI); | ||
} | ||
this.asyncAPIResult = new AsyncAPIResult(asyncAPI, null); | ||
} catch (Throwable t) { | ||
this.asyncAPIResult = new AsyncAPIResult(null, t); | ||
} | ||
} | ||
|
||
@Override | ||
public AsyncAPI getAsyncAPI() { | ||
return asyncAPI; | ||
/** | ||
* checks whether asyncApi has internally allready been initialized or not. | ||
* | ||
* @return true if asyncApi has not allready been created and initialized. | ||
*/ | ||
public boolean isNotInitialized() { | ||
return this.asyncAPIResult == null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
...main/java/io/github/stavshamir/springwolf/asyncapi/SpringWolfInitApplicationListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package io.github.stavshamir.springwolf.asyncapi; | ||
|
||
import io.github.stavshamir.springwolf.configuration.properties.SpringWolfConfigProperties; | ||
import io.github.stavshamir.springwolf.configuration.properties.SpringWolfConfigProperties.InitMode; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.beans.factory.InitializingBean; | ||
import org.springframework.boot.context.event.ApplicationReadyEvent; | ||
import org.springframework.context.ApplicationListener; | ||
import org.springframework.core.task.TaskExecutor; | ||
import org.springframework.stereotype.Service; | ||
|
||
/** | ||
* Spring ApplicationListener listening on {@link ApplicationReadyEvent}. Triggers the AsyncAPI creation. | ||
* If loadingMode equals {@link InitMode#BACKGROUND} AsyncAPI creation is triggered asynchronously using the | ||
* provided Spring TaskExecutor. | ||
*/ | ||
@Slf4j | ||
@RequiredArgsConstructor | ||
@Service | ||
public class SpringWolfInitApplicationListener implements ApplicationListener<ApplicationReadyEvent>, InitializingBean { | ||
|
||
private final TaskExecutor taskExecutor; | ||
private final AsyncApiService asyncApiService; | ||
private final SpringWolfConfigProperties configProperties; | ||
|
||
@Override | ||
public void onApplicationEvent(ApplicationReadyEvent event) { | ||
if (configProperties.getInitMode() == InitMode.BACKGROUND) { | ||
log.debug("triggering background asyncapi creation.."); | ||
taskExecutor.execute(this.asyncApiService::getAsyncAPI); | ||
} | ||
} | ||
|
||
@Override | ||
public void afterPropertiesSet() { | ||
if (configProperties.getInitMode() == InitMode.FAIL_FAST) { | ||
log.debug("triggering asyncapi creation.."); | ||
this.asyncApiService.getAsyncAPI(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.