Package org.commoncrawl.async

Examples of org.commoncrawl.async.EventLoop


      _asyncStub = asyncStub;
    }

    protected boolean waitForResult(CountDownLatch latch) throws IOException {

      EventLoop sourceEventLoop = _asyncStub.getEventLoop();
     
      // if called from event thread ...
      if (sourceEventLoop != null && Thread.currentThread() ==
          sourceEventLoop.getEventThread()) {
        // pump events until timeout is reached

        long timeoutTime = System.currentTimeMillis() + getRPCTimeout();

        do {
          sourceEventLoop.waitForIO(1);
        } while (latch.getCount() == 1
            || System.currentTimeMillis() >= timeoutTime);
      } else {
        try {
          latch.await(getRPCTimeout(), TimeUnit.MILLISECONDS);
View Full Code Here


   * @param optionalExecutor
   * @throws IOException
   */
  public InProcessActor(ThreadPoolExecutor optionalExecutor,Events optionalEventListener) throws IOException {
    if (optionalExecutor == null) {
      _eventLoop = new EventLoop();
      _ownsEventLoop = true;
    }
    else {
      _executor = optionalExecutor;
    }
View Full Code Here

  }

  @Test
  public void testServerRPC() throws Exception {

    final EventLoop eventLoop = new EventLoop();

    eventLoop.start();

    RPCTestServer server = new RPCTestServer();

    InetSocketAddress localAddress = new InetSocketAddress("localhost", 0);

    InetSocketAddress address = new InetSocketAddress("localhost", 9000);

    RPCServerChannel channel = new RPCServerChannel(server, eventLoop,
        address, null);

    server.bindActor(channel, RPCTestService.spec,server,null);

    server.start();

    RPCChannel clientChannel = new RPCChannel(eventLoop,null,
        localAddress, address, null);

    clientChannel.open();
   
    AsyncStub stub = new AsyncStub(clientChannel,eventLoop);

    UnitTestStruct1 input = new UnitTestStruct1();

    for (int i = 0; i < 1000; ++i) {

      input.setStringType("hello" + Integer.toString(i));
      input.setIntType(i);

      System.out.println("Sending Request:" + i);
      stub.hello(input, new Callback<UnitTestStruct1, UnitTestStruct1>() {

        public void requestComplete(
            OutgoingMessageContext<UnitTestStruct1, UnitTestStruct1> request) {
          System.out.println("Request returned with status:"
              + request.getStatus().toString());

          if (request.getStatus() == Status.Success) {
            System.out.println("Returned string value is:"
                + request.getOutput().getStringType());

            if (request.getOutput().getIntType() == 999) {
              System.out
                  .println("Got Final Response. Stopping Event Loop from within Callback");
              eventLoop.stop();
            }
          }
        }
      });
      System.out.println("Sent Request:" + i);

    }

    // wait for server to quit ...
    eventLoop.getEventThread().join();
  }
View Full Code Here

      // TODO Auto-generated catch block
      e2.printStackTrace();
    }
   
    try {
      final EventLoop outerEventLoop = new EventLoop();
      outerEventLoop.start();
      final ThreadPoolExecutor targetExecutor = (ThreadPoolExecutor) Executors.newCachedThreadPool();
      final ThreadPoolExecutor sourceExecutor = (ThreadPoolExecutor) Executors.newCachedThreadPool();
      outerEventLoop.queueAsyncCallback(new Callbacks.Callback() {
       
        @Override
        public void execute() {

         
          try {
            final InProcessActor localActor = RPCTestService.InProcessActorFactory.createInProcessActor(new RPCTestService() {

             int responseCount = 0;
            
             @Override
             public void hello(final IncomingMessageContext<UnitTestStruct1, UnitTestStruct1> rpcContext)throws RPCException {
               System.out.println("Actor Received Hello for Message:" + rpcContext.getInput().getIntType() + " Thread:" + Thread.currentThread().getId());
               outerEventLoop.setTimer(new Timer((long)(Math.random() * 10.0),false,new Timer.Callback() {

                @Override
                public void timerFired(Timer timer) {
                  System.out.println("Actor Processing Delayed Hello Response for Message:" + rpcContext.getInput().getIntType() + " Thread:" + Thread.currentThread().getId());
                  responseCount++;
                  rpcContext.getOutput().setIntType(rpcContext.getInput().getIntType());
                  if (responseCount == 100) {
                    System.out.println("Hit 100 Responses. Killing Actor Thread");
                    rpcContext.getOutput().setLongType(1);
                    ((InProcessChannel)rpcContext.getChannel()).getActor().stop();
                  }
                  try {
                    rpcContext.completeRequest();
                  } catch (RPCException e) {
                    e.printStackTrace();
                  }
                 
                }
               }));
              
             }
            
             },targetExecutor,new InProcessActor.Events() {
             
              @Override
              public void onStartup(InProcessActor actor) {
                System.out.println("OnStartup - ThreadId:" + Thread.currentThread().getId());
              }
             
              @Override
              public void onShutdown(InProcessActor actor) {
                System.out.println("OnShutdown- ThreadId:" + Thread.currentThread().getId());
              }
            });
           
            for (int i=0;i<1000;++i) {
              Channel localChannel = null;

              if (i % 2 == 0)
                localChannel = localActor.createChannel(outerEventLoop);
              else
                localChannel = localActor.createChannel(sourceExecutor);
             
              RPCTestService.AsyncStub stub = new RPCTestService.AsyncStub(localChannel,outerEventLoop);
             
              UnitTestStruct1 struct1 = new UnitTestStruct1();
              struct1.setIntType(i);
              System.out.println("Sending Request:" + struct1.getIntType() + " From Thread:" + Thread.currentThread().getId());
              stub.hello(struct1,new Callback<UnitTestStruct1, UnitTestStruct1>() {
                public void requestComplete(org.commoncrawl.rpc.OutgoingMessageContext<UnitTestStruct1,UnitTestStruct1> request) {
                  System.out.println("Received Request Complete for Request:" + request.getRequestId() + " Thread:" + Thread.currentThread().getId());
                 
                  if (request.getOutput().getLongType() == 1) {
                    System.out.println("Shutdown Cmd Received");
                    outerEventLoop.queueAsyncCallback(new Callbacks.Callback() {
                     
                      @Override
                      public void execute() {
                        System.out.println("Killing Outer Event Loop - Thread:" + Thread.currentThread().getId());
                        outerEventLoop.stop();
                        targetExecutor.shutdown();
                        sourceExecutor.shutdown();
                      }
                    });
                  }
View Full Code Here

    // initialize the server address
    _serverAddress = new InetSocketAddress(selectedRPCInterface, rpcPort);

    // init the event loop
    _eventLoop = new EventLoop(registerThreadPool(DNS_POOL_NAME,
        _dnsThreadPoolSize));

    // and the Web Server
    _webServer = new WebServer(this, selectedWebInterface, httpPort, false,
        _useAsyncWebDispatch);
View Full Code Here

  private File getTempDirForQuery(long queryId) {
    return new File(_localTempDir,Long.toString(queryId));
  }
 
  public void renderQueueStats(final JsonWriter jsonWriter)throws IOException {
    EventLoop eventLoop = getHost().getEventLoop();
    final AtomicReference<IOException> exceptionRef = new AtomicReference<IOException>();
    if (Thread.currentThread() != eventLoop.getEventThread()) {
      final Semaphore semaphore = new Semaphore(0);
      eventLoop.queueAsyncCallback(new Callback() {

        @Override
        public void execute() {
          try {
            renderQueueStatsInternal(jsonWriter);
View Full Code Here

  public S3Downloader(String s3BucketName,String s3AccessId,String s3SecretKey, boolean isRequesterPays)throws IOException {
    _s3BucketName = s3BucketName;
    _s3AccessId = s3AccessId;
    _s3SecretKey = s3SecretKey;
    _isRequesterPays = isRequesterPays;
    _eventLoop = new EventLoop();
  }
View Full Code Here

    initialize(listener,null);
  }
  public void initialize(Callback listener,EventLoop externalEventLoop) throws IOException {
   
    if (externalEventLoop == null) {
      _eventLoop = new EventLoop();
      _ownsEventLoop = true;
    }
    else {
      _eventLoop = externalEventLoop;
      _ownsEventLoop = false;
View Full Code Here

TOP

Related Classes of org.commoncrawl.async.EventLoop

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.