com.ecwid.consul.v1.session.model.Session Java Examples

The following examples show how to use com.ecwid.consul.v1.session.model.Session. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: BaseLock.java    From consul-distributed-lock with Apache License 2.0 6 votes vote down vote up
/**
 * 创建session
 * @param sessionName
 * @return
 */
protected String createSession(String sessionName) {
    NewSession newSession = new NewSession();
    newSession.setName(sessionName);
    if(checkTtl != null) {
        checkTtl.start();
        // 如果有CheckTtl,就为该Session设置Check相关信息
        List<String> checks = new ArrayList<>();
        checks.add(checkTtl.getCheckId());
        newSession.setChecks(checks);
        newSession.setBehavior(Session.Behavior.DELETE);
        /** newSession.setTtl("60s");
         指定秒数(10s到86400s之间)。如果提供,在TTL到期之前没有更新,则会话无效。
         应使用最低的实际TTL来保持管理会话的数量。
         当锁被强制过期时,例如在领导选举期间,会话可能无法获得最多双倍TTL,
         因此应避免长TTL值(> 1小时)。**/
    }
    return consulClient.sessionCreate(newSession, null).getValue();
}
 
Example #2
Source File: SessionConsulClient.java    From consul-api with Apache License 2.0 6 votes vote down vote up
@Override
public Response<Session> getSessionInfo(String session, QueryParams queryParams, String token) {
	UrlParameters tokenParam = token != null ? new SingleUrlParameters("token", token) : null;
	HttpResponse httpResponse = rawClient.makeGetRequest("/v1/session/info/" + session, queryParams, tokenParam);

	if (httpResponse.getStatusCode() == 200) {
		List<Session> value = GsonFactory.getGson().fromJson(httpResponse.getContent(), new TypeToken<List<Session>>() {
		}.getType());

		if (value == null || value.isEmpty()) {
			return new Response<Session>(null, httpResponse);
		} else if (value.size() == 1) {
			return new Response<Session>(value.get(0), httpResponse);
		} else {
			throw new ConsulException("Strange response (list size=" + value.size() + ")");
		}
	} else {
		throw new OperationException(httpResponse);
	}
}
 
Example #3
Source File: SessionConsulClient.java    From consul-api with Apache License 2.0 6 votes vote down vote up
@Override
public Response<Session> renewSession(String session, QueryParams queryParams, String token) {
	UrlParameters tokenParam = token != null ? new SingleUrlParameters("token", token) : null;
	HttpResponse httpResponse = rawClient.makePutRequest("/v1/session/renew/" + session, "", queryParams, tokenParam);

	if (httpResponse.getStatusCode() == 200) {
		List<Session> value = GsonFactory.getGson().fromJson(httpResponse.getContent(), new TypeToken<List<Session>>() {
		}.getType());

		if (value.size() == 1) {
			return new Response<Session>(value.get(0), httpResponse);
		} else {
			throw new ConsulException("Strange response (list size=" + value.size() + ")");
		}
	} else {
		throw new OperationException(httpResponse);
	}
}
 
Example #4
Source File: ConsulEphemralNode.java    From saluki with Apache License 2.0 5 votes vote down vote up
public NewSession getNewSession() {
    NewSession newSersson = new NewSession();
    newSersson.setName(getSessionName());
    newSersson.setLockDelay(15);
    newSersson.setBehavior(Session.Behavior.DELETE);
    newSersson.setTtl(this.interval + "s");
    return newSersson;
}
 
Example #5
Source File: TxleConsulClient.java    From txle with Apache License 2.0 5 votes vote down vote up
public void destroyCurrentSession() {
    try {
        Response<Session> session = consulClient.getSessionInfo(consulSessionId, QueryParams.DEFAULT);
        if (session != null) {
            consulClient.sessionDestroy(session.getValue().getId(), QueryParams.DEFAULT);
        }
    } catch (Exception e) {
        log.error("Failed to destroy current session from Consul.", e);
    }
}
 
Example #6
Source File: SessionConsulClient.java    From consul-api with Apache License 2.0 5 votes vote down vote up
@Override
public Response<List<Session>> getSessionNode(String node, QueryParams queryParams, String token) {
	UrlParameters tokenParam = token != null ? new SingleUrlParameters("token", token) : null;
	HttpResponse httpResponse = rawClient.makeGetRequest("/v1/session/node/" + node, queryParams, tokenParam);

	if (httpResponse.getStatusCode() == 200) {
		List<Session> value = GsonFactory.getGson().fromJson(httpResponse.getContent(), new TypeToken<List<Session>>() {
		}.getType());
		return new Response<List<Session>>(value, httpResponse);
	} else {
		throw new OperationException(httpResponse);
	}
}
 
Example #7
Source File: SessionConsulClient.java    From consul-api with Apache License 2.0 5 votes vote down vote up
@Override
public Response<List<Session>> getSessionList(QueryParams queryParams, String token) {
	UrlParameters tokenParam = token != null ? new SingleUrlParameters("token", token) : null;
	HttpResponse httpResponse = rawClient.makeGetRequest("/v1/session/list", queryParams, tokenParam);

	if (httpResponse.getStatusCode() == 200) {
		List<Session> value = GsonFactory.getGson().fromJson(httpResponse.getContent(), new TypeToken<List<Session>>() {
		}.getType());
		return new Response<List<Session>>(value, httpResponse);
	} else {
		throw new OperationException(httpResponse);
	}
}
 
Example #8
Source File: ConsulClient.java    From consul-api with Apache License 2.0 4 votes vote down vote up
@Override
public Response<Session> renewSession(String session, QueryParams queryParams, String token) {
	return sessionClient.renewSession(session, queryParams, token);
}
 
Example #9
Source File: ConsulClient.java    From consul-api with Apache License 2.0 4 votes vote down vote up
@Override
public Response<Session> renewSession(String session, QueryParams queryParams) {
	return sessionClient.renewSession(session, queryParams);
}
 
Example #10
Source File: ConsulClient.java    From consul-api with Apache License 2.0 4 votes vote down vote up
@Override
public Response<List<Session>> getSessionList(QueryParams queryParams, String token) {
	return sessionClient.getSessionList(queryParams, token);
}
 
Example #11
Source File: ConsulClient.java    From consul-api with Apache License 2.0 4 votes vote down vote up
@Override
public Response<List<Session>> getSessionList(QueryParams queryParams) {
	return sessionClient.getSessionList(queryParams);
}
 
Example #12
Source File: ConsulClient.java    From consul-api with Apache License 2.0 4 votes vote down vote up
@Override
public Response<List<Session>> getSessionNode(String node, QueryParams queryParams, String token) {
	return sessionClient.getSessionNode(node, queryParams, token);
}
 
Example #13
Source File: ConsulClient.java    From consul-api with Apache License 2.0 4 votes vote down vote up
@Override
public Response<List<Session>> getSessionNode(String node, QueryParams queryParams) {
	return sessionClient.getSessionNode(node, queryParams);
}
 
Example #14
Source File: ConsulClient.java    From consul-api with Apache License 2.0 4 votes vote down vote up
@Override
public Response<Session> getSessionInfo(String session, QueryParams queryParams, String token) {
	return sessionClient.getSessionInfo(session, queryParams, token);
}
 
Example #15
Source File: ConsulClient.java    From consul-api with Apache License 2.0 4 votes vote down vote up
@Override
public Response<Session> getSessionInfo(String session, QueryParams queryParams) {
	return sessionClient.getSessionInfo(session, queryParams);
}
 
Example #16
Source File: SessionConsulClient.java    From consul-api with Apache License 2.0 4 votes vote down vote up
public Response<Session> renewSession(String session, QueryParams queryParams) {
	return renewSession(session, queryParams, null);
}
 
Example #17
Source File: SessionConsulClient.java    From consul-api with Apache License 2.0 4 votes vote down vote up
@Override
public Response<List<Session>> getSessionList(QueryParams queryParams) {
	return getSessionList(queryParams, null);
}
 
Example #18
Source File: SessionConsulClient.java    From consul-api with Apache License 2.0 4 votes vote down vote up
@Override
public Response<List<Session>> getSessionNode(String node, QueryParams queryParams) {
	return getSessionNode(node, queryParams, null);
}
 
Example #19
Source File: SessionConsulClient.java    From consul-api with Apache License 2.0 4 votes vote down vote up
@Override
public Response<Session> getSessionInfo(String session, QueryParams queryParams) {
	return getSessionInfo(session, queryParams, null);
}
 
Example #20
Source File: SessionClient.java    From consul-api with Apache License 2.0 votes vote down vote up
Response<List<Session>> getSessionList(QueryParams queryParams); 
Example #21
Source File: SessionClient.java    From consul-api with Apache License 2.0 votes vote down vote up
Response<List<Session>> getSessionList(QueryParams queryParams, String token); 
Example #22
Source File: SessionClient.java    From consul-api with Apache License 2.0 votes vote down vote up
Response<Session> renewSession(String session, QueryParams queryParams); 
Example #23
Source File: SessionClient.java    From consul-api with Apache License 2.0 votes vote down vote up
Response<Session> renewSession(String session, QueryParams queryParams, String token); 
Example #24
Source File: SessionClient.java    From consul-api with Apache License 2.0 votes vote down vote up
Response<List<Session>> getSessionNode(String node, QueryParams queryParams, String token); 
Example #25
Source File: SessionClient.java    From consul-api with Apache License 2.0 votes vote down vote up
Response<List<Session>> getSessionNode(String node, QueryParams queryParams); 
Example #26
Source File: SessionClient.java    From consul-api with Apache License 2.0 votes vote down vote up
Response<Session> getSessionInfo(String session, QueryParams queryParams, String token); 
Example #27
Source File: SessionClient.java    From consul-api with Apache License 2.0 votes vote down vote up
Response<Session> getSessionInfo(String session, QueryParams queryParams);