本文整理匯總了Java中akka.pattern.Patterns類的典型用法代碼示例。如果您正苦於以下問題:Java Patterns類的具體用法?Java Patterns怎麽用?Java Patterns使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
Patterns類屬於akka.pattern包,在下文中一共展示了Patterns類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: verifyActorReady
import akka.pattern.Patterns; //導入依賴的package包/類
@SuppressWarnings("checkstyle:IllegalCatch")
private void verifyActorReady(ActorRef actorRef) {
// Sometimes we see messages go to dead letters soon after creation - it seems the actor isn't quite
// in a state yet to receive messages or isn't actually created yet. This seems to happen with
// actorSelection so, to alleviate it, we use an actorSelection and send an Identify message with
// retries to ensure it's ready.
Timeout timeout = new Timeout(100, TimeUnit.MILLISECONDS);
Throwable lastError = null;
Stopwatch sw = Stopwatch.createStarted();
while (sw.elapsed(TimeUnit.SECONDS) <= 10) {
try {
ActorSelection actorSelection = system.actorSelection(actorRef.path().toString());
Future<Object> future = Patterns.ask(actorSelection, new Identify(""), timeout);
ActorIdentity reply = (ActorIdentity)Await.result(future, timeout.duration());
Assert.assertNotNull("Identify returned null", reply.getRef());
return;
} catch (Exception | AssertionError e) {
Uninterruptibles.sleepUninterruptibly(100, TimeUnit.MILLISECONDS);
lastError = e;
}
}
throw new RuntimeException(lastError);
}
示例2: testPersistentActorWIthIgnite
import akka.pattern.Patterns; //導入依賴的package包/類
@Test
public void testPersistentActorWIthIgnite() throws Exception {
ActorRef actorRef = actorSystem.actorOf(Props.create(IgnitePersistentTestActor.class, "1"));
actorRef.tell("+a", ActorRef.noSender());
actorRef.tell("+b", ActorRef.noSender());
actorRef.tell("+c", ActorRef.noSender());
actorRef.tell("throw", ActorRef.noSender());
Future<Object> future = Patterns.ask(actorRef, "-b", 1000);
Await.result(future, Duration.create(1, TimeUnit.SECONDS));
IgniteCache<Object, Object> cache = ignite.getOrCreateCache("akka-journal");
Assert.assertEquals(cache.size(), 4);
actorSystem.actorSelection("akka://test/user/**").tell("!!!", ActorRef.noSender());
Await.result(actorSystem.terminate(), Duration.create(1, TimeUnit.SECONDS));
}
示例3: testStartGame
import akka.pattern.Patterns; //導入依賴的package包/類
@Test
public void testStartGame() throws Exception {
new JavaTestKit(system) {{
StartGameVsCPUMsg msg = new StartGameVsCPUMsg(true, ac.getGameViewActorRef());
Future<Object> future = Patterns.ask(ac.getCpuActorRef(), msg, 3000);
assertTrue(future.isCompleted());
TestMessage received = (TestMessage)Await.result(future, Duration.Zero());
assertEquals(MessageType.TEST, received.getType());
assertTrue(ac.getCpuActor().getCurrentSequence().size() == 1);
assertTrue(ac.getCpuActor().getnColors() == Constants.CLASSIC_MODE);
}};
}
示例4: testTimeToBlink
import akka.pattern.Patterns; //導入依賴的package包/類
@Test
public void testTimeToBlink() throws Exception {
new JavaTestKit(system) {{
List<SimonColorImpl> colors = getExampleSequence();
TimeToBlinkMsg msg = new TimeToBlinkMsg(colors);
Future<Object> future = Patterns.ask(ac.getGameViewActorRef(), msg, 3000);
assertTrue(future.isCompleted());
TestMessage received = (TestMessage)Await.result(future, Duration.Zero());
assertEquals(MessageType.TEST, received.getType());
assertTrue(ac.getGameViewActor().getCpuColorIndex() == 0);
assertFalse(ac.getGameViewActor().isPaused());
assertFalse(ac.getGameViewActor().isPlayerTurn());
assertTrue(ac.getGameViewActor().getCpuSequence().equals(colors));
}};
}
示例5: testHandlePlayerMsg
import akka.pattern.Patterns; //導入依賴的package包/類
@Test
public void testHandlePlayerMsg() throws Exception {
new JavaTestKit(system) {{
PlayerTurnMsg msg = new PlayerTurnMsg();
Future<Object> future = Patterns.ask(ac.getGameViewActorRef(), msg, 3000);
assertTrue(future.isCompleted());
TestMessage received = (TestMessage)Await.result(future, Duration.Zero());
assertEquals(MessageType.TEST, received.getType());
assertEquals(ac.getGameViewActor().getPlayerColorIndex(), 0);
assertEquals(ac.getGameViewActor().getPlayerSequence().size(), 0);
}};
}
示例6: testPause
import akka.pattern.Patterns; //導入依賴的package包/類
@Test
public void testPause() throws Exception {
new JavaTestKit(system) {{
PauseMsg pause1 = new PauseMsg(true);
Future<Object> future1 = Patterns.ask(ac.getGameViewActorRef(), pause1, 3000);
assertTrue(future1.isCompleted());
Await.result(future1, Duration.Zero());
assertTrue(ac.getGameViewActor().isPaused());
PauseMsg pause2 = new PauseMsg(false);
Future<Object> future2 = Patterns.ask(ac.getGameViewActorRef(), pause2, 3000);
assertTrue(future2.isCompleted());
Await.result(future2, Duration.Zero());
assertFalse(ac.getGameViewActor().isPaused());
TestMessage received3 = (TestMessage)Await.result(future2, Duration.Zero());
assertEquals(MessageType.TEST, received3.getType());
assertFalse(ac.getGameViewActor().isPaused());
}};
}
示例7: sendShutDown
import akka.pattern.Patterns; //導入依賴的package包/類
private void sendShutDown(final ActorRef actor) throws Exception {
testLog.info("sendShutDown for {} starting", actor.path());
FiniteDuration duration = FiniteDuration.create(5, TimeUnit.SECONDS);
Future<Boolean> stopFuture = Patterns.gracefulStop(actor, duration, Shutdown.INSTANCE);
Boolean stopped = Await.result(stopFuture, duration);
assertEquals("Stopped", Boolean.TRUE, stopped);
testLog.info("sendShutDown for {} ending", actor.path());
}
示例8: waitUntilLeader
import akka.pattern.Patterns; //導入依賴的package包/類
@SuppressWarnings("checkstyle:IllegalCatch")
public static void waitUntilLeader(ActorRef actorRef) {
FiniteDuration duration = Duration.create(100, TimeUnit.MILLISECONDS);
for (int i = 0; i < 20 * 5; i++) {
Future<Object> future = Patterns.ask(actorRef, FindLeader.INSTANCE, new Timeout(duration));
try {
final Optional<String> maybeLeader = ((FindLeaderReply)Await.result(future, duration)).getLeaderActor();
if (maybeLeader.isPresent()) {
return;
}
} catch (Exception e) {
LOG.error("FindLeader failed", e);
}
Uninterruptibles.sleepUninterruptibly(50, TimeUnit.MILLISECONDS);
}
Assert.fail("Leader not found for actorRef " + actorRef.path());
}
示例9: tryCommitModifications
import akka.pattern.Patterns; //導入依賴的package包/類
void tryCommitModifications(final BatchedModifications modifications) {
if (isLeader()) {
LOG.debug("{}: Committing BatchedModifications {} locally", persistenceId(),
modifications.getTransactionId());
// Note that it's possible the commit won't get consensus and will timeout and not be applied
// to the state. However we don't need to retry it in that case b/c it will be committed to
// the journal first and, once a majority of followers come back on line and it is replicated,
// it will be applied at that point.
handleBatchedModificationsLocal(modifications, self());
} else {
final ActorSelection leader = getLeader();
if (leader != null) {
possiblyRemoveAllInitialCandidates(leader);
LOG.debug("{}: Sending BatchedModifications {} to leader {}", persistenceId(),
modifications.getTransactionId(), leader);
Future<Object> future = Patterns.ask(leader, modifications, TimeUnit.SECONDS.toMillis(
getDatastoreContext().getShardTransactionCommitTimeoutInSeconds()));
Patterns.pipe(future, getContext().dispatcher()).pipeTo(getSelf(), ActorRef.noSender());
}
}
}
示例10: getListenerActorsInfo
import akka.pattern.Patterns; //導入依賴的package包/類
@SuppressWarnings("checkstyle:IllegalCatch")
private List<DataTreeListenerInfo> getListenerActorsInfo(Collection<ActorSelection> actors) {
final Timeout timeout = new Timeout(20, TimeUnit.SECONDS);
final List<Future<Object>> futureList = new ArrayList<>(actors.size());
for (ActorSelection actor: actors) {
futureList.add(Patterns.ask(actor, GetInfo.INSTANCE, timeout));
}
try {
final List<DataTreeListenerInfo> listenerInfoList = new ArrayList<>();
Await.result(Futures.sequence(futureList, ExecutionContext.Implicits$.MODULE$.global()),
timeout.duration()).forEach(obj -> listenerInfoList.add((DataTreeListenerInfo) obj));
return listenerInfoList;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
示例11: performRegistration
import akka.pattern.Patterns; //導入依賴的package包/類
private synchronized void performRegistration(ActorRef shard) {
if (isClosed()) {
return;
}
cohortRegistry = shard;
Future<Object> future =
Patterns.ask(shard, new DataTreeCohortActorRegistry.RegisterCohort(subtree, actor), TIMEOUT);
future.onComplete(new OnComplete<Object>() {
@Override
public void onComplete(Throwable failure, Object val) {
if (failure != null) {
LOG.error("Unable to register {} as commit cohort", getInstance(), failure);
}
if (isClosed()) {
removeRegistration();
}
}
}, actorContext.getClientDispatcher());
}
示例12: run
import akka.pattern.Patterns; //導入依賴的package包/類
@Override
public void run() {
final Future<Object> ask = Patterns.ask(shard, FindLeader.INSTANCE, context.getOperationTimeout());
ask.onComplete(new OnComplete<Object>() {
@Override
public void onComplete(final Throwable throwable, final Object findLeaderReply) throws Throwable {
if (throwable != null) {
tryReschedule(throwable);
} else {
final FindLeaderReply findLeader = (FindLeaderReply) findLeaderReply;
final java.util.Optional<String> leaderActor = findLeader.getLeaderActor();
if (leaderActor.isPresent()) {
// leader is found, backend seems ready, check if the frontend is ready
LOG.debug("{} - Leader for config shard is ready. Ending lookup.",
clusterWrapper.getCurrentMemberName());
replyTo.tell(new Status.Success(null), noSender());
} else {
tryReschedule(null);
}
}
}
}, system.dispatcher());
}
示例13: lookupConfigShard
import akka.pattern.Patterns; //導入依賴的package包/類
private ListenableFuture<Void> lookupConfigShard(final LogicalDatastoreType type) {
final SettableFuture<Void> future = SettableFuture.create();
final Future<Object> ask =
Patterns.ask(shardedDataTreeActor, new StartConfigShardLookup(type), SHARD_FUTURE_TIMEOUT);
ask.onComplete(new OnComplete<Object>() {
@Override
public void onComplete(final Throwable throwable, final Object result) throws Throwable {
if (throwable != null) {
future.setException(throwable);
} else {
future.set(null);
}
}
}, actorSystem.dispatcher());
return future;
}
示例14: close
import akka.pattern.Patterns; //導入依賴的package包/類
@Override
public CompletionStage<Void> close() {
// first despawn on the local node
distributedShardedDOMDataTree.despawnShardFrontend(prefix);
// update the config so the remote nodes are updated
final Future<Object> ask =
Patterns.ask(shardedDataTreeActor, new PrefixShardRemovalLookup(prefix), SHARD_FUTURE_TIMEOUT);
final Future<Void> closeFuture = ask.transform(
new Mapper<Object, Void>() {
@Override
public Void apply(final Object parameter) {
return null;
}
},
new Mapper<Throwable, Throwable>() {
@Override
public Throwable apply(final Throwable throwable) {
return throwable;
}
}, actorSystem.dispatcher());
return FutureConverters.toJava(closeFuture);
}
示例15: verifyRaftState
import akka.pattern.Patterns; //導入依賴的package包/類
static void verifyRaftState(final TestActorRef<? extends EntityOwnershipShard> shard,
Consumer<OnDemandRaftState> verifier)
throws Exception {
AssertionError lastError = null;
Stopwatch sw = Stopwatch.createStarted();
while (sw.elapsed(TimeUnit.SECONDS) <= 5) {
FiniteDuration operationDuration = Duration.create(5, TimeUnit.SECONDS);
Future<Object> future = Patterns.ask(shard, GetOnDemandRaftState.INSTANCE, new Timeout(operationDuration));
OnDemandRaftState raftState = (OnDemandRaftState)Await.result(future, operationDuration);
try {
verifier.accept(raftState);
return;
} catch (AssertionError e) {
lastError = e;
Uninterruptibles.sleepUninterruptibly(50, TimeUnit.MILLISECONDS);
}
}
throw lastError;
}