use of com.orbitz.consul.model.session.SessionCreatedResponse 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);
}
use of com.orbitz.consul.model.session.SessionCreatedResponse 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);
}
Aggregations