Пример #1
0
  public void run() {
    // Initialize the timer that schedules subsequent reconnection attempts.
    final Timer timer = new HashedWheelTimer();

    // Configure the client.
    final ClientBootstrap bootstrap =
        new ClientBootstrap(
            new NioClientSocketChannelFactory(
                Executors.newCachedThreadPool(), Executors.newCachedThreadPool()));

    // Configure the pipeline factory.
    bootstrap.setPipelineFactory(
        new ChannelPipelineFactory() {

          private final ChannelHandler timeoutHandler = new ReadTimeoutHandler(timer, READ_TIMEOUT);
          private final ChannelHandler uptimeHandler = new UptimeClientHandler(bootstrap, timer);

          public ChannelPipeline getPipeline() throws Exception {
            return Channels.pipeline(timeoutHandler, uptimeHandler);
          }
        });

    bootstrap.setOption("remoteAddress", new InetSocketAddress(host, port));

    // Initiate the first connection attempt - the rest is handled by
    // UptimeClientHandler.
    bootstrap.connect();
  }
Пример #2
0
  private synchronized void setThreadPools() {
    if (threadPool != null) {
      return;
    } else if (useGlobalPools) {
      threadPool = getGlobalThreadPool();

      scheduledThreadPool = getGlobalScheduledThreadPool();
    } else {
      this.shutdownPool = true;

      ThreadFactory factory =
          new HornetQThreadFactory(
              "HornetQ-client-factory-threads-" + System.identityHashCode(this),
              true,
              getThisClassLoader());

      if (threadPoolMaxSize == -1) {
        threadPool = Executors.newCachedThreadPool(factory);
      } else {
        threadPool = Executors.newFixedThreadPool(threadPoolMaxSize, factory);
      }

      factory =
          new HornetQThreadFactory(
              "HornetQ-client-factory-pinger-threads-" + System.identityHashCode(this),
              true,
              getThisClassLoader());

      scheduledThreadPool = Executors.newScheduledThreadPool(scheduledThreadPoolMaxSize, factory);
    }
  }
Пример #3
0
  public static void main(String[] args) {
    new ThreadPoolExecutor(
        0,
        10,
        1000,
        TimeUnit.MILLISECONDS,
        new LinkedBlockingQueue<Runnable>(),
        Executors.defaultThreadFactory(),
        new RejectedExecutionHandler() {

          @Override
          public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
            // TODO Auto-generated method stub

          }
        });

    new ThreadPoolExecutor(
        0,
        10,
        1000,
        TimeUnit.MILLISECONDS,
        new LinkedBlockingQueue<Runnable>(10),
        Executors.defaultThreadFactory(),
        new ThreadPoolExecutor.CallerRunsPolicy());
    int numOfProcessors = Runtime.getRuntime().availableProcessors();

    System.out.println(numOfProcessors);
    MyLogger logger = new MyLogger();
    Thread.currentThread().setUncaughtExceptionHandler(logger);
    int i = 10 / 0;
    System.out.println(i);
  }
public class ConcurrentServiceQueue implements Serializable {

  /** */
  private static final long serialVersionUID = 1L;

  private static final BlockingQueue<Runnable> execHandleQueue = new SynchronousQueue<Runnable>();

  private static final BlockingQueue<Runnable> resultHandleQueue = new SynchronousQueue<Runnable>();

  public static final ThreadPoolExecutor execHandleExecutor =
      new ThreadPoolExecutor(
          100,
          1000,
          300,
          TimeUnit.SECONDS,
          execHandleQueue,
          Executors.defaultThreadFactory(),
          new ThreadPoolExecutor.AbortPolicy());

  public static final ThreadPoolExecutor resultHandleExecutor =
      new ThreadPoolExecutor(
          100,
          1000,
          300,
          TimeUnit.SECONDS,
          execHandleQueue,
          Executors.defaultThreadFactory(),
          new ThreadPoolExecutor.AbortPolicy());

  public static final AtomicInteger currentTaskCount = new AtomicInteger(0);

  public static final int maxProcessCount = 600;
}
    public GremlinExecutor create() {
      final BasicThreadFactory threadFactory =
          new BasicThreadFactory.Builder().namingPattern("gremlin-executor-default-%d").build();

      final AtomicBoolean poolCreatedByBuilder = new AtomicBoolean();
      final AtomicBoolean suppliedExecutor = new AtomicBoolean(true);
      final AtomicBoolean suppliedScheduledExecutor = new AtomicBoolean(true);

      final ExecutorService es =
          Optional.ofNullable(executorService)
              .orElseGet(
                  () -> {
                    poolCreatedByBuilder.set(true);
                    suppliedExecutor.set(false);
                    return Executors.newScheduledThreadPool(4, threadFactory);
                  });
      executorService = es;

      final ScheduledExecutorService ses =
          Optional.ofNullable(scheduledExecutorService)
              .orElseGet(
                  () -> {
                    // if the pool is created by the builder and we need another just re-use it,
                    // otherwise create
                    // a new one of those guys
                    suppliedScheduledExecutor.set(false);
                    return (poolCreatedByBuilder.get())
                        ? (ScheduledExecutorService) es
                        : Executors.newScheduledThreadPool(4, threadFactory);
                  });
      scheduledExecutorService = ses;

      return new GremlinExecutor(this, suppliedExecutor.get(), suppliedScheduledExecutor.get());
    }
Пример #6
0
  public static void main(String[] args) throws Exception {
    String host = "localhost";
    int port = Integer.parseInt("8080");

    ChannelFactory factory =
        new NioClientSocketChannelFactory(
            Executors.newCachedThreadPool(), Executors.newCachedThreadPool());

    ClientBootstrap bootstrap = new ClientBootstrap(factory);

    bootstrap.setPipelineFactory(
        new ChannelPipelineFactory() {
          public ChannelPipeline getPipeline() {
            return Channels.pipeline(new TimeDecoder(), new TimeClientHandler());
          }
        });

    bootstrap.setOption("tcpNoDelay", true);
    bootstrap.setOption("keepAlive", true);

    ChannelFuture future = bootstrap.connect(new InetSocketAddress(host, port));
    future.awaitUninterruptibly();
    if (!future.isSuccess()) {
      future.getCause().printStackTrace();
    }
    future.getChannel().getCloseFuture().awaitUninterruptibly();
    factory.releaseExternalResources();
  }
Пример #7
0
 private void start() {
   log.info("ExecutorManager starting...");
   numberOfOrderedTasks = 0;
   scheduler = Executors.newScheduledThreadPool(1, new SchedulerThreadFactory());
   canceller = Executors.newScheduledThreadPool(1, new CancellerThreadFactory());
   nearRealTimeOrderedTask =
       new Runnable() {
         public void run() {
           try {
             UserTasks[] tasksArray = null;
             synchronized (nearRealTimeOrderedTasksMap) {
               if (nearRealTimeOrderedTasksMap.size() > 0) {
                 tasksArray = new UserTasks[nearRealTimeOrderedTasksMap.size()];
                 nearRealTimeOrderedTasksMap.values().toArray(tasksArray);
               }
             }
             if (tasksArray != null) {
               for (int i = 0; i < tasksArray.length; i++) {
                 tasksArray[i].run();
               }
             }
             synchronized (nearRealTimeOrderedTasksMap) {
               if (numberOfOrderedTasks == 0) nearRealTimeMainTaskFuture.cancel(true);
             }
           } catch (Exception e) {
             log.error("nearRealTimeTask run error", e);
           }
         }
       };
 }
Пример #8
0
  @Override
  public void start() {
    logger.info("Starting {}...", this);

    Responder responder = new SpecificResponder(AvroSourceProtocol.class, this);
    if (maxThreads <= 0) {
      server = new NettyServer(responder, new InetSocketAddress(bindAddress, port));
    } else {
      server =
          new NettyServer(
              responder,
              new InetSocketAddress(bindAddress, port),
              new NioServerSocketChannelFactory(
                  Executors.newCachedThreadPool(), Executors.newFixedThreadPool(maxThreads)));
    }
    connectionCountUpdater = Executors.newSingleThreadScheduledExecutor();
    server.start();
    sourceCounter.start();
    super.start();
    final NettyServer srv = (NettyServer) server;
    connectionCountUpdater.scheduleWithFixedDelay(
        new Runnable() {

          @Override
          public void run() {
            sourceCounter.setOpenConnectionCount(Long.valueOf(srv.getNumActiveConnections()));
          }
        },
        0,
        60,
        TimeUnit.SECONDS);

    logger.info("Avro source {} started.", getName());
  }
Пример #9
0
  protected void init() {
    // Configure the client.
    bootstrap =
        new ClientBootstrap(
            new NioClientSocketChannelFactory(
                Executors.newCachedThreadPool(), Executors.newCachedThreadPool()));

    // Set up the event pipeline factory.
    bootstrap.setPipelineFactory(new FileClientPipelineFactory());
    // Options for a new channel
    bootstrap.setOption("tcpNoDelay", true);
    bootstrap.setOption("receiveBufferSize", 1048576);
    ChannelFuture future = bootstrap.connect(new InetSocketAddress("localhost", 8022));
    // Wait until the connection is made successfully.
    future.awaitUninterruptibly(timeout);
    future.addListener(
        new ChannelFutureListener() {

          @Override
          public void operationComplete(ChannelFuture future) throws Exception {
            System.out.println("Connected.");
          }
        });
    if (!future.isSuccess()) {
      future.getCause().printStackTrace();
      bootstrap.releaseExternalResources();
      return;
    }
    channel = future.getChannel();
  }
Пример #10
0
  /** initialization per Storm configuration */
  @SuppressWarnings("rawtypes")
  public void prepare(Map storm_conf) {
    this.storm_conf = storm_conf;
    connections = new HashMap<String, IConnection>();

    // each context will have a single client channel factory
    int maxWorkers =
        Utils.getInt(storm_conf.get(Config.STORM_MESSAGING_NETTY_CLIENT_WORKER_THREADS));
    ThreadFactory bossFactory = new NettyRenameThreadFactory("client" + "-boss");
    ThreadFactory workerFactory = new NettyRenameThreadFactory("client" + "-worker");
    // TODO investigate impact of having one worker
    if (maxWorkers > 0) {
      clientChannelFactory =
          new NioClientSocketChannelFactory(
              Executors.newCachedThreadPool(bossFactory),
              Executors.newCachedThreadPool(workerFactory),
              maxWorkers);
    } else {
      clientChannelFactory =
          new NioClientSocketChannelFactory(
              Executors.newCachedThreadPool(bossFactory),
              Executors.newCachedThreadPool(workerFactory));
    }

    clientScheduleService =
        new HashedWheelTimer(new NettyRenameThreadFactory("client-schedule-service"));
  }
Пример #11
0
  public static void listen() {

    if (!initialized) {

      contextQueues = new ConcurrentHashMap<String, ConcurrentLinkedQueue<AsyncContext>>();

      serverSocketChannelFactory =
          new NioServerSocketChannelFactory(
              Executors.newCachedThreadPool(), Executors.newCachedThreadPool());

      ServerBootstrap serverBootstrap = new ServerBootstrap(serverSocketChannelFactory);

      serverBootstrap.setPipelineFactory(
          new ChannelPipelineFactory() {
            public ChannelPipeline getPipeline() {
              return Channels.pipeline(new LongPollServerHandler());
            }
          });

      serverBootstrap.setOption("child.tcpNoDelay", true);
      serverBootstrap.setOption("child.keepAlive", true);

      serverBootstrap.bind(new InetSocketAddress(CMBProperties.getInstance().getCQSLongPollPort()));

      initialized = true;

      logger.info(
          "event=longpoll_receiver_service_listening port="
              + CMBProperties.getInstance().getCQSLongPollPort());
    }
  }
Пример #12
0
/**
 * 用来执行任务的线程工具,内包含了一个线程池
 *
 * @author xuan
 * @version $Revision: 1.0 $, $Date: 2012-12-4 上午10:05:03 $
 */
public abstract class ThreadUtils {

  /** 初始化的线程数,有待历史的验证,暂时弄4个 */
  public static ExecutorService threadPool = Executors.newFixedThreadPool(4);

  /** 执行延迟任务,类似Timer的效果 */
  public static ScheduledExecutorService scheduleThreadPool = Executors.newScheduledThreadPool(2);

  /**
   * 立即执行任务
   *
   * @param task
   */
  public static void excute(Runnable task) {
    threadPool.execute(task);
  }

  /**
   * 延后执行任务
   *
   * @param task
   * @param delay
   */
  public static void schedule(Runnable task, long delay) {
    scheduleThreadPool.schedule(task, delay, TimeUnit.MILLISECONDS);
  }

  public static void shutdownThreadPool() {
    threadPool.shutdown();
  }

  public static void shutdownScheduleThreadPool() {
    scheduleThreadPool.shutdown();
  }
}
Пример #13
0
  static {
    Executor instance;

    if (Debug.ONE_THREAD_PER_POOL) instance = Executors.newFixedThreadPool(1, new Factory());
    else instance = Executors.newCachedThreadPool(new Factory());

    if (Debug.ENABLED) {
      if (Debug.DISABLE_THREAD_POOL) {
        instance =
            new Executor() {

              @Override
              public void execute(Runnable runnable) {
                direct(runnable);
              }
            };
      } else instance = new Wrapper(instance);
    }

    _executor = instance;

    _scheduler =
        Executors.newScheduledThreadPool(
            1,
            new ThreadFactory() {

              public Thread newThread(Runnable r) {
                Thread thread = new Thread(r);
                thread.setName("ObjectFabric Scheduler");
                thread.setDaemon(true);
                return thread;
              }
            });
  }
Пример #14
0
  @Override
  public void contextInitialized(ServletContextEvent arg0) {
    smsCreatorScheduler = Executors.newSingleThreadScheduledExecutor();
    smsCreatorScheduler.scheduleWithFixedDelay(
        new SmsCreatorThread(),
        ParseUtil.sToL(processSchedulerConfig.get(Constants.PROP_SMS_CREATORL_INIT_DELAY)),
        ParseUtil.sToL(processSchedulerConfig.get(Constants.PROP_SMS_CREATOR_PROC_DELAY)),
        TimeUnit.SECONDS);
    schedulerLogging.info("smsCreatorScheduler  : startup context");

    smsSenderScheduler = Executors.newSingleThreadScheduledExecutor();
    smsSenderScheduler.scheduleWithFixedDelay(
        new SmsSenderThread(),
        ParseUtil.sToL(processSchedulerConfig.get(Constants.PROP_SMS_SENDER_INIT_DELAY)),
        ParseUtil.sToL(processSchedulerConfig.get(Constants.PROP_SMS_SENDER_PROC_DELAY)),
        TimeUnit.MINUTES);
    schedulerLogging.info("smsSenderScheduler  : startup context");

    mailScheduler = Executors.newSingleThreadScheduledExecutor();
    mailScheduler.scheduleWithFixedDelay(
        new SingleMailmanThread(),
        ParseUtil.sToL(processSchedulerConfig.get(Constants.PROP_STANNDARD_MAIL_INIT_DELAY)),
        ParseUtil.sToL(processSchedulerConfig.get(Constants.PROP_STANNDARD_MAIL_PROC_DELAY)),
        TimeUnit.SECONDS);
    schedulerLogging.info("mailScheduler  : startup context");

    mailCreator = Executors.newSingleThreadScheduledExecutor();
    mailCreator.scheduleWithFixedDelay(
        new EmailCreatorThread(),
        ParseUtil.sToL(processSchedulerConfig.get(Constants.PROP_EMAIL_CREATORL_INIT_DELAY)),
        ParseUtil.sToL(processSchedulerConfig.get(Constants.PROP_EMAIL_CREATOR_PROC_DELAY)),
        TimeUnit.SECONDS);
    schedulerLogging.info("mailCreator  : startup context");
  }
  protected synchronized void initializeQueue(String host, String queueName, Integer port)
      throws InterruptedException {
    final String bind = "tcp://" + host + ":" + port;
    _log.warn("binding to " + bind);
    if (_context == null) {
      _context = ZMQ.context(1);
    }
    if (_socket != null) {
      _executorService.shutdownNow();
      _heartbeatService.shutdownNow();
      Thread.sleep(1 * 1000);
      _log.warn("_executorService.isTerminated=" + _executorService.isTerminated());
      _socket.close();
      _executorService = Executors.newFixedThreadPool(1);
      _heartbeatService = Executors.newFixedThreadPool(1);
    }

    _socket = _context.socket(ZMQ.PUB);
    _socket.connect(bind);
    _executorService.execute(new SendThread(_socket, queueName));
    _heartbeatService.execute(new HeartbeatThread(HEARTBEAT_INTERVAL));

    _log.debug("Inference output queue is sending to " + bind);
    _initialized = true;
  }
Пример #16
0
 /** Run the command from an executor */
 private void runExecutor() {
   // Unlock Mode Codec
   try {
     session.getDataConn().getDataNetworkHandler().unlockModeCodec();
   } catch (FtpNoConnectionException e) {
     setTransferAbortedFromInternal(false);
     return;
   }
   // Run the command
   if (executorService == null) {
     executorService = Executors.newSingleThreadExecutor();
   }
   endOfCommand = new WaarpFuture(true);
   final ArrayList<Callable<Object>> tasks = new ArrayList<Callable<Object>>(1);
   tasks.add(Executors.callable(new FtpTransferExecutor(session, executingCommand)));
   try {
     executorService.invokeAll(tasks);
   } catch (InterruptedException e1) {
   }
   // XXX TRY FIX TO IMPROVE
   /*
   executorService.execute(new FtpTransferExecutor(session,
   	executingCommand));
   	*/
   try {
     commandFinishing.await();
   } catch (InterruptedException e) {
   }
 }
Пример #17
0
 public static MasterNodeService getMasterNodeService(Config config)
     throws IOException, RpcException, XMPPException {
   if (null != master) {
     return master;
   }
   RPC rpc = null;
   if (config.getClient2ServerConnectionMode().equals(ConnectionMode.HTTP2GAE)) {
     rpc = ClientUtils.createHttpRPC(Executors.newCachedThreadPool());
     int oldtimeout = rpc.getSessionManager().getSessionTimeout();
     // wait 2s to retrieve remote object reference
     rpc.getSessionManager().setSessionTimeout(20000);
     try {
       master =
           rpc.getRemoteService(
               MasterNodeService.class,
               MasterNodeService.NAME,
               ClientUtils.createHttpServerAddress(Constants.MASTER_APPID));
     } catch (Rpctimeout e) {
       // TODO: handle exception
     } finally {
       rpc.getSessionManager().setSessionTimeout(oldtimeout);
     }
   } else {
     if (null != config.getXmppAccounts()) {
       XmppAccount account = config.getXmppAccounts().get(0);
       rpc = ClientUtils.createXmppRPC(account, Executors.newCachedThreadPool());
       master =
           rpc.getRemoteService(
               MasterNodeService.class,
               MasterNodeService.NAME,
               ClientUtils.createXmppAddress(Constants.MASTER_APPID));
     }
   }
   return master;
 }
Пример #18
0
  public MessagingServer(MessageReceiptCallback cb, int port) {
    this.messagingServerHandler = new MessagingServerHandler(cb);
    this.port = port;

    // Start server with Nb of active threads = 2*NB CPU + 1 as maximum.
    ChannelFactory factory =
        new NioServerSocketChannelFactory(
            Executors.newCachedThreadPool(),
            Executors.newCachedThreadPool(),
            Runtime.getRuntime().availableProcessors() * 2 + 1);

    bootstrap = new ServerBootstrap(factory);
    // Create the global ChannelGroup
    channelGroup = new DefaultChannelGroup(MessagingServer.class.getName());
    // 200 threads max, Memory limitation: 1MB by channel, 1GB global, 100 ms of timeout
    OrderedMemoryAwareThreadPoolExecutor pipelineExecutor =
        new OrderedMemoryAwareThreadPoolExecutor(
            200, 1048576, 1073741824, 100, TimeUnit.MILLISECONDS, Executors.defaultThreadFactory());

    // We need to use a pipeline factory because we are using stateful handlers
    bootstrap.setPipelineFactory(
        new ChannelPipelineFactory() {
          @Override
          public ChannelPipeline getPipeline() throws Exception {
            return Channels.pipeline(
                new ObjectDecoder(getMaxObjectSize()), new ObjectEncoder(), messagingServerHandler);
          }
        });
    bootstrap.setOption("child.tcpNoDelay", true);
    bootstrap.setOption("child.keepAlive", true);
    bootstrap.setOption("child.reuseAddress", true);
    bootstrap.setOption("child.connectTimeoutMillis", 100);
    bootstrap.setOption("readWriteFair", true);
  }
Пример #19
0
  @Override
  public void init(SpoutApplication args) {
    boolean inJar = false;

    try {
      CodeSource cs = SpoutClient.class.getProtectionDomain().getCodeSource();
      inJar = cs.getLocation().toURI().getPath().endsWith(".jar");
    } catch (URISyntaxException e) {
      e.printStackTrace();
    }

    if (inJar) {
      unpackLwjgl();
    }
    ExecutorService executorBoss =
        Executors.newCachedThreadPool(new NamedThreadFactory("SpoutServer - Boss", true));
    ExecutorService executorWorker =
        Executors.newCachedThreadPool(new NamedThreadFactory("SpoutServer - Worker", true));
    ChannelFactory factory = new NioClientSocketChannelFactory(executorBoss, executorWorker);
    bootstrap.setFactory(factory);

    ChannelPipelineFactory pipelineFactory = new CommonPipelineFactory(this, true);
    bootstrap.setPipelineFactory(pipelineFactory);
    super.init(args);

    getScheduler().startRenderThread();
  }
Пример #20
0
 public Future<Integer> execute() {
   if (executor == null) executor = Executors.newFixedThreadPool(4);
   if (executor.isShutdown()) executor = Executors.newFixedThreadPool(4);
   ExecutorCompletionService<Integer> commandExecutor =
       new ExecutorCompletionService<Integer>(executor);
   return commandExecutor.submit(this);
 }
Пример #21
0
  private void setUpReceiver() throws IOException {
    ChannelFactory factory =
        new NioServerSocketChannelFactory(
            Executors.newCachedThreadPool(), Executors.newCachedThreadPool());
    bootstrap = new ServerBootstrap(factory);
    bootstrap.setPipelineFactory(
        new ChannelPipelineFactory() {

          @Override
          public ChannelPipeline getPipeline() throws Exception {
            return Channels.pipeline(
                new SimpleChannelHandler() {
                  @Override
                  public void messageReceived(ChannelHandlerContext ctx, MessageEvent e)
                      throws Exception {
                    readMessage(e);
                  }

                  @Override
                  public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e)
                      throws Exception {
                    LOG.error("error caught!", e.getCause());
                  }
                });
          }
        });

    //		listener = ServerSocketChannel.open();
    //		listener.socket().bind(new InetSocketAddress(listenPort));
    listenerChannel = bootstrap.bind(new InetSocketAddress(listenPort));
    //		new ListenerThread().start();
  }
Пример #22
0
  public static void startSystem() throws ExecutionException, InterruptedException {

    // Create the entry gates and link them to the parking lot
    for (int i = 0; i < numOfEntries; i++) {
      entries.add(new Entry("entry" + i, parkingLot));
    }
    parkingLot.setEntries(entries);

    // Create the exit gates and link them to the parking lot
    for (int k = 0; k < numOfExits; k++) {
      exits.add(new Exit("exit" + k, parkingLot));
    }
    parkingLot.setExits(exits);

    // Create a thread pool based on the number of entries and exits for this lot
    ScheduledExecutorService scheduledExecutorService1 =
        Executors.newScheduledThreadPool(numOfEntries);
    ScheduledExecutorService scheduledExecutorService2 =
        Executors.newScheduledThreadPool(numOfExits);

    // Schedule the threads, which will act as Car objects entering and exiting the parking lot
    ScheduledFuture<?> scheduledFuture1 =
        scheduledExecutorService1.scheduleWithFixedDelay(
            new Car(entries, exits, 0), firstCarEntryTime, enteringCarsDelay, TimeUnit.SECONDS);

    ScheduledFuture<?> scheduledFuture2 =
        scheduledExecutorService2.scheduleWithFixedDelay(
            new Car(entries, exits, 1), firstCarExitTime, exitingCarsDelay, TimeUnit.SECONDS);

    scheduledFuture1.get();
    scheduledFuture2.get();
    scheduledExecutorService1.shutdown();
    scheduledExecutorService2.shutdown();
  }
  private int[] computeClusterNumberRange(int min, int max, int hop, Optimizer optimizer)
      throws ExecutionException, InterruptedException {
    ExecutorService executor;
    if (parallelWorkers > 1) {
      executor = Executors.newFixedThreadPool(2);
    } else {
      executor = Executors.newFixedThreadPool(1);
    }
    ConcurrentMap<Integer, Double> sharedScores = new ConcurrentHashMap<>();
    SearchSpaceBoundaryFinder_old upperBoundFinder =
        new SearchSpaceUpperBoundaryFinder_old(min, max, hop, optimizer, sharedScores);
    Future<Integer> futureUpperBound = executor.submit(upperBoundFinder);
    SearchSpaceBoundaryFinder_old lowerBoundFinder =
        new SearchSpaceLowerBoundaryFinder_old(min, max, hop, optimizer, sharedScores);
    Future<Integer> futureLowerBound = executor.submit(lowerBoundFinder);
    executor.shutdown();
    int[] r = new int[2];
    Integer realMin = futureLowerBound.get();
    Integer realMax = futureUpperBound.get();
    r[0] = realMin == null ? -1 : realMin;
    r[1] = realMax == null ? -1 : realMax;

    scores.putAll(lowerBoundFinder.getSplitsAndScores());
    // scores.putAll(upperBoundFinder.getSplitsAndScores());
    return r;
  }
Пример #24
0
  public void run() {
    // Configure the client.
    ClientBootstrap bootstrap =
        new ClientBootstrap(
            new NioClientSocketChannelFactory(
                Executors.newCachedThreadPool(), Executors.newCachedThreadPool()));

    // Set up the event pipeline factory.
    bootstrap.setPipelineFactory(new FactorialClientPipelineFactory(count));

    // Make a new connection.
    ChannelFuture connectFuture = bootstrap.connect(new InetSocketAddress(host, port));

    // Wait until the connection is made successfully.
    Channel channel = connectFuture.awaitUninterruptibly().getChannel();

    // Get the handler instance to retrieve the answer.
    FactorialClientHandler handler = (FactorialClientHandler) channel.getPipeline().getLast();

    // Print out the answer.
    System.err.format("Factorial of %,d is: %,d", count, handler.getFactorial());

    // Shut down all thread pools to exit.
    bootstrap.releaseExternalResources();
  }
Пример #25
0
  /** Start TCP socket for Graphite Carbon events */
  public void startListenerCarbonGraphite() {
    NioServerSocketChannelFactory nioServerSocketChannelFactory =
        new NioServerSocketChannelFactory(
            Executors.newCachedThreadPool(), Executors.newCachedThreadPool());
    ServerBootstrap bootstrap = new ServerBootstrap(nioServerSocketChannelFactory);

    // Configure the pipeline factory.
    bootstrap.setPipelineFactory(
        new ChannelPipelineFactory() {

          @Override
          public ChannelPipeline getPipeline() throws Exception {
            return Channels.pipeline(
                new DelimiterBasedFrameDecoder(80960, Delimiters.lineDelimiter()),
                new StringDecoder(CharsetUtil.UTF_8),
                new CarbonGraphiteServiceHandler(chartService));
          }
        });

    // bootstrap.setOption("child.sendBufferSize", 1048576);
    // bootstrap.setOption("child.receiveBufferSize", 1048576);
    // bootstrap.setOption("child.tcpNoDelay", true);
    // bootstrap.setOption("child.writeBufferLowWaterMark", 32 * 1024);
    // bootstrap.setOption("child.writeBufferHighWaterMark", 64 * 1024);

    logger.info("Listening Carbon/Graphite events on port " + getPortCarbon());
    Channel channel = bootstrap.bind(new InetSocketAddress(getPortCarbon()));
    channels.add(channel);
  }
 protected void applyConfiguration() {
   if (configuration.multipleThreads == false) {
     executors = Executors.newSingleThreadExecutor();
   } else {
     executors = Executors.newFixedThreadPool(configuration.maxNoOfThreads);
   }
 }
  @Override
  public void start(StartContext context) throws StartException {
    final ExecutorService executorService = getExecutorServiceInjector().getValue();
    this.hostControllerConfigurationPersister =
        new HostControllerConfigurationPersister(
            environment, hostControllerInfo, executorService, extensionRegistry);
    setConfigurationPersister(hostControllerConfigurationPersister);
    prepareStepHandler.setExecutorService(executorService);
    ThreadFactory threadFactory =
        new JBossThreadFactory(
            new ThreadGroup("proxy-threads"),
            Boolean.FALSE,
            null,
            "%G - %t",
            null,
            null,
            doPrivileged(GetAccessControlContextAction.getInstance()));
    proxyExecutor = Executors.newCachedThreadPool(threadFactory);
    ThreadFactory pingerThreadFactory =
        new JBossThreadFactory(
            new ThreadGroup("proxy-pinger-threads"),
            Boolean.TRUE,
            null,
            "%G - %t",
            null,
            null,
            doPrivileged(GetAccessControlContextAction.getInstance()));
    pingScheduler = Executors.newScheduledThreadPool(PINGER_POOL_SIZE, pingerThreadFactory);

    super.start(context);
  }
Пример #28
0
  public void testStartStop() throws Exception {
    BufferHandler handler =
        new BufferHandler() {
          public void bufferReceived(final Object connectionID, final HornetQBuffer buffer) {}
        };
    Map<String, Object> params = new HashMap<String, Object>();
    ConnectionLifeCycleListener listener =
        new ConnectionLifeCycleListener() {
          public void connectionException(final Object connectionID, final HornetQException me) {}

          public void connectionDestroyed(final Object connectionID) {}

          public void connectionCreated(final Connection connection, final ProtocolType protocol) {}

          public void connectionReadyForWrites(Object connectionID, boolean ready) {}
        };

    NettyConnector connector =
        new NettyConnector(
            params,
            handler,
            listener,
            Executors.newCachedThreadPool(),
            Executors.newCachedThreadPool(),
            Executors.newScheduledThreadPool(5));

    connector.start();
    Assert.assertTrue(connector.isStarted());
    connector.close();
    Assert.assertFalse(connector.isStarted());
  }
Пример #29
0
  @Override
  public synchronized NettyWebServer start() {
    if (isRunning()) {
      throw new IllegalStateException("Server already started.");
    }

    // Configure the server.
    bootstrap = new ServerBootstrap();

    // Set up the event pipeline factory.
    bootstrap.setPipelineFactory(
        new ChannelPipelineFactory() {
          @Override
          public ChannelPipeline getPipeline() throws Exception {
            long timestamp = timestamp();
            Object id = nextId();
            ChannelPipeline pipeline = pipeline();
            pipeline.addLast("staleconnectiontracker", staleConnectionTrackingHandler);
            pipeline.addLast("connectiontracker", connectionTrackingHandler);
            pipeline.addLast("flashpolicydecoder", new FlashPolicyFileDecoder(getPort()));
            pipeline.addLast(
                "decoder",
                new HttpRequestDecoder(maxInitialLineLength, maxHeaderSize, maxChunkSize));
            pipeline.addLast("aggregator", new HttpChunkAggregator(maxContentLength));
            pipeline.addLast("decompressor", new HttpContentDecompressor());
            pipeline.addLast("encoder", new HttpResponseEncoder());
            pipeline.addLast("compressor", new HttpContentCompressor());
            pipeline.addLast(
                "handler",
                new NettyHttpChannelHandler(
                    executor, handlers, id, timestamp, exceptionHandler, ioExceptionHandler));
            return pipeline;
          }
        });

    staleConnectionTrackingHandler =
        new StaleConnectionTrackingHandler(staleConnectionTimeout, executor);
    ScheduledExecutorService staleCheckExecutor = Executors.newSingleThreadScheduledExecutor();
    staleCheckExecutor.scheduleWithFixedDelay(
        new Runnable() {
          @Override
          public void run() {
            staleConnectionTrackingHandler.closeStaleConnections();
          }
        },
        staleConnectionTimeout / 2,
        staleConnectionTimeout / 2,
        TimeUnit.MILLISECONDS);
    executorServices.add(staleCheckExecutor);

    connectionTrackingHandler = new ConnectionTrackingHandler();
    ExecutorService bossExecutor = Executors.newSingleThreadExecutor();
    executorServices.add(bossExecutor);
    ExecutorService workerExecutor = Executors.newSingleThreadExecutor();
    executorServices.add(workerExecutor);
    bootstrap.setFactory(new NioServerSocketChannelFactory(bossExecutor, workerExecutor, 1));
    channel = bootstrap.bind(socketAddress);
    return this;
  }
Пример #30
0
  @Test
  public void testWriteALotOfUsers() throws Exception {
    int userCount = 40;

    ExecutorService pool = Executors.newCachedThreadPool();
    List<Future<?>> createFutures = new ArrayList<Future<?>>();
    for (int i = 0; i < userCount; i++) {
      final String userName = "******" + i;
      createFutures.add(
          pool.submit(
              Executors.callable(
                  new Runnable() {
                    @Override
                    public void run() {
                      try {
                        createUser(userName, userName + "@opennms.org");
                      } catch (Exception e) {
                        e.printStackTrace();
                        fail(e.getMessage());
                      }
                    }
                  })));
    }

    // Wait for all of the REST operations to complete
    for (Future<?> future : createFutures) {
      future.get();
    }

    // validate list
    OnmsUserList users = JaxbUtils.unmarshal(OnmsUserList.class, sendRequest(GET, "/users", 200));
    assertNotNull(users);
    assertEquals(
        userCount + 1,
        users.size()); // +1 because user "admin" is there before creating all the new users.

    // Try changing the password for every user to make sure that they
    // are properly accessible in the UserManager
    for (int i = 0; i < userCount; i++) {
      // validate each created user
      String xml = sendRequest(GET, "/users/test" + i, 200);
      OnmsUser eachUser = JaxbUtils.unmarshal(OnmsUser.class, xml);
      assertEquals("test" + i, eachUser.getUsername());
      assertEquals("test" + i + " Full Name", eachUser.getFullName());
      assertEquals("test" + i + "@opennms.org", eachUser.getEmail());
      assertEquals("Autogenerated by a unit test...", eachUser.getComments());

      // change
      sendPut("/users/test" + i, "password=MONKEYS&[email protected]", 303, "/users/test" + i);

      // validate change of password
      eachUser = JaxbUtils.unmarshal(OnmsUser.class, sendRequest(GET, "/users/test" + i, 200));
      User castorUser = getWebAppContext().getBean(UserManager.class).getUser("test" + i);
      assertEquals(castorUser.getPassword().getContent(), "MONKEYS");

      // validate change of email
      assertEquals("*****@*****.**", eachUser.getEmail());
    }
  }