Search in sources :

Example 1 with SessionRegistry

use of io.moquette.broker.SessionRegistry in project openremote by openremote.

the class MqttBrokerService method getSession.

Object getSession(String clientId) {
    if (mqttBroker == null) {
        return null;
    }
    try {
        SessionRegistry sessionRegistry = getSessionRegistry();
        Method retrieveSessionMethod = SessionRegistry.class.getDeclaredMethod("retrieve", String.class);
        retrieveSessionMethod.setAccessible(true);
        return retrieveSessionMethod.invoke(sessionRegistry, clientId);
    } catch (Exception e) {
        LOG.log(Level.WARNING, "Failed to get Moquette session using reflection", e);
    }
    return null;
}
Also used : SessionRegistry(io.moquette.broker.SessionRegistry) Method(java.lang.reflect.Method)

Example 2 with SessionRegistry

use of io.moquette.broker.SessionRegistry in project openremote by openremote.

the class MqttBrokerService method clearConnectionSession.

/**
 * We have this as there's no way to configure expiry time of sessions in Moquette so to make things simple we
 * clear the session whenever the client disconnects; the client can then re-subscribe as needed
 */
public MqttConnection clearConnectionSession(String clientId) {
    if (mqttBroker == null) {
        return null;
    }
    synchronized (clientIdConnectionMap) {
        MqttConnection connection = clientIdConnectionMap.remove(clientId);
        if (connection != null && !connection.isCleanSession()) {
            try {
                SessionRegistry sessionRegistry = getSessionRegistry();
                Object session = getSession(clientId);
                if (sessionRegistry == null || session == null) {
                    throw new IllegalStateException("Couldn't get session registry and/or session for client: " + clientId);
                }
                Method removeMethod = sessionRegistry.getClass().getDeclaredMethod("remove", session.getClass());
                removeMethod.setAccessible(true);
                removeMethod.invoke(sessionRegistry, session);
            } catch (Exception e) {
                LOG.log(Level.WARNING, "Failed to remove Moquette session using reflection", e);
            }
        }
        return connection;
    }
}
Also used : SessionRegistry(io.moquette.broker.SessionRegistry) Method(java.lang.reflect.Method)

Aggregations

SessionRegistry (io.moquette.broker.SessionRegistry)2 Method (java.lang.reflect.Method)2