Java Code Examples for scala.concurrent.duration.Duration#create()
The following examples show how to use
scala.concurrent.duration.Duration#create() .
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: ExecutorTest.java From Nickle-Scheduler with Apache License 2.0 | 6 votes |
@Test public void testExecutor() { ActorRef server = system.actorOf(ServerActor.props()); Timeout t = new Timeout(Duration.create(3, TimeUnit.SECONDS)); //使用ask发送消息,actor处理完,必须有返回(超时时间5秒) try { Future<Object> ask = Patterns.ask(server, "123", t); ask.onComplete(new OnComplete<Object>() { @Override public void onComplete(Throwable throwable, Object o) throws Throwable { if (throwable != null) { System.out.println("some thing wrong.{}" + throwable); } else { System.out.println("success:" + o); } } }, system.dispatcher()); System.out.println("执行完毕"); } catch (Exception e) { e.printStackTrace(); } }
Example 2
Source File: PingWorkerTest.java From parallec with Apache License 2.0 | 6 votes |
@Test public void testTcpWorkerBadMsgType() { logger.info("IN testTcpWorkerBadMsgType"); ActorRef asyncWorker = null; try { int actorMaxOperationTimeoutSec = 15; asyncWorker = ActorConfig.createAndGetActorSystem().actorOf( Props.create(PingWorker.class, actorMaxOperationTimeoutSec, getPingMetaSample(), "www.google.com")); final FiniteDuration duration = Duration.create(20, TimeUnit.SECONDS); Future<Object> future = Patterns .ask(asyncWorker, new Integer(0), new Timeout(duration)); ResponseOnSingeRequest response = (ResponseOnSingeRequest) Await .result(future, duration); logger.info("\nWorker response:" + response.toString()); } catch (Throwable ex) { logger.error("Exception in test : " + ex); } }
Example 3
Source File: PingWorkerTest.java From parallec with Apache License 2.0 | 6 votes |
@Test public void testSlowAndPollProgress() { ActorRef asyncWorker = null; try { int actorMaxOperationTimeoutSec = 15; asyncWorker = ActorConfig.createAndGetActorSystem().actorOf( Props.create(PingWorker.class, actorMaxOperationTimeoutSec, getPingMetaSample(), "www.google.com")); final FiniteDuration duration = Duration.create(20, TimeUnit.SECONDS); Future<Object> future = Patterns .ask(asyncWorker, RequestWorkerMsgType.PROCESS_REQUEST, new Timeout(duration)); ResponseOnSingeRequest response = (ResponseOnSingeRequest) Await .result(future, duration); logger.info("\nWorker response:" + response.toString()); } catch (Throwable ex) { logger.error("Exception in test : " + ex); } }
Example 4
Source File: KafkaSagaEventConsumer.java From servicecomb-pack with Apache License 2.0 | 6 votes |
private CompletionStage<String> sendSagaActor(BaseEvent event) { try { long begin = System.currentTimeMillis(); metricsService.metrics().doActorReceived(); // Use the synchronous method call to ensure that Kafka's Offset is set after the delivery is successful. Timeout timeout = new Timeout(Duration.create(10, "seconds")); Future<Object> future = Patterns.ask(sagaShardRegionActor, event, timeout); Await.result(future, timeout.duration()); long end = System.currentTimeMillis(); metricsService.metrics().doActorAccepted(); metricsService.metrics().doActorAvgTime(end - begin); return CompletableFuture.completedFuture("OK"); } catch (Exception ex) { LOG.error(ex.getMessage(),ex); metricsService.metrics().doActorRejected(); throw new CompletionException(ex); } }
Example 5
Source File: UdpWorkerTest.java From parallec with Apache License 2.0 | 5 votes |
@Test public void testUdpWorkerNormalCheckComplete() { ActorRef asyncWorker = null; logger.info("IN testUdpWorkerNormalCheckComplete"); try { // Start new job int actorMaxOperationTimeoutSec = 15; asyncWorker = ActorConfig.createAndGetActorSystem().actorOf( Props.create(UdpWorker.class, actorMaxOperationTimeoutSec, getUdpMetaSample(), LOCALHOST)); final FiniteDuration duration = Duration.create(20, TimeUnit.SECONDS); Future<Object> future = Patterns .ask(asyncWorker, RequestWorkerMsgType.PROCESS_REQUEST, new Timeout(duration)); ResponseOnSingeRequest response = (ResponseOnSingeRequest) Await .result(future, duration); logger.info("\nWorker response:" + response.toString()); } catch (Throwable ex) { logger.error("Exception in test : " + ex); } }
Example 6
Source File: TcpWorkerIdleTest.java From parallec with Apache License 2.0 | 5 votes |
@Test public void testTcpWorkerNormalCheckCompleteForIdle() { ActorRef asyncWorker = null; logger.info("IN testTcpWorkerNormalCheckCompleteForIdle"); try { // Start new job int actorMaxOperationTimeoutSec = 15; asyncWorker = ActorConfig.createAndGetActorSystem().actorOf( Props.create(TcpWorker.class, actorMaxOperationTimeoutSec, getTcpMetaSample(), LOCALHOST)); final FiniteDuration duration = Duration.create(20, TimeUnit.SECONDS); Future<Object> future = Patterns .ask(asyncWorker, RequestWorkerMsgType.PROCESS_REQUEST, new Timeout(duration)); ResponseOnSingeRequest response = (ResponseOnSingeRequest) Await .result(future, duration); logger.info("\nWorker response:" + response.toString()); } catch (Throwable ex) { logger.error("Exception in test : " + ex); } }
Example 7
Source File: TcpWorkerTest.java From parallec with Apache License 2.0 | 5 votes |
/** * fake a NPE in bootStrapTcpClient */ @Test public void testBootStrapTcpClient() { logger.info("IN testTcpWorkerException"); ActorRef asyncWorker = null; try { // Start new job TcpMeta meta = getTcpMetaSample(); meta.setChannelFactory(null); int actorMaxOperationTimeoutSec = 15; asyncWorker = ActorConfig.createAndGetActorSystem().actorOf( Props.create(TcpWorker.class, actorMaxOperationTimeoutSec, meta, LOCALHOST)); final FiniteDuration duration = Duration.create(20, TimeUnit.SECONDS); Future<Object> future = Patterns .ask(asyncWorker, RequestWorkerMsgType.PROCESS_REQUEST, new Timeout(duration)); ResponseOnSingeRequest response = (ResponseOnSingeRequest) Await .result(future, duration); logger.info("\nWorker response:" + response.toString()); } catch (Throwable ex) { } }
Example 8
Source File: ExecutionManagerTest.java From parallec with Apache License 2.0 | 5 votes |
@Test public void testHttpWorkerNormalCheckCancel() { ActorRef executionManager = null; try { // Start new job ParallelTask task = genParallelTask(); InternalDataProvider adp = InternalDataProvider.getInstance(); adp.genNodeDataMap(task); executionManager = ActorConfig.createAndGetActorSystem().actorOf( Props.create(ExecutionManager.class, task), "executionManager-" + task.getTaskId()); final FiniteDuration duration = Duration.create(20, TimeUnit.SECONDS); Future<Object> future = Patterns.ask(executionManager, new InitialRequestToManager(task), new Timeout(duration)); executionManager.tell(ExecutionManagerMsgType.CANCEL, executionManager); Await.result(future, duration); logger.info("\nWorker response header:" + PcStringUtils.renderJson(task.getParallelTaskResult())); // logger.info("\nWorker response:" + // PcStringUtils.renderJson(task)); } catch (Exception ex) { logger.error("Exception in testHttpWorkerNormalCheckCancel : " + ex); } }
Example 9
Source File: SshWorkerTest.java From parallec with Apache License 2.0 | 5 votes |
@Test public void testSshWorkerTimeoutException() { ActorRef asyncWorker = null; try { // Start new job // made a timeout int actorMaxOperationTimeoutSec = 0; asyncWorker = ActorConfig.createAndGetActorSystem() .actorOf( Props.create(SshWorker.class, actorMaxOperationTimeoutSec, SshProviderMockTest.sshMetaPassword, hostIpSample2)); final FiniteDuration duration = Duration.create(20, TimeUnit.SECONDS); Future<Object> future = Patterns .ask(asyncWorker, RequestWorkerMsgType.PROCESS_REQUEST, new Timeout(duration)); ResponseOnSingeRequest response = (ResponseOnSingeRequest) Await .result(future, duration); logger.info("\nWorker response:" + response.toString()); } catch (Throwable ex) { logger.error("Exception in test : " + ex); } }
Example 10
Source File: ExecutionManagerTest.java From parallec with Apache License 2.0 | 5 votes |
@Test public void testException() { ActorRef executionManager = null; try { // Start new job ParallelTask task = genParallelTask(); InternalDataProvider adp = InternalDataProvider.getInstance(); adp.genNodeDataMap(task); // fake bad attribute task.setTaskId(null); executionManager = ActorConfig.createAndGetActorSystem().actorOf( Props.create(ExecutionManager.class, task), "executionManager-" + task.getTaskId()); final FiniteDuration duration = Duration.create(20, TimeUnit.SECONDS); // set task as null Future<Object> future = Patterns.ask(executionManager, new InitialRequestToManager(task), new Timeout(duration)); Await.result(future, duration); logger.info("\nWorker response header:" + PcStringUtils.renderJson(task.getParallelTaskResult())); } catch (Exception ex) { logger.error("Exception in testBadMsg : " + ex); } }
Example 11
Source File: HttpWorkerTest.java From parallec with Apache License 2.0 | 5 votes |
/** * fake a bad request */ @Test public void testHttpWorkerCreateRequestException() { ActorRef asyncWorker = null; try { // Start new job int actorMaxOperationTimeoutSec = 15; String urlComplete = "http://www.parallec.io/v**``\"..,++08alidateInternals.html"; asyncWorker = ActorConfig.createAndGetActorSystem().actorOf( Props.create(HttpWorker.class, actorMaxOperationTimeoutSec, HttpClientStore.getInstance() .getCurrentDefaultClient(), urlComplete, HttpMethod.GET, "", null,null)); ; final FiniteDuration duration = Duration.create(20, TimeUnit.SECONDS); Future<Object> future = Patterns .ask(asyncWorker, RequestWorkerMsgType.PROCESS_REQUEST, new Timeout(duration)); ResponseOnSingeRequest response = (ResponseOnSingeRequest) Await .result(future, duration); logger.info("\nWorker response:" + response.toString()); } catch (Throwable ex) { logger.error("Exception in test: " + ex); } }
Example 12
Source File: TcpWorkerTest.java From parallec with Apache License 2.0 | 5 votes |
/** * fake a NPE of logger; do not forget to reset it or other tests will fail. */ @Test public void testTcpWorkerException() { logger.info("IN testTcpWorkerException"); ActorRef asyncWorker = null; try { TcpWorker.setLogger(null); // Start new job int actorMaxOperationTimeoutSec = 15; asyncWorker = ActorConfig.createAndGetActorSystem().actorOf( Props.create(TcpWorker.class, actorMaxOperationTimeoutSec, getTcpMetaSample(), LOCALHOST)); final FiniteDuration duration = Duration.create(20, TimeUnit.SECONDS); Future<Object> future = Patterns .ask(asyncWorker, RequestWorkerMsgType.CANCEL, new Timeout(duration)); ResponseOnSingeRequest response = (ResponseOnSingeRequest) Await .result(future, duration); logger.info("\nWorker response:" + response.toString()); } catch (Throwable ex) { logger.error("Exception in test : " + ex); } TcpWorker.setLogger(LoggerFactory.getLogger(TcpWorker.class)); }
Example 13
Source File: SshWorkerTest.java From parallec with Apache License 2.0 | 5 votes |
@Test public void testSshWorkerBadMsgType() { ActorRef asyncWorker = null; try { // made a timeout int actorMaxOperationTimeoutSec = 15; asyncWorker = ActorConfig.createAndGetActorSystem() .actorOf( Props.create(SshWorker.class, actorMaxOperationTimeoutSec, SshProviderMockTest.sshMetaPassword, hostIpSample2)); final FiniteDuration duration = Duration.create(20, TimeUnit.SECONDS); Future<Object> future = Patterns .ask(asyncWorker, RequestWorkerMsgType.PROCESS_REQUEST, new Timeout(duration)); // test invalid type asyncWorker.tell(new Integer(0), asyncWorker); ResponseOnSingeRequest response = (ResponseOnSingeRequest) Await .result(future, duration); logger.info("\nWorker response:" + response.toString()); } catch (Throwable ex) { logger.error("Exception in test : " + ex); } }
Example 14
Source File: PingWorkerTest.java From parallec with Apache License 2.0 | 5 votes |
@Test public void testDupAndCancel() { ActorRef asyncWorker = null; logger.info("IN testTcpWorkerDupAndCancel"); try { int actorMaxOperationTimeoutSec = 15; asyncWorker = ActorConfig.createAndGetActorSystem().actorOf( Props.create(PingWorker.class, actorMaxOperationTimeoutSec, getPingMetaSample(), "www.google.com")); final FiniteDuration duration = Duration.create(20, TimeUnit.SECONDS); Future<Object> future = Patterns .ask(asyncWorker, RequestWorkerMsgType.PROCESS_REQUEST, new Timeout(duration)); // test dup asyncWorker.tell(RequestWorkerMsgType.PROCESS_REQUEST, asyncWorker); // test cancel asyncWorker.tell(RequestWorkerMsgType.CANCEL, asyncWorker); ResponseOnSingeRequest response = (ResponseOnSingeRequest) Await .result(future, duration); logger.info("\nWorker response:" + response.toString()); // Start new job } catch (Throwable ex) { logger.error("Exception in test : " + ex); } }
Example 15
Source File: HttpWorkerTest.java From parallec with Apache License 2.0 | 5 votes |
@Test public void testHttpWorkerTimeoutException() { ActorRef asyncWorker = null; try { // Start new job // made a timeout int actorMaxOperationTimeoutSec = 0; String urlComplete = "http://www.parallec.io/validateInternals.html"; pc.getHttpClientStore(); asyncWorker = ActorConfig.createAndGetActorSystem().actorOf( Props.create(HttpWorker.class, actorMaxOperationTimeoutSec, HttpClientStore.getInstance() .getCurrentDefaultClient(), urlComplete, HttpMethod.GET, "", null,null)); final FiniteDuration duration = Duration.create(20, TimeUnit.SECONDS); Future<Object> future = Patterns .ask(asyncWorker, RequestWorkerMsgType.PROCESS_REQUEST, new Timeout(duration)); ResponseOnSingeRequest response = (ResponseOnSingeRequest) Await .result(future, duration); logger.info("\nWorker response:" + response.toString()); } catch (Throwable ex) { logger.error("Exception in test : " + ex); } }
Example 16
Source File: TcpWorkerTest.java From parallec with Apache License 2.0 | 5 votes |
@Test public void testTcpWorkerNormalCheckComplete() { ActorRef asyncWorker = null; logger.info("IN testTcpWorkerNormalCheckComplete"); try { // Start new job int actorMaxOperationTimeoutSec = 15; asyncWorker = ActorConfig.createAndGetActorSystem().actorOf( Props.create(TcpWorker.class, actorMaxOperationTimeoutSec, getTcpMetaSample(), LOCALHOST)); final FiniteDuration duration = Duration.create(20, TimeUnit.SECONDS); Future<Object> future = Patterns .ask(asyncWorker, RequestWorkerMsgType.PROCESS_REQUEST, new Timeout(duration)); ResponseOnSingeRequest response = (ResponseOnSingeRequest) Await .result(future, duration); logger.info("\nWorker response:" + response.toString()); } catch (Throwable ex) { logger.error("Exception in test : " + ex); } }
Example 17
Source File: OperationWorker.java From restcommander with Apache License 2.0 | 4 votes |
private final void processRequest() { // the first time dont count. the first time will delay if (!hasBeenDelayed) { // jeff only the first time will pause sender = getSender(); startTimeMillis = System.currentTimeMillis(); validateRequest(request); timeoutDuration = Duration.create( request.getMaxOperationTimeSeconds(), TimeUnit.SECONDS); hasBeenDelayed = true; /** * 20131013 if it is 0; no need to schedule another message. */ if (this.request.getPauseIntervalBeforeSendMillis() != 0L) { long MAX_PAUSE_INTERVAL_MILLIS = 600000L; // 600 sec long pauseIntervalWorkerMillis = Math.min( MAX_PAUSE_INTERVAL_MILLIS, this.request.getPauseIntervalBeforeSendMillis()); getContext() .system() .scheduler() .scheduleOnce( Duration.create(pauseIntervalWorkerMillis, TimeUnit.MILLISECONDS), getSelf(), OperationWorker.MessageType.PROCESS_REQUEST, getContext().system().dispatcher()); return; } } /** * 20130917: change to add uniform target node capability */ final String trueTargetNode = (hostUniform == null) ? host : hostUniform; asyncWorker = getContext().actorOf(new Props(new UntypedActorFactory() { private static final long serialVersionUID = 1L; public Actor create() { final String requestUrl = String.format("%s://%s:%d%s", protocol.toString(), trueTargetNode, agentPort, request.getResourcePath()); return new HttpWorker(client, protocol, requestUrl, request.getHttpMethod(), request.getPostData(), CONTENT_TYPE_JSON, HTTP_MAX_RETRIES, HTTP_RETRY_INTERVAL_MILLIS, httpHeaderType); } })); asyncWorker.tell(HttpWorker.MessageType.PROCESS_REQUEST, getSelf()); // To handle cases where this operation takes extremely long, schedule a // 'timeout' message to be sent to us timeoutMessageCancellable = getContext() .system() .scheduler() .scheduleOnce(timeoutDuration, getSelf(), InternalMessageType.OPERATION_TIMEOUT, getContext().system().dispatcher()); }
Example 18
Source File: AggregationDirector.java From restcommander with Apache License 2.0 | 4 votes |
/** * Here dont need the nodeGroupMetaData.Assuming the request content has * already been ready as in the keys of the hashmap with all the target * nodes. So just need a string to have the nodeGroupType is fine. * * @param nodeGroupType * @param agentCommandType * @param dataStore * @return */ public void sendAggregationCommandToManager(String patternStr, AggregateData aggregateData) { ActorRef aggregationManager = null; try { // Start new job String directorJobUuid = UUID.randomUUID().toString(); models.utils.LogUtils.printLogNormal("!!STARTED sendAggregationCommandToManager : " + directorJobUuid + " at " + DateUtils.getNowDateTimeStr()); // Get the singleton actor system // create the master aggregationManager = ActorConfig.getActorSystem().actorOf( new Props(new UntypedActorFactory() { private static final long serialVersionUID = 1L; public UntypedActor create() { return new AggregationManager(); } }), "AggregationManager-" + UUID.randomUUID().toString()); final FiniteDuration duration = Duration.create( VarUtils.TIMEOUT_ASK_AGGREGATION_MANAGER_SCONDS, TimeUnit.SECONDS); Future<Object> future = Patterns.ask(aggregationManager, new RequestToAggregationManager(patternStr, directorJobUuid, aggregateData), new Timeout( duration)); responseFromAggregationManager = (ResponseFromAggregationManager) Await .result(future, duration); models.utils.LogUtils.printLogNormal("!!COMPLETED sendAggregationCommandToManager : " + directorJobUuid + " at " + DateUtils.getNowDateTimeStr()); } catch (Throwable ex) { models.utils.LogUtils.printLogError ("Exception in sendAggregationCommandToManager : " + ex.getLocalizedMessage()); ex.printStackTrace(); } finally { // stop the manager:agentCommandManager if (aggregationManager != null) { ActorConfig.getActorSystem().stop(aggregationManager); } aggregationManager = null; } }
Example 19
Source File: AkkaTest.java From java-specialagent with Apache License 2.0 | 4 votes |
private static FiniteDuration getDefaultDuration() { return Duration.create(15, "seconds"); }
Example 20
Source File: OperationWorker.java From parallec with Apache License 2.0 | 4 votes |
/** * the 1st request from the manager. */ private final void processMainRequest() { sender = getSender(); startTimeMillis = System.currentTimeMillis(); timeoutDuration = Duration.create( request.getActorMaxOperationTimeoutSec(), TimeUnit.SECONDS); actorMaxOperationTimeoutSec = request.getActorMaxOperationTimeoutSec(); if (request.getProtocol() == RequestProtocol.HTTP || request.getProtocol() == RequestProtocol.HTTPS) { String urlComplete = String.format("%s://%s:%d%s", request .getProtocol().toString(), trueTargetNode, request .getPort(), request.getResourcePath()); // http://stackoverflow.com/questions/1600291/validating-url-in-java if (!PcHttpUtils.isUrlValid(urlComplete.trim())) { String errMsg = "INVALID_URL"; logger.error("INVALID_URL: " + urlComplete + " return.."); replyErrors(errMsg, errMsg, PcConstants.NA, PcConstants.NA_INT); return; } else { logger.debug("url pass validation: " + urlComplete); } asyncWorker = getContext().actorOf( Props.create(HttpWorker.class, actorMaxOperationTimeoutSec, client, urlComplete, request.getHttpMethod(), request.getPostData(), request.getHttpHeaderMap(), request.getResponseHeaderMeta())); } else if (request.getProtocol() == RequestProtocol.SSH ){ asyncWorker = getContext().actorOf( Props.create(SshWorker.class, actorMaxOperationTimeoutSec, request.getSshMeta(), trueTargetNode)); } else if (request.getProtocol() == RequestProtocol.TCP ){ asyncWorker = getContext().actorOf( Props.create(TcpWorker.class, actorMaxOperationTimeoutSec, request.getTcpMeta(), trueTargetNode)); } else if (request.getProtocol() == RequestProtocol.UDP ){ asyncWorker = getContext().actorOf( Props.create(UdpWorker.class, actorMaxOperationTimeoutSec, request.getUdpMeta(), trueTargetNode)); } else if (request.getProtocol() == RequestProtocol.PING ){ asyncWorker = getContext().actorOf( Props.create(PingWorker.class, actorMaxOperationTimeoutSec, request.getPingMeta(), trueTargetNode)); } asyncWorker.tell(RequestWorkerMsgType.PROCESS_REQUEST, getSelf()); cancelExistingIfAnyAndScheduleTimeoutCall(); }