use of io.gravitee.repository.management.model.Event in project gravitee-management-rest-api by gravitee-io.
the class SyncManager method getLastEvent.
private Event getLastEvent(String api) {
EventCriteria eventCriteria;
if (lastRefreshAt == -1) {
eventCriteria = new EventCriteria.Builder().property(Event.EventProperties.API_ID.getValue(), api).build();
} else {
eventCriteria = new EventCriteria.Builder().property(Event.EventProperties.API_ID.getValue(), api).from(lastRefreshAt).to(System.currentTimeMillis()).build();
}
List<Event> events = eventRepository.search(eventCriteria, new PageableBuilder().pageNumber(0).pageSize(1).build()).getContent();
return (!events.isEmpty()) ? events.get(0) : null;
}
use of io.gravitee.repository.management.model.Event in project gravitee-management-rest-api by gravitee-io.
the class EventServiceImpl method findById.
@Override
public EventEntity findById(String id) {
try {
LOGGER.debug("Find event by ID: {}", id);
Optional<Event> event = eventRepository.findById(id);
if (event.isPresent()) {
return convert(event.get());
}
throw new EventNotFoundException(id);
} catch (TechnicalException ex) {
LOGGER.error("An error occurs while trying to find an event using its ID {}", id, ex);
throw new TechnicalManagementException("An error occurs while trying to find an event using its ID " + id, ex);
}
}
use of io.gravitee.repository.management.model.Event in project gravitee-management-rest-api by gravitee-io.
the class EventServiceImpl method create.
@Override
public EventEntity create(NewEventEntity newEventEntity) {
String hostAddress = "";
try {
hostAddress = InetAddress.getLocalHost().getHostAddress();
LOGGER.debug("Create {} for server {}", newEventEntity, hostAddress);
Event event = convert(newEventEntity);
event.setId(io.gravitee.common.utils.UUID.random().toString());
// Set origin
event.getProperties().put(Event.EventProperties.ORIGIN.getValue(), hostAddress);
// Set date fields
event.setCreatedAt(new Date());
event.setUpdatedAt(event.getCreatedAt());
Event createdEvent = eventRepository.create(event);
return convert(createdEvent);
} catch (UnknownHostException e) {
LOGGER.error("An error occurs while getting the server IP address", e);
throw new TechnicalManagementException("An error occurs while getting the server IP address", e);
} catch (TechnicalException ex) {
LOGGER.error("An error occurs while trying to create {} for server {}", newEventEntity, hostAddress, ex);
throw new TechnicalManagementException("An error occurs while trying create " + newEventEntity + " for server " + hostAddress, ex);
}
}
use of io.gravitee.repository.management.model.Event in project gravitee-gateway by gravitee-io.
the class MonitoringService method doStart.
@Override
protected void doStart() throws Exception {
if (enabled) {
super.doStart();
LOGGER.info("Start gateway monitor");
Event evt = prepareEvent();
LOGGER.debug("Sending a {} event", evt.getType());
heartbeatEvent = eventRepository.create(evt);
executorService = Executors.newSingleThreadScheduledExecutor(r -> new Thread(r, "gateway-monitor"));
MonitorThread monitorThread = new MonitorThread(heartbeatEvent);
this.applicationContext.getAutowireCapableBeanFactory().autowireBean(monitorThread);
LOGGER.info("Monitoring scheduled with fixed delay {} {} ", delay, unit.name());
((ScheduledExecutorService) executorService).scheduleWithFixedDelay(monitorThread, 0, delay, unit);
LOGGER.info("Associate a new HTTP handler on {}", PATH);
// Create and associate handler
MonitorHandler monitorHandler = new MonitorHandler();
applicationContext.getAutowireCapableBeanFactory().autowireBean(monitorHandler);
router.get(PATH).produces(MediaType.APPLICATION_JSON).handler(monitorHandler);
LOGGER.info("Start gateway monitor : DONE");
}
}
use of io.gravitee.repository.management.model.Event in project gravitee-gateway by gravitee-io.
the class MonitoringService method prepareEvent.
private Event prepareEvent() {
Event event = new Event();
event.setId(UUID.random().toString());
event.setType(EventType.GATEWAY_STARTED);
event.setCreatedAt(new Date());
event.setUpdatedAt(event.getCreatedAt());
Map<String, String> properties = new HashMap<>();
properties.put("id", node.id());
properties.put("started_at", Long.toString(event.getCreatedAt().getTime()));
properties.put("last_heartbeat_at", Long.toString(event.getCreatedAt().getTime()));
event.setProperties(properties);
InstanceEventPayload instance = createInstanceInfo();
try {
String payload = objectMapper.writeValueAsString(instance);
event.setPayload(payload);
} catch (JsonProcessingException jsex) {
LOGGER.error("An error occurs while transforming instance information into JSON", jsex);
}
return event;
}
Aggregations