Java Code Examples for com.ecwid.consul.v1.session.model.NewSession#setName()
The following examples show how to use
com.ecwid.consul.v1.session.model.NewSession#setName() .
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 |
/** * 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 |
/** * 创建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 |
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; }