Exemplo n.º 1
0
  @SuppressWarnings("unchecked")
  @Test
  public void demonstrateGetWithRequestContext() {
    probe = new JavaTestKit(system);

    // #get-request-context
    final ActorRef replicator = DistributedData.get(system).replicator();
    final ReadConsistency readTwo = new ReadFrom(2, Duration.create(3, SECONDS));
    final Key<PNCounter> counter1Key = PNCounterKey.create("counter1");

    receive(
        ReceiveBuilder.match(
                String.class,
                a -> a.equals("get-count"),
                a -> {
                  // incoming request to retrieve current value of the counter
                  Optional<Object> reqContext = Optional.of(sender());
                  replicator.tell(new Replicator.Get<PNCounter>(counter1Key, readTwo), self());
                })
            .match(
                GetSuccess.class,
                a -> a.key().equals(counter1Key),
                a -> {
                  ActorRef replyTo = (ActorRef) a.getRequest().get();
                  GetSuccess<PNCounter> g = a;
                  long value = g.dataValue().getValue().longValue();
                  replyTo.tell(value, self());
                })
            .match(
                GetFailure.class,
                a -> a.key().equals(counter1Key),
                a -> {
                  ActorRef replyTo = (ActorRef) a.getRequest().get();
                  replyTo.tell(-1L, self());
                })
            .match(
                NotFound.class,
                a -> a.key().equals(counter1Key),
                a -> {
                  ActorRef replyTo = (ActorRef) a.getRequest().get();
                  replyTo.tell(0L, self());
                })
            .build());
    // #get-request-context
  }
Exemplo n.º 2
0
  @SuppressWarnings({"unused", "unchecked"})
  @Test
  public void demonstrateGet() {
    probe = new JavaTestKit(system);

    // #get
    final ActorRef replicator = DistributedData.get(system).replicator();
    final Key<PNCounter> counter1Key = PNCounterKey.create("counter1");
    final Key<GSet<String>> set1Key = GSetKey.create("set1");
    final Key<ORSet<String>> set2Key = ORSetKey.create("set2");
    final Key<Flag> activeFlagKey = FlagKey.create("active");

    replicator.tell(new Replicator.Get<PNCounter>(counter1Key, Replicator.readLocal()), self());

    final ReadConsistency readFrom3 = new ReadFrom(3, Duration.create(1, SECONDS));
    replicator.tell(new Replicator.Get<GSet<String>>(set1Key, readFrom3), self());

    final ReadConsistency readMajority = new ReadMajority(Duration.create(5, SECONDS));
    replicator.tell(new Replicator.Get<ORSet<String>>(set2Key, readMajority), self());

    final ReadConsistency readAll = new ReadAll(Duration.create(5, SECONDS));
    replicator.tell(new Replicator.Get<Flag>(activeFlagKey, readAll), self());
    // #get

    // #get-response1
    receive(
        ReceiveBuilder.match(
                GetSuccess.class,
                a -> a.key().equals(counter1Key),
                a -> {
                  GetSuccess<PNCounter> g = a;
                  BigInteger value = g.dataValue().getValue();
                })
            .match(
                NotFound.class,
                a -> a.key().equals(counter1Key),
                a -> {
                  // key counter1 does not exist
                })
            .build());
    // #get-response1

    // #get-response2
    receive(
        ReceiveBuilder.match(
                GetSuccess.class,
                a -> a.key().equals(set1Key),
                a -> {
                  GetSuccess<GSet<String>> g = a;
                  Set<String> value = g.dataValue().getElements();
                })
            .match(
                GetFailure.class,
                a -> a.key().equals(set1Key),
                a -> {
                  // read from 3 nodes failed within 1.second
                })
            .match(
                NotFound.class,
                a -> a.key().equals(set1Key),
                a -> {
                  // key set1 does not exist
                })
            .build());
    // #get-response2

  }