Example usage for org.joda.time Duration millis

List of usage examples for org.joda.time Duration millis

Introduction

In this page you can find the example usage for org.joda.time Duration millis.

Prototype

public static Duration millis(long millis) 

Source Link

Document

Create a duration with the specified number of milliseconds.

Usage

From source file:io.druid.client.DirectDruidClient.java

License:Apache License

@Override
public Sequence<T> run(final QueryPlus<T> queryPlus, final Map<String, Object> context) {
    final Query<T> query = queryPlus.getQuery();
    QueryToolChest<T, Query<T>> toolChest = warehouse.getToolChest(query);
    boolean isBySegment = QueryContexts.isBySegment(query);

    Pair<JavaType, JavaType> types = typesMap.get(query.getClass());
    if (types == null) {
        final TypeFactory typeFactory = objectMapper.getTypeFactory();
        JavaType baseType = typeFactory.constructType(toolChest.getResultTypeReference());
        JavaType bySegmentType = typeFactory.constructParametricType(Result.class,
                typeFactory.constructParametricType(BySegmentResultValueClass.class, baseType));
        types = Pair.of(baseType, bySegmentType);
        typesMap.put(query.getClass(), types);
    }//from  w  w  w  .  j  a v  a 2 s  .  c  o m

    final JavaType typeRef;
    if (isBySegment) {
        typeRef = types.rhs;
    } else {
        typeRef = types.lhs;
    }

    final ListenableFuture<InputStream> future;
    final String url = StringUtils.format("%s://%s/druid/v2/", scheme, host);
    final String cancelUrl = StringUtils.format("%s://%s/druid/v2/%s", scheme, host, query.getId());

    try {
        log.debug("Querying queryId[%s] url[%s]", query.getId(), url);

        final long requestStartTimeNs = System.nanoTime();

        long timeoutAt = ((Long) context.get(QUERY_FAIL_TIME)).longValue();
        long maxScatterGatherBytes = QueryContexts.getMaxScatterGatherBytes(query);
        AtomicLong totalBytesGathered = (AtomicLong) context.get(QUERY_TOTAL_BYTES_GATHERED);

        final HttpResponseHandler<InputStream, InputStream> responseHandler = new HttpResponseHandler<InputStream, InputStream>() {
            private final AtomicLong byteCount = new AtomicLong(0);
            private final BlockingQueue<InputStream> queue = new LinkedBlockingQueue<>();
            private final AtomicBoolean done = new AtomicBoolean(false);
            private final AtomicReference<String> fail = new AtomicReference<>();

            private QueryMetrics<? super Query<T>> queryMetrics;
            private long responseStartTimeNs;

            private QueryMetrics<? super Query<T>> acquireResponseMetrics() {
                if (queryMetrics == null) {
                    queryMetrics = toolChest.makeMetrics(query);
                    queryMetrics.server(host);
                }
                return queryMetrics;
            }

            @Override
            public ClientResponse<InputStream> handleResponse(HttpResponse response) {
                checkQueryTimeout();
                checkTotalBytesLimit(response.getContent().readableBytes());

                log.debug("Initial response from url[%s] for queryId[%s]", url, query.getId());
                responseStartTimeNs = System.nanoTime();
                acquireResponseMetrics().reportNodeTimeToFirstByte(responseStartTimeNs - requestStartTimeNs)
                        .emit(emitter);

                try {
                    final String responseContext = response.headers().get("X-Druid-Response-Context");
                    // context may be null in case of error or query timeout
                    if (responseContext != null) {
                        context.putAll(objectMapper.<Map<String, Object>>readValue(responseContext,
                                new TypeReference<Map<String, Object>>() {
                                }));
                    }
                    queue.put(new ChannelBufferInputStream(response.getContent()));
                } catch (final IOException e) {
                    log.error(e, "Error parsing response context from url [%s]", url);
                    return ClientResponse.<InputStream>finished(new InputStream() {
                        @Override
                        public int read() throws IOException {
                            throw e;
                        }
                    });
                } catch (InterruptedException e) {
                    log.error(e, "Queue appending interrupted");
                    Thread.currentThread().interrupt();
                    throw Throwables.propagate(e);
                }
                byteCount.addAndGet(response.getContent().readableBytes());
                return ClientResponse
                        .<InputStream>finished(new SequenceInputStream(new Enumeration<InputStream>() {
                            @Override
                            public boolean hasMoreElements() {
                                if (fail.get() != null) {
                                    throw new RE(fail.get());
                                }
                                checkQueryTimeout();

                                // Done is always true until the last stream has be put in the queue.
                                // Then the stream should be spouting good InputStreams.
                                synchronized (done) {
                                    return !done.get() || !queue.isEmpty();
                                }
                            }

                            @Override
                            public InputStream nextElement() {
                                if (fail.get() != null) {
                                    throw new RE(fail.get());
                                }

                                try {
                                    InputStream is = queue.poll(checkQueryTimeout(), TimeUnit.MILLISECONDS);
                                    if (is != null) {
                                        return is;
                                    } else {
                                        throw new RE("Query[%s] url[%s] timed out.", query.getId(), url);
                                    }
                                } catch (InterruptedException e) {
                                    Thread.currentThread().interrupt();
                                    throw Throwables.propagate(e);
                                }
                            }
                        }));
            }

            @Override
            public ClientResponse<InputStream> handleChunk(ClientResponse<InputStream> clientResponse,
                    HttpChunk chunk) {
                checkQueryTimeout();

                final ChannelBuffer channelBuffer = chunk.getContent();
                final int bytes = channelBuffer.readableBytes();

                checkTotalBytesLimit(bytes);

                if (bytes > 0) {
                    try {
                        queue.put(new ChannelBufferInputStream(channelBuffer));
                    } catch (InterruptedException e) {
                        log.error(e, "Unable to put finalizing input stream into Sequence queue for url [%s]",
                                url);
                        Thread.currentThread().interrupt();
                        throw Throwables.propagate(e);
                    }
                    byteCount.addAndGet(bytes);
                }
                return clientResponse;
            }

            @Override
            public ClientResponse<InputStream> done(ClientResponse<InputStream> clientResponse) {
                long stopTimeNs = System.nanoTime();
                long nodeTimeNs = stopTimeNs - responseStartTimeNs;
                final long nodeTimeMs = TimeUnit.NANOSECONDS.toMillis(nodeTimeNs);
                log.debug(
                        "Completed queryId[%s] request to url[%s] with %,d bytes returned in %,d millis [%,f b/s].",
                        query.getId(), url, byteCount.get(), nodeTimeMs, byteCount.get() / (0.001 * nodeTimeMs) // Floating math; division by zero will yield Inf, not exception
                );
                QueryMetrics<? super Query<T>> responseMetrics = acquireResponseMetrics();
                responseMetrics.reportNodeTime(nodeTimeNs);
                responseMetrics.reportNodeBytes(byteCount.get());
                responseMetrics.emit(emitter);
                synchronized (done) {
                    try {
                        // An empty byte array is put at the end to give the SequenceInputStream.close() as something to close out
                        // after done is set to true, regardless of the rest of the stream's state.
                        queue.put(ByteSource.empty().openStream());
                    } catch (InterruptedException e) {
                        log.error(e, "Unable to put finalizing input stream into Sequence queue for url [%s]",
                                url);
                        Thread.currentThread().interrupt();
                        throw Throwables.propagate(e);
                    } catch (IOException e) {
                        // This should never happen
                        throw Throwables.propagate(e);
                    } finally {
                        done.set(true);
                    }
                }
                return ClientResponse.<InputStream>finished(clientResponse.getObj());
            }

            @Override
            public void exceptionCaught(final ClientResponse<InputStream> clientResponse, final Throwable e) {
                String msg = StringUtils.format("Query[%s] url[%s] failed with exception msg [%s]",
                        query.getId(), url, e.getMessage());
                setupResponseReadFailure(msg, e);
            }

            private void setupResponseReadFailure(String msg, Throwable th) {
                fail.set(msg);
                queue.clear();
                queue.offer(new InputStream() {
                    @Override
                    public int read() throws IOException {
                        if (th != null) {
                            throw new IOException(msg, th);
                        } else {
                            throw new IOException(msg);
                        }
                    }
                });

            }

            // Returns remaining timeout or throws exception if timeout already elapsed.
            private long checkQueryTimeout() {
                long timeLeft = timeoutAt - System.currentTimeMillis();
                if (timeLeft <= 0) {
                    String msg = StringUtils.format("Query[%s] url[%s] timed out.", query.getId(), url);
                    setupResponseReadFailure(msg, null);
                    throw new RE(msg);
                } else {
                    return timeLeft;
                }
            }

            private void checkTotalBytesLimit(long bytes) {
                if (maxScatterGatherBytes < Long.MAX_VALUE
                        && totalBytesGathered.addAndGet(bytes) > maxScatterGatherBytes) {
                    String msg = StringUtils.format("Query[%s] url[%s] max scatter-gather bytes limit reached.",
                            query.getId(), url);
                    setupResponseReadFailure(msg, null);
                    throw new RE(msg);
                }
            }
        };

        long timeLeft = timeoutAt - System.currentTimeMillis();

        if (timeLeft <= 0) {
            throw new RE("Query[%s] url[%s] timed out.", query.getId(), url);
        }

        future = httpClient.go(
                new Request(HttpMethod.POST, new URL(url))
                        .setContent(objectMapper.writeValueAsBytes(QueryContexts.withTimeout(query, timeLeft)))
                        .setHeader(HttpHeaders.Names.CONTENT_TYPE,
                                isSmile ? SmileMediaTypes.APPLICATION_JACKSON_SMILE
                                        : MediaType.APPLICATION_JSON),
                responseHandler, Duration.millis(timeLeft));

        queryWatcher.registerQuery(query, future);

        openConnections.getAndIncrement();
        Futures.addCallback(future, new FutureCallback<InputStream>() {
            @Override
            public void onSuccess(InputStream result) {
                openConnections.getAndDecrement();
            }

            @Override
            public void onFailure(Throwable t) {
                openConnections.getAndDecrement();
                if (future.isCancelled()) {
                    // forward the cancellation to underlying queriable node
                    try {
                        StatusResponseHolder res = httpClient
                                .go(new Request(HttpMethod.DELETE, new URL(cancelUrl))
                                        .setContent(objectMapper.writeValueAsBytes(query))
                                        .setHeader(HttpHeaders.Names.CONTENT_TYPE,
                                                isSmile ? SmileMediaTypes.APPLICATION_JACKSON_SMILE
                                                        : MediaType.APPLICATION_JSON),
                                        new StatusResponseHandler(Charsets.UTF_8), Duration.standardSeconds(1))
                                .get(1, TimeUnit.SECONDS);

                        if (res.getStatus().getCode() >= 500) {
                            throw new RE("Error cancelling query[%s]: queriable node returned status[%d] [%s].",
                                    res.getStatus().getCode(), res.getStatus().getReasonPhrase());
                        }
                    } catch (IOException | ExecutionException | InterruptedException | TimeoutException e) {
                        Throwables.propagate(e);
                    }
                }
            }
        });
    } catch (IOException e) {
        throw Throwables.propagate(e);
    }

    Sequence<T> retVal = new BaseSequence<>(new BaseSequence.IteratorMaker<T, JsonParserIterator<T>>() {
        @Override
        public JsonParserIterator<T> make() {
            return new JsonParserIterator<T>(typeRef, future, url, query);
        }

        @Override
        public void cleanup(JsonParserIterator<T> iterFromMake) {
            CloseQuietly.close(iterFromMake);
        }
    });

    // bySegment queries are de-serialized after caching results in order to
    // avoid the cost of de-serializing and then re-serializing again when adding to cache
    if (!isBySegment) {
        retVal = Sequences.map(retVal,
                toolChest.makePreComputeManipulatorFn(query, MetricManipulatorFns.deserializing()));
    }

    return retVal;
}

From source file:io.druid.indexing.overlord.helpers.TaskLogAutoCleaner.java

License:Apache License

@Override
public void schedule(ScheduledExecutorService exec) {
    log.info("Scheduling TaskLogAutoCleaner with config [%s].", config.toString());

    ScheduledExecutors.scheduleWithFixedDelay(exec, Duration.millis(config.getInitialDelay()),
            Duration.millis(config.getDelay()), new Runnable() {
                @Override//from ww  w . j av a2s.  c o  m
                public void run() {
                    try {
                        taskLogKiller.killOlderThan(System.currentTimeMillis() - config.getDurationToRetain());
                    } catch (Exception ex) {
                        log.error(ex, "Failed to clean-up the task logs");
                    }
                }
            });
}

From source file:io.druid.security.basic.CommonCacheNotifier.java

License:Apache License

private List<ListenableFuture<StatusResponseHolder>> sendUpdate(String updatedAuthorizerPrefix,
        byte[] serializedUserMap) {
    List<ListenableFuture<StatusResponseHolder>> futures = new ArrayList<>();
    for (String nodeType : NODE_TYPES) {
        DruidNodeDiscovery nodeDiscovery = discoveryProvider.getForNodeType(nodeType);
        Collection<DiscoveryDruidNode> nodes = nodeDiscovery.getAllNodes();
        for (DiscoveryDruidNode node : nodes) {
            URL listenerURL = getListenerURL(node.getDruidNode(), baseUrl, updatedAuthorizerPrefix);

            // best effort, if this fails, remote node will poll and pick up the update eventually
            Request req = new Request(HttpMethod.POST, listenerURL);
            req.setContent(MediaType.APPLICATION_JSON, serializedUserMap);

            BasicAuthDBConfig itemConfig = itemConfigMap.get(updatedAuthorizerPrefix);

            ListenableFuture<StatusResponseHolder> future = httpClient.go(req, new ResponseHandler(),
                    Duration.millis(itemConfig.getCacheNotificationTimeout()));
            futures.add(future);//from w w w  . ja  v a2 s.com
        }
    }
    return futures;
}

From source file:io.druid.server.coordination.ChangeRequestHttpSyncer.java

License:Apache License

private void sync() {
    if (!startStopLock.awaitStarted(1, TimeUnit.MILLISECONDS)) {
        log.info("Skipping sync() call for server[%s].", logIdentity);
        return;/*from  ww w  .j  a  va2  s . c  o m*/
    }

    lastSyncTime = System.currentTimeMillis();

    try {
        final String req;
        if (counter != null) {
            req = StringUtils.format("%s?counter=%s&hash=%s&timeout=%s", baseRequestPath, counter.getCounter(),
                    counter.getHash(), serverTimeoutMS);
        } else {
            req = StringUtils.format("%s?counter=-1&timeout=%s", baseRequestPath, serverTimeoutMS);
        }

        BytesAccumulatingResponseHandler responseHandler = new BytesAccumulatingResponseHandler();

        log.debug("Sending sync request to server[%s]", logIdentity);

        ListenableFuture<InputStream> future = httpClient.go(
                new Request(HttpMethod.GET, new URL(baseServerURL, req))
                        .addHeader(HttpHeaders.Names.ACCEPT, SmileMediaTypes.APPLICATION_JACKSON_SMILE)
                        .addHeader(HttpHeaders.Names.CONTENT_TYPE, SmileMediaTypes.APPLICATION_JACKSON_SMILE),
                responseHandler, Duration.millis(serverHttpTimeout));

        log.debug("Sent sync request to [%s]", logIdentity);

        Futures.addCallback(future, new FutureCallback<InputStream>() {
            @Override
            public void onSuccess(InputStream stream) {
                synchronized (startStopLock) {
                    if (!startStopLock.awaitStarted(1, TimeUnit.MILLISECONDS)) {
                        log.info("Skipping sync() success for server[%s].", logIdentity);
                        return;
                    }

                    try {
                        if (responseHandler.status == HttpServletResponse.SC_NO_CONTENT) {
                            log.debug("Received NO CONTENT from server[%s]", logIdentity);
                            lastSuccessfulSyncTime = System.currentTimeMillis();
                            return;
                        } else if (responseHandler.status != HttpServletResponse.SC_OK) {
                            handleFailure(new RE("Bad Sync Response."));
                            return;
                        }

                        log.debug("Received sync response from [%s]", logIdentity);

                        ChangeRequestsSnapshot<T> changes = smileMapper.readValue(stream,
                                responseTypeReferences);

                        log.debug("Finished reading sync response from [%s]", logIdentity);

                        if (changes.isResetCounter()) {
                            log.info("[%s] requested resetCounter for reason [%s].", logIdentity,
                                    changes.getResetCause());
                            counter = null;
                            return;
                        }

                        if (counter == null) {
                            // means, on last request either server had asked us to reset the counter or it was very first
                            // request to the server.
                            listener.fullSync(changes.getRequests());
                        } else {
                            listener.deltaSync(changes.getRequests());
                        }

                        counter = changes.getCounter();

                        initializationLatch.countDown();
                        consecutiveFailedAttemptCount = 0;
                        lastSuccessfulSyncTime = System.currentTimeMillis();
                    } catch (Exception ex) {
                        String logMsg = StringUtils.nonStrictFormat(
                                "Error processing sync response from [%s]. Reason [%s]", logIdentity,
                                ex.getMessage());

                        if (incrementFailedAttemptAndCheckUnstabilityTimeout()) {
                            log.error(ex, logMsg);
                        } else {
                            log.info("Temporary Failure. %s", logMsg);
                            log.debug(ex, logMsg);
                        }
                    } finally {
                        addNextSyncToWorkQueue();
                    }
                }
            }

            @Override
            public void onFailure(Throwable t) {
                synchronized (startStopLock) {
                    if (!startStopLock.awaitStarted(1, TimeUnit.MILLISECONDS)) {
                        log.info("Skipping sync() failure for URL[%s].", logIdentity);
                        return;
                    }

                    try {
                        handleFailure(t);
                    } finally {
                        addNextSyncToWorkQueue();
                    }
                }
            }

            private void handleFailure(Throwable t) {
                String logMsg = StringUtils.nonStrictFormat(
                        "failed to get sync response from [%s]. Return code [%s], Reason: [%s]", logIdentity,
                        responseHandler.status, responseHandler.description);

                if (incrementFailedAttemptAndCheckUnstabilityTimeout()) {
                    log.error(t, logMsg);
                } else {
                    log.info("Temporary Failure. %s", logMsg);
                    log.debug(t, logMsg);
                }
            }
        }, executor);
    } catch (Throwable th) {
        try {
            String logMsg = StringUtils.nonStrictFormat("Fatal error while fetching segment list from [%s].",
                    logIdentity);

            if (incrementFailedAttemptAndCheckUnstabilityTimeout()) {
                log.makeAlert(th, logMsg).emit();
            } else {
                log.info("Temporary Failure. %s", logMsg);
                log.debug(th, logMsg);
            }
        } finally {
            addNextSyncToWorkQueue();
        }
    }
}

From source file:io.v.android.apps.syncslides.db.SyncbaseDB.java

License:Open Source License

private void createPresentationRunnable(final String deckId,
        final Callback<CreatePresentationResult> callback) {
    final String presentationId = UUID.randomUUID().toString();
    String prefix = NamingUtil.join(deckId, presentationId);
    try {/*w w w.  java2s.c  o m*/
        Blessings blessings = V23Manager.Singleton.get().getBlessings();
        AccessList everyoneAcl = new AccessList(ImmutableList.of(new BlessingPattern("...")),
                ImmutableList.<String>of());
        AccessList justMeAcl = new AccessList(ImmutableList.of(new BlessingPattern(blessings.toString())),
                ImmutableList.<String>of());

        Permissions groupReadPermissions = new Permissions(ImmutableMap.of(Constants.RESOLVE.getValue(),
                everyoneAcl, Constants.READ.getValue(), everyoneAcl, Constants.WRITE.getValue(), justMeAcl,
                Constants.ADMIN.getValue(), justMeAcl));
        Permissions groupWritePermissions = new Permissions(ImmutableMap.of(Constants.RESOLVE.getValue(),
                everyoneAcl, Constants.READ.getValue(), everyoneAcl, Constants.WRITE.getValue(), justMeAcl,
                Constants.ADMIN.getValue(), justMeAcl));

        // Add rows to Presentations table.
        VPresentation presentation = new VPresentation(); // Empty for now.
        mPresentations.put(mVContext, prefix, presentation, VPresentation.class);
        VCurrentSlide current = new VCurrentSlide(0);
        mPresentations.put(mVContext, NamingUtil.join(prefix, CURRENT_SLIDE), current, VCurrentSlide.class);

        mPresentations.setPrefixPermissions(mVContext, RowRange.prefix(prefix), groupReadPermissions);
        // Anybody can add a question.
        mPresentations.setPrefixPermissions(mVContext, RowRange.prefix(NamingUtil.join(prefix, QUESTIONS)),
                groupWritePermissions);
        mDecks.setPrefixPermissions(mVContext, RowRange.prefix(deckId), groupReadPermissions);

        // Create the syncgroup.
        final String syncgroupName = NamingUtil.join(
                mSyncbaseServer.getStatus().getPublisherStatus()[0].getName(), "%%sync/syncslides", prefix);
        Log.i(TAG, "Creating syncgroup " + syncgroupName);
        Syncgroup syncgroup = mDB.getSyncgroup(syncgroupName);
        CancelableVContext context = mVContext.withTimeout(Duration.millis(5000));
        try {
            syncgroup.create(context,
                    new SyncgroupSpec(SYNCGROUP_PRESENTATION_DESCRIPTION, groupReadPermissions,
                            Arrays.asList(new TableRow(PRESENTATIONS_TABLE, prefix),
                                    new TableRow(DECKS_TABLE, deckId)),
                            Arrays.asList(V23Manager.syncName("sg")), false),
                    new SyncgroupMemberInfo((byte) 10, false));
        } catch (ExistException e) {
            Log.i(TAG, "Syncgroup already exists");
        }
        Log.i(TAG, "Finished creating syncgroup");

        // TODO(kash): Create a syncgroup for Notes?  Not sure if we should do that
        // here or somewhere else.  We're not going to demo sync across a user's
        // devices right away, so we'll figure this out later.

        Handler handler = new Handler(Looper.getMainLooper());
        handler.post(new Runnable() {
            @Override
            public void run() {
                callback.done(new CreatePresentationResult(presentationId, syncgroupName));
            }
        });
    } catch (VException e) {
        // TODO(kash): Change Callback to take an error parameter.
        handleError(e.toString());
        Handler handler = new Handler(Looper.getMainLooper());
        handler.post(new Runnable() {
            @Override
            public void run() {
                // TODO(kash): fix me!
                callback.done(new CreatePresentationResult(presentationId, "dummy name"));
            }
        });
    }
}

From source file:io.v.syncslides.db.SyncbaseDB.java

License:Open Source License

@Override
public Session getSession(String sessionId) throws VException {
    Table ui = mDB.getTable(UI_TABLE);//  w w  w.j  av  a  2s .co m
    CancelableVContext context = mVContext.withTimeout(Duration.millis(5000));
    VSession vSession = (VSession) sync(ui.get(context, sessionId, VSession.class));
    return new SyncbaseSession(mVContext, mDB, sessionId, vSession);
}

From source file:io.v.syncslides.db.SyncbaseSession.java

License:Open Source License

@Override
public void setNotes(int slideNum, String notes) throws VException {
    Table table = mDb.getTable(SyncbaseDB.NOTES_TABLE);
    CancelableVContext context = mVContext.withTimeout(Duration.millis(5000));
    String rowKey = SyncbaseDB.slideRowKey(mVSession.getDeckId(), slideNum);
    sync(table.put(context, rowKey, new VNote(notes), VNote.class));
}

From source file:io.v.syncslides.db.SyncbaseSession.java

License:Open Source License

/**
 * Persists the VSession to the UI_TABLE.
 *
 * @throws VException when the Syncbase put() fails
 *//*from w w  w . java  2s  .c o  m*/
void save() throws VException {
    mVSession.setLastTouched(System.currentTimeMillis());
    Table ui = mDb.getTable(SyncbaseDB.UI_TABLE);
    CancelableVContext context = mVContext.withTimeout(Duration.millis(5000));
    sync(ui.put(context, mId, mVSession, VSession.class));
}

From source file:io.v.todos.persistence.syncbase.SyncbasePersistence.java

License:Open Source License

private static void ensureCloudDatabaseExists() {
    synchronized (sCloudDatabaseMutex) {
        if (sCloudDatabase == null) {
            SyncbaseService cloudService = Syncbase.newService(CLOUD_NAME);
            Database db = cloudService.getDatabase(getAppVContext(), DATABASE, null);

            try {
                VFutures.sync(db.create(getAppVContext().withTimeout(Duration.millis(SHORT_TIMEOUT)), null));
            } catch (ExistException e) {
                // This is acceptable. No need to do it again.
            } catch (Exception e) {
                Log.w(TAG, "Could not ensure cloud database exists: " + e.getMessage());
            }/*from  ww w  .j  av  a2 s. c  o m*/
            sCloudDatabase = db;
        }
    }
}

From source file:io.vitess.example.VitessClientExample.java

License:Apache License

public static void main(String[] args) {
    if (args.length != 1) {
        System.out.println("usage: VitessClientExample <vtgate-host:port>");
        System.exit(1);/*www  .  ja v a2  s . c  o  m*/
    }

    Context ctx = Context.getDefault().withDeadlineAfter(Duration.millis(5 * 1000));
    try (RpcClient client = new GrpcClientFactory().create(ctx, args[0]);
            VTGateBlockingConnection conn = new VTGateBlockingConnection(client)) {
        VTSession session = new VTSession("@master", ExecuteOptions.getDefaultInstance());
        // Insert some messages on random pages.
        System.out.println("Inserting into master...");
        Random rand = new Random();
        for (int i = 0; i < 3; i++) {
            Instant timeCreated = Instant.now();
            Map<String, Object> bindVars = new ImmutableMap.Builder<String, Object>()
                    .put("page", rand.nextInt(100) + 1)
                    .put("time_created_ns", timeCreated.getMillis() * 1000000).put("message", "V is for speed")
                    .build();

            conn.execute(ctx, "begin", null, session);
            conn.execute(ctx,
                    "INSERT INTO messages (page,time_created_ns,message) VALUES (:page,:time_created_ns,"
                            + ":message)",
                    bindVars, session);
            conn.execute(ctx, "commit", null, session);
        }

        // Read it back from the master.
        System.out.println("Reading from master...");
        try (Cursor cursor = conn.execute(ctx, "SELECT page, time_created_ns, message FROM messages", null,
                session)) {
            Row row;
            while ((row = cursor.next()) != null) {
                UnsignedLong page = row.getULong("page");
                UnsignedLong timeCreated = row.getULong("time_created_ns");
                byte[] message = row.getBytes("message");
                System.out.format("(%s, %s, %s)\n", page, timeCreated, new String(message));
            }
        }
    } catch (Exception exc) {
        System.out.println("Vitess Java example failed.");
        System.out.println("Error Details:");
        exc.printStackTrace();
        System.exit(2);
    }
}