Elffuss BraidHub

kikocis/java-driver public

a7a8be09fd1a · 2.1 KiBRawPermalink
1/*
2 * Copyright (C) 2012-2015 DataStax Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package com.datastax.driver.stress;
17
18import com.datastax.driver.core.Session;
19import com.datastax.driver.core.exceptions.DriverException;
20import com.google.common.util.concurrent.Uninterruptibles;
21
22public class BlockingConsumer implements Consumer {
23
24 private final Runner runner = new Runner();
25
26 private final Session session;
27 private final QueryGenerator requests;
28 private final Reporter reporter;
29
30 public BlockingConsumer(Session session,
31 QueryGenerator requests,
32 Reporter reporter) {
33 this.session = session;
34 this.requests = requests;
35 this.reporter = reporter;
36 this.runner.setDaemon(true);
37 }
38
39 @Override
40 public void start() {
41 this.runner.start();
42 }
43
44 @Override
45 public void join() {
46 Uninterruptibles.joinUninterruptibly(this.runner);
47 }
48
49 private class Runner extends Thread {
50
51 public Runner() {
52 super("Consumer Threads");
53 }
54
55 @Override
56 public void run() {
57 try {
58 while (requests.hasNext())
59 handle(requests.next());
60 } catch (DriverException e) {
61 System.err.println("Error during query: " + e.getMessage());
62 }
63 }
64
65 protected void handle(QueryGenerator.Request request) {
66 Reporter.Context ctx = reporter.newRequest();
67 try {
68 request.execute(session);
69 } finally {
70 ctx.done();
71 }
72 }
73 }
74}