use of io.github.microcks.event.ServiceViewChangeEvent in project microcks by microcks.
the class ServiceChangeEventPublisher method onApplicationEvent.
@Override
@Async
public void onApplicationEvent(ServiceChangeEvent event) {
log.debug("Received a ServiceChangeEvent on " + event.getServiceId());
ServiceView serviceView = null;
if (event.getChangeType() != ChangeType.DELETED) {
Service service = serviceRepository.findById(event.getServiceId()).orElse(null);
if (service != null) {
// Put messages into a map where key is operation name.
Map<String, List<? extends Exchange>> messagesMap = new HashMap<>();
for (Operation operation : service.getOperations()) {
if (service.getType() == ServiceType.EVENT) {
// If an event, we should explicitly retrieve event messages.
List<UnidirectionalEvent> events = messageService.getEventByOperation(IdBuilder.buildOperationId(service, operation));
messagesMap.put(operation.getName(), events);
} else {
// Otherwise we have traditional request / response pairs.
List<RequestResponsePair> pairs = messageService.getRequestResponseByOperation(IdBuilder.buildOperationId(service, operation));
messagesMap.put(operation.getName(), pairs);
}
}
serviceView = new ServiceView(service, messagesMap);
}
}
// Build and send a ServiceViewChangeEvent that wraps ServiceView.
ServiceViewChangeEvent serviceViewChangeEvent = new ServiceViewChangeEvent(event.getServiceId(), serviceView, event.getChangeType(), System.currentTimeMillis());
kafkaTemplate.send("microcks-services-updates", event.getServiceId(), serviceViewChangeEvent);
log.debug("Processing of ServiceChangeEvent done !");
}
use of io.github.microcks.event.ServiceViewChangeEvent in project microcks by microcks.
the class AsyncMockDefinitionUpdater method onServiceUpdate.
@Incoming("microcks-services-updates")
public void onServiceUpdate(ServiceViewChangeEvent serviceViewChangeEvent) {
logger.info("Received a new change event [" + serviceViewChangeEvent.getChangeType() + "] for '" + serviceViewChangeEvent.getServiceId() + "', at " + serviceViewChangeEvent.getTimestamp());
// Remove existing definitions or add/update existing for EVENT services.
if (serviceViewChangeEvent.getChangeType().equals(ChangeType.DELETED)) {
logger.info("Removing mock definitions for " + serviceViewChangeEvent.getServiceId());
mockRepository.removeMockDefinitions(serviceViewChangeEvent.getServiceId());
schemaRegistry.clearRegistryForService(serviceViewChangeEvent.getServiceId());
} else {
// Only deal with service of type EVENT...
if (serviceViewChangeEvent.getServiceView() != null && serviceViewChangeEvent.getServiceView().getService().getType().equals(ServiceType.EVENT)) {
// Browse and check operation regarding restricted frequencies and supported bindings.
boolean scheduled = false;
for (Operation operation : serviceViewChangeEvent.getServiceView().getService().getOperations()) {
if (Arrays.asList(restrictedFrequencies).contains(operation.getDefaultDelay()) && operation.getBindings().keySet().stream().anyMatch(Arrays.asList(supportedBindings)::contains)) {
logger.info("Found '" + operation.getName() + "' as a candidate for async message mocking");
// Build an Async mock definition and store it into repository.
AsyncMockDefinition mockDefinition = new AsyncMockDefinition(serviceViewChangeEvent.getServiceView().getService(), operation, serviceViewChangeEvent.getServiceView().getMessagesMap().get(operation.getName()).stream().filter(e -> e instanceof UnidirectionalEvent).map(e -> ((UnidirectionalEvent) e).getEventMessage()).collect(Collectors.toList()));
mockRepository.storeMockDefinition(mockDefinition);
schemaRegistry.updateRegistryForService(mockDefinition.getOwnerService());
scheduled = true;
}
}
if (!scheduled) {
logger.info("Ensure to un-schedule " + serviceViewChangeEvent.getServiceId() + " on this minion. Removing definitions.");
mockRepository.removeMockDefinitions(serviceViewChangeEvent.getServiceId());
schemaRegistry.clearRegistryForService(serviceViewChangeEvent.getServiceId());
}
}
}
}
Aggregations