use of io.micronaut.scheduling.annotation.Async in project quick by bakdata.
the class TopicRegistryInitializer method onStartUp.
/**
* Ensures the topic registry itself and its topic are created.
*/
@EventListener
@Async
public void onStartUp(final StartupEvent event) {
// the topic registry is a basically just a mirror application
// therefore, it needs its own kafka topic
final Properties properties = new Properties();
properties.put(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG, this.kafkaConfig.getBootstrapServer());
boolean registryExists = false;
try (final AdminClient admin = AdminClient.create(properties)) {
final NewTopic immutableTopic = this.topicRegistryConfig.toNewKafkaTopic();
admin.createTopics(List.of(immutableTopic));
} catch (final TopicExistsException ignored) {
log.info("Internal registry topic already exists");
registryExists = true;
} catch (final KafkaException e) {
throw new InternalErrorException("Kafka could not be reached: " + e.getMessage());
}
// register the avro schema of the topic data class with the schema registry
try {
final String subject = VALUE.asSubject(this.topicRegistryConfig.getTopicName());
final Schema topicDataSchema = AvroTopicData.getClassSchema();
this.schemaRegistryClient.register(subject, topicDataSchema);
} catch (final IOException | RestClientException exception) {
if (!registryExists) {
throw new InternalErrorException("Could not register schema for internal topic registry topic");
}
}
final MirrorCreationData topicRegistryCreationData = new MirrorCreationData(this.topicRegistryConfig.getServiceName(), this.topicRegistryConfig.getTopicName(), 1, // null means we use the default tag
null, null);
// create topic-registry mirror
// no retention time
this.mirrorService.createInternalMirror(topicRegistryCreationData).subscribe(() -> log.info("Deployed internal topic-registry service"), e -> log.info("Could not deploy internal topic-registry service: {}", e.getMessage())).dispose();
}
use of io.micronaut.scheduling.annotation.Async in project micronaut-core by micronaut-projects.
the class AsyncInterceptor method intercept.
@Nullable
@Override
public Object intercept(MethodInvocationContext<Object, Object> context) {
String executorServiceName = context.stringValue(Async.class).orElse(TaskExecutors.SCHEDULED);
ExecutorService executorService;
if (TaskExecutors.SCHEDULED.equals(executorServiceName) && scheduledExecutorService.isPresent()) {
executorService = scheduledExecutorService.get().get();
} else {
executorService = scheduledExecutorServices.computeIfAbsent(executorServiceName, name -> beanLocator.findBean(ExecutorService.class, Qualifiers.byName(name)).orElseThrow(() -> new TaskExecutionException("No ExecutorService named [" + name + "] configured in application context")));
}
InterceptedMethod interceptedMethod = InterceptedMethod.of(context);
try {
switch(interceptedMethod.resultType()) {
case PUBLISHER:
return interceptedMethod.handleResult(interceptedMethod.interceptResultAsPublisher(executorService));
case COMPLETION_STAGE:
return interceptedMethod.handleResult(CompletableFuture.supplyAsync(() -> interceptedMethod.interceptResultAsCompletionStage(), executorService).thenCompose(Function.identity()));
case SYNCHRONOUS:
ReturnType<Object> rt = context.getReturnType();
Class<?> returnType = rt.getType();
if (void.class == returnType) {
executorService.submit(() -> {
try {
context.proceed();
} catch (Throwable e) {
if (LOG.isErrorEnabled()) {
LOG.error("Error occurred executing @Async method [" + context.getExecutableMethod() + "]: " + e.getMessage(), e);
}
}
});
return null;
}
throw new TaskExecutionException("Method [" + context.getExecutableMethod() + "] must return either void, or an instance of Publisher or CompletionStage");
default:
return interceptedMethod.unsupported();
}
} catch (Exception e) {
return interceptedMethod.handleException(e);
}
}
use of io.micronaut.scheduling.annotation.Async in project micronaut-zeebe-client by camunda-community-hub.
the class ProcessDeployer method onStartupEvent.
@EventListener
@Async
public void onStartupEvent(StartupEvent event) {
DeploymentEvent deployment = zeebeClient.newDeployCommand().addResourceFromClasspath("bpmn/say_hello.bpmn").requestTimeout(Duration.ofSeconds(10)).send().join();
Process process = deployment.getProcesses().get(0);
logger.info("deployed process {} with id {}", process.getResourceName(), process.getProcessDefinitionKey());
processId = process.getBpmnProcessId();
}
Aggregations