private String createSession() { final ImmutableSession session = ImmutableSession.builder() .name(name) .ttl(Optional.fromNullable(ttl)) .build(); return consul.sessionClient().createSession(session).getId(); }
private void destroySession() { consul.sessionClient().destroySession(sessionId); sessionId = null; }
public Optional<SessionInfo> renewSession(final String sessionId) { return renewSession(null, sessionId); }
/** * Lists all sessions. * * GET /v1/session/list * * @return A list of available sessions. */ public List<SessionInfo> listSessions() { return listSessions(null); }
/** * Retrieves session info. * * GET /v1/session/info/{sessionId} * * @param sessionId * @return {@link SessionInfo}. */ public Optional<SessionInfo> getSessionInfo(final String sessionId) { return getSessionInfo(sessionId, null); }
/** * Renews a session. * * @param dc The datacenter. * @param sessionId The session ID to renew. * @return The {@link SessionInfo} object for the renewed session. */ public Optional<SessionInfo> renewSession(final String dc, final String sessionId) { List<SessionInfo> sessionInfo = http.extract(api.renewSession(sessionId, ImmutableMap.of(), dcQuery(dc))); return sessionInfo == null || sessionInfo.isEmpty() ? Optional.empty() : Optional.of(sessionInfo.get(0)); }
CatalogClient catalogClient = new CatalogClient(retrofit, config, eventCallback); StatusClient statusClient = new StatusClient(retrofit, config, eventCallback); SessionClient sessionClient = new SessionClient(retrofit, config, eventCallback); EventClient eventClient = new EventClient(retrofit, config, eventCallback); PreparedQueryClient preparedQueryClient = new PreparedQueryClient(retrofit, config, eventCallback);
@Override public void run() { if (isRunAllowed()) { // Refresh session sessionClient.renewSession(sessionId); keyValueClient.getValue( servicePath, QueryOptions.blockSeconds(ttl / 3, index.get()).build(), this ); } } }
@Override public List<CamelClusterMember> getMembers() { if (sessionClient == null) { return Collections.emptyList(); } return sessionClient.listSessions().stream() .filter(i -> i.getName().equals(getNamespace())) .map(ConsulClusterMember::new) .collect(Collectors.toList()); }
private boolean acquireLock() { synchronized (sessionId) { String sid = sessionId.get(); return (sid != null) ? sessionClient.getSessionInfo(sid).map(si -> keyValueClient.acquireLock(path, sid)).orElse(Boolean.FALSE) : false; } }
/** * Retrieves session info. * * GET /v1/session/info/{sessionId} * * @param sessionId * @param dc Data center * @return {@link SessionInfo}. */ public Optional<SessionInfo> getSessionInfo(final String sessionId, final String dc) { List<SessionInfo> sessionInfo = http.extract(api.getSessionInfo(sessionId, dcQuery(dc))); return sessionInfo == null || sessionInfo.isEmpty() ? Optional.empty() : Optional.of(sessionInfo.get(0)); }
/** * Create Session. * * PUT /v1/session/create * * @param value The session to create. * @return ID of the newly created session . */ public SessionCreatedResponse createSession(final Session value) { return createSession(value, null); }
/** * Destroys a session. * * PUT /v1/session/destroy/{sessionId} * * @param sessionId The session ID to destroy. */ public void destroySession(final String sessionId) { destroySession(sessionId, null); }
public void watch() { if (sessionId.get() == null) { return; } if (isStarting() || isStarted()) { // Watch for changes keyValueClient.getValue( path, QueryOptions.blockSeconds(configuration.getSessionRefreshInterval(), index.get()).build(), this ); if (sessionId.get() != null) { // Refresh session sessionClient.renewSession(sessionId.get()); } } } }
@InvokeOnHeader(ConsulSessionActions.LIST) protected void list(Message message) throws Exception { setBodyAndResult( message, getClient().listSessions( message.getHeader(ConsulConstants.CONSUL_DATACENTER, String.class) ) ); }
@InvokeOnHeader(ConsulSessionActions.INFO) protected void info(Message message) throws Exception { String sessionId = message.getHeader(ConsulConstants.CONSUL_SESSION, String.class); if (ObjectHelper.isEmpty(sessionId)) { setBodyAndResult( message, getClient().getSessionInfo( message.getMandatoryBody(String.class), message.getHeader(ConsulConstants.CONSUL_DATACENTER, String.class) ).orElse(null) ); } else { setBodyAndResult( message, getClient().getSessionInfo( sessionId, message.getHeader(ConsulConstants.CONSUL_DATACENTER, String.class) ).orElse(null) ); } }
/** * Create Session. * * PUT /v1/session/create * * @param value The session to create. * @param dc The data center. * @return Response containing the session ID. */ public SessionCreatedResponse createSession(final Session value, final String dc) { return http.extract(api.createSession(value, dcQuery(dc))); }
@InvokeOnHeader(ConsulSessionActions.CREATE) protected void create(Message message) throws Exception { setBodyAndResult( message, getClient().createSession( message.getMandatoryBody(Session.class), message.getHeader(ConsulConstants.CONSUL_DATACENTER, String.class) ) ); }
@Override protected void doStop() throws Exception { if (sessionId.get() != null) { if (keyValueClient.releaseLock(this.path, sessionId.get())) { LOGGER.debug("Successfully released lock on path '{}' with id '{}'", path, sessionId.get()); } synchronized (sessionId) { sessionClient.destroySession(sessionId.getAndSet(null)); localMember.setMaster(false); } } }
@InvokeOnHeader(ConsulSessionActions.RENEW) protected void renew(Message message) throws Exception { String sessionId = message.getHeader(ConsulConstants.CONSUL_SESSION, String.class); if (ObjectHelper.isEmpty(sessionId)) { setBodyAndResult( message, getClient().renewSession( message.getHeader(ConsulConstants.CONSUL_DATACENTER, String.class), message.getMandatoryBody(String.class) ) ); } else { setBodyAndResult( message, getClient().renewSession( message.getHeader(ConsulConstants.CONSUL_DATACENTER, String.class), sessionId ) ); } } }