io.etcd.jetcd.Util Java Examples

The following examples show how to use io.etcd.jetcd.Util. 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: Main.java    From jetcd with Apache License 2.0 7 votes vote down vote up
public static void main(String[] args) {
    Args cmd = new Args();

    JCommander.newBuilder().addObject(cmd).build().parse(args);

    CountDownLatch latch = new CountDownLatch(cmd.maxEvents);
    ByteSequence key = ByteSequence.from(cmd.key, StandardCharsets.UTF_8);
    Collection<URI> endpoints = Util.toURIs(cmd.endpoints);

    Watch.Listener listener = Watch.listener(response -> {
        LOGGER.info("Watching for key={}", cmd.key);

        for (WatchEvent event : response.getEvents()) {
            LOGGER.info("type={}, key={}, value={}", event.getEventType(),
                Optional.ofNullable(event.getKeyValue().getKey()).map(bs -> bs.toString(StandardCharsets.UTF_8)).orElse(""),
                Optional.ofNullable(event.getKeyValue().getValue()).map(bs -> bs.toString(StandardCharsets.UTF_8))
                    .orElse(""));
        }

        latch.countDown();
    });

    try (Client client = Client.builder().endpoints(endpoints).build();
        Watch watch = client.getWatchClient();
        Watch.Watcher watcher = watch.watch(key, listener)) {

        latch.await();
    } catch (Exception e) {
        LOGGER.error("Watching Error {}", e);
        System.exit(1);
    }
}
 
Example #2
Source File: EtcdConfig.java    From kkbinlog with Apache License 2.0 5 votes vote down vote up
@Bean
public Client etcdClient() {

    Client client  = Client
            .builder()
            .endpoints(Util.toURIs(endpoints))
            //.authority(authority)
            //.user(ByteSequence.from(username, StandardCharsets.UTF_8))
            //.password(ByteSequence.from(password, StandardCharsets.UTF_8))
            .build();

    return client;
}
 
Example #3
Source File: Op.java    From jetcd with Apache License 2.0 5 votes vote down vote up
RequestOp toRequestOp(ByteSequence namespace) {
    RangeRequest.Builder range = RangeRequest.newBuilder().setKey(Util.prefixNamespace(this.key, namespace))
        .setCountOnly(this.option.isCountOnly()).setLimit(this.option.getLimit())
        .setRevision(this.option.getRevision()).setKeysOnly(this.option.isKeysOnly())
        .setSerializable(this.option.isSerializable())
        .setSortOrder(toRangeRequestSortOrder(this.option.getSortOrder()))
        .setSortTarget(toRangeRequestSortTarget(this.option.getSortField()));

    this.option.getEndKey()
        .map(endKey -> Util.prefixNamespaceToRangeEnd(ByteString.copyFrom(endKey.getBytes()), namespace))
        .ifPresent(range::setRangeEnd);

    return RequestOp.newBuilder().setRequestRange(range).build();
}
 
Example #4
Source File: Op.java    From jetcd with Apache License 2.0 5 votes vote down vote up
RequestOp toRequestOp(ByteSequence namespace) {
    DeleteRangeRequest.Builder delete = DeleteRangeRequest.newBuilder()
        .setKey(Util.prefixNamespace(this.key, namespace)).setPrevKv(this.option.isPrevKV());

    this.option.getEndKey()
        .map(endKey -> Util.prefixNamespaceToRangeEnd(ByteString.copyFrom(endKey.getBytes()), namespace))
        .ifPresent(delete::setRangeEnd);

    return RequestOp.newBuilder().setRequestDeleteRange(delete).build();
}
 
Example #5
Source File: EtcdCenterRepository.java    From shardingsphere with Apache License 2.0 4 votes vote down vote up
@Override
public void init(final CenterConfiguration config) { 
    this.etcdProperties = new EtcdProperties(props);
    client = Client.builder().endpoints(Util.toURIs(Splitter.on(",").trimResults().splitToList(config.getServerLists()))).build();
}
 
Example #6
Source File: Cmp.java    From jetcd with Apache License 2.0 4 votes vote down vote up
Compare toCompare(ByteSequence namespace) {
    Compare.Builder compareBuilder = Compare.newBuilder().setKey(Util.prefixNamespace(this.key, namespace));
    switch (this.op) {
        case EQUAL:
            compareBuilder.setResult(Compare.CompareResult.EQUAL);
            break;
        case GREATER:
            compareBuilder.setResult(Compare.CompareResult.GREATER);
            break;
        case LESS:
            compareBuilder.setResult(Compare.CompareResult.LESS);
            break;
        case NOT_EQUAL:
            compareBuilder.setResult(Compare.CompareResult.NOT_EQUAL);
            break;
        default:
            throw new IllegalArgumentException("Unexpected compare type (" + this.op + ")");
    }

    Compare.CompareTarget target = this.target.getTarget();
    Object value = this.target.getTargetValue();

    compareBuilder.setTarget(target);
    switch (target) {
        case VERSION:
            compareBuilder.setVersion((Long) value);
            break;
        case VALUE:
            compareBuilder.setValue((ByteString) value);
            break;
        case MOD:
            compareBuilder.setModRevision((Long) value);
            break;
        case CREATE:
            compareBuilder.setCreateRevision((Long) value);
            break;
        default:
            throw new IllegalArgumentException("Unexpected target type (" + target + ")");
    }

    return compareBuilder.build();
}
 
Example #7
Source File: Op.java    From jetcd with Apache License 2.0 4 votes vote down vote up
RequestOp toRequestOp(ByteSequence namespace) {
    PutRequest put = PutRequest.newBuilder().setKey(Util.prefixNamespace(this.key, namespace)).setValue(this.value)
        .setLease(this.option.getLeaseId()).setPrevKv(this.option.getPrevKV()).build();

    return RequestOp.newBuilder().setRequestPut(put).build();
}
 
Example #8
Source File: LockResponse.java    From jetcd with Apache License 2.0 4 votes vote down vote up
public LockResponse(io.etcd.jetcd.api.lock.LockResponse response, ByteSequence namespace) {
    super(response, response.getHeader());
    this.unprefixedKey = ByteSequence.from(Util.unprefixNamespace(getResponse().getKey(), namespace));
}
 
Example #9
Source File: Member.java    From jetcd with Apache License 2.0 4 votes vote down vote up
/**
 * @return the list of URLs the member exposes to the cluster for communication.
 */
public List<URI> getPeerURIs() {
    return Util.toURIs(member.getPeerURLsList());
}
 
Example #10
Source File: Member.java    From jetcd with Apache License 2.0 2 votes vote down vote up
/**
 * @return list of URLs the member exposes to clients for communication, if the member is not started, clientURLs will
 *         be empty.
 */
public List<URI> getClientURIs() {
    return Util.toURIs(member.getClientURLsList());
}