Search in sources :

Example 1 with SessionClient

use of com.orbitz.consul.SessionClient in project camel by apache.

the class ConsulRegistry method remove.

public void remove(String key) {
    // create session to avoid conflicts (not sure if that is safe enough)
    SessionClient sessionClient = consul.sessionClient();
    String sessionName = "session_" + UUID.randomUUID().toString();
    SessionCreatedResponse response = sessionClient.createSession(ImmutableSession.builder().name(sessionName).build());
    String sessionId = response.getId();
    kvClient = consul.keyValueClient();
    String lockKey = "lock_" + key;
    kvClient.acquireLock(lockKey, sessionName, sessionId);
    Object object = lookupByName(key);
    if (object == null) {
        String msg = "Bean with key '" + key + "' did not exist in Consul Registry.";
        throw new NoSuchBeanException(msg);
    }
    kvClient.deleteKey(key);
    kvClient.deleteKey(object.getClass().getName() + "/" + key);
    kvClient.releaseLock(lockKey, sessionId);
}
Also used : SessionClient(com.orbitz.consul.SessionClient) NoSuchBeanException(org.apache.camel.NoSuchBeanException) SessionCreatedResponse(com.orbitz.consul.model.session.SessionCreatedResponse)

Example 2 with SessionClient

use of com.orbitz.consul.SessionClient in project camel by apache.

the class ConsulRegistry method put.

public void put(String key, Object object) {
    // Substitute $ character in key
    key = key.replaceAll("\\$", "/");
    // create session to avoid conflicts
    // (not sure if that is safe enough, again)
    SessionClient sessionClient = consul.sessionClient();
    String sessionName = "session_" + UUID.randomUUID().toString();
    SessionCreatedResponse response = sessionClient.createSession(ImmutableSession.builder().name(sessionName).build());
    String sessionId = response.getId();
    kvClient = consul.keyValueClient();
    String lockKey = "lock_" + key;
    kvClient.acquireLock(lockKey, sessionName, sessionId);
    // Allow only unique keys, last one wins
    if (lookupByName(key) != null) {
        remove(key);
    }
    Object clone = SerializationUtils.clone((Serializable) object);
    byte[] serializedObject = SerializationUtils.serialize((Serializable) clone);
    // pre-encode due native encoding issues
    byte[] preEncodedValue = Base64.encodeBase64(serializedObject);
    String value = new String(preEncodedValue);
    // store the actual class
    kvClient.putValue(key, value);
    // store just as a bookmark
    kvClient.putValue(object.getClass().getName().replaceAll("\\$", "/") + "/" + key, "1");
    kvClient.releaseLock(lockKey, sessionId);
}
Also used : SessionClient(com.orbitz.consul.SessionClient) SessionCreatedResponse(com.orbitz.consul.model.session.SessionCreatedResponse)

Aggregations

SessionClient (com.orbitz.consul.SessionClient)2 SessionCreatedResponse (com.orbitz.consul.model.session.SessionCreatedResponse)2 NoSuchBeanException (org.apache.camel.NoSuchBeanException)1