use of org.openremote.container.persistence.PersistenceService.PERSISTENCE_TOPIC in project openremote by openremote.
the class MqttBrokerService method configure.
@SuppressWarnings("unchecked")
@Override
public void configure() throws Exception {
from(PERSISTENCE_TOPIC).routeId("UserPersistenceChanges").filter(isPersistenceEventForEntityType(User.class)).process(exchange -> {
PersistenceEvent<User> persistenceEvent = (PersistenceEvent<User>) exchange.getIn().getBody(PersistenceEvent.class);
User user = persistenceEvent.getEntity();
if (!user.isServiceAccount()) {
return;
}
boolean forceDisconnect = persistenceEvent.getCause() == PersistenceEvent.Cause.DELETE;
if (persistenceEvent.getCause() == PersistenceEvent.Cause.UPDATE) {
// Force disconnect if certain properties have changed
forceDisconnect = Arrays.stream(persistenceEvent.getPropertyNames()).anyMatch((propertyName) -> (propertyName.equals("enabled") && !user.getEnabled()) || propertyName.equals("username"));
}
if (forceDisconnect) {
// Find existing connection for this user
Arrays.stream(getConnections()).filter(connection -> user.getUsername().equals(connection.getUsername())).findFirst().ifPresent(connection -> {
LOG.info("User modified or deleted so forcing connected client to disconnect: connection=" + connection);
forceDisconnect(connection.getClientId());
});
}
});
}
Aggregations