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

The following examples show how to use com.ecwid.consul.v1.session.model.NewSession. 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: TxleConsulClient.java    From txle with Apache License 2.0 6 votes vote down vote up
/**
 * Multiple txle apps register the same key 'CONSUL_LEADER_KEY', it would be leader in case of getting 'true'.
 * The Session, Checks and Services have to be destroyed/deregistered before shutting down JVM, so that the lock of leader key could be released.
 *
 * @return String session id
 */
public String registerConsulSession() {
    String serverHost = "127.0.0.1";
    try {
        // Firstly, to set an available ConsulClient before registering session.
        this.setAvailableConsulClient();
        if (consulClient != null) {
            destroyConsulCriticalServices();
            // To create a key for leader election no matter if it is exists.
            consulClient.setKVValue(CONSUL_LEADER_KEY, CONSUL_LEADER_KEY_VALUE);
            NewSession session = new NewSession();
            serverHost = CrossSystemInetAddress.readCrossSystemIPv4();
            session.setName("session-" + serverName + "-" + serverHost + "-" + serverPort + "-" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
            consulSessionId = consulClient.sessionCreate(session, null).getValue();
            return consulSessionId;
        }
    } catch (Exception e) {
        log.error("Failed to register Consul Session, serverName [{}], serverHost [{}], serverPort [{}].", serverName, serverHost, serverPort, e);
    }
    return consulSessionId;
}
 
Example #2
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 #3
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 #4
Source File: SessionConsulClient.java    From consul-api with Apache License 2.0 5 votes vote down vote up
@Override
public Response<String> sessionCreate(NewSession newSession, QueryParams queryParams, String token) {
	UrlParameters tokenParam = token != null ? new SingleUrlParameters("token", token) : null;

	String json = GsonFactory.getGson().toJson(newSession);
	HttpResponse httpResponse = rawClient.makePutRequest("/v1/session/create", json, queryParams, tokenParam);

	if (httpResponse.getStatusCode() == 200) {
		Map<String, String> value = GsonFactory.getGson().fromJson(httpResponse.getContent(), new TypeToken<Map<String, String>>() {
		}.getType());
		return new Response<String>(value.get("ID"), httpResponse);
	} else {
		throw new OperationException(httpResponse);
	}
}
 
Example #5
Source File: SessionConsulClient.java    From consul-api with Apache License 2.0 4 votes vote down vote up
@Override
public Response<String> sessionCreate(NewSession newSession, QueryParams queryParams) {
	return sessionCreate(newSession, queryParams, null);
}
 
Example #6
Source File: ConsulClient.java    From consul-api with Apache License 2.0 4 votes vote down vote up
@Override
public Response<String> sessionCreate(NewSession newSession, QueryParams queryParams) {
	return sessionClient.sessionCreate(newSession, queryParams);
}
 
Example #7
Source File: ConsulClient.java    From consul-api with Apache License 2.0 4 votes vote down vote up
@Override
public Response<String> sessionCreate(NewSession newSession, QueryParams queryParams, String token) {
	return sessionClient.sessionCreate(newSession, queryParams, token);
}
 
Example #8
Source File: SessionClient.java    From consul-api with Apache License 2.0 votes vote down vote up
public Response<String> sessionCreate(NewSession newSession, QueryParams queryParams); 
Example #9
Source File: SessionClient.java    From consul-api with Apache License 2.0 votes vote down vote up
public Response<String> sessionCreate(NewSession newSession, QueryParams queryParams, String token);