public static void main(String[] args) throws MalformedURLException {
    ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
    /*
    UserService userService = (UserService) context.getBean("userService");
    User user = new User();
    user.setAge(25);
    user.setName("gaohuan");
    userService.save(user);
    */

    UserBusiness userBusiness = context.getBean(UserBusiness.class);

    ThreadPoolTaskExecutor poolTaskExecutor = new ThreadPoolTaskExecutor();
    // 线程池所使用的缓冲队列
    poolTaskExecutor.setQueueCapacity(200);
    // 线程池维护线程的最少数量
    poolTaskExecutor.setCorePoolSize(10);
    // 线程池维护线程的最大数量
    poolTaskExecutor.setMaxPoolSize(1000);
    // 线程池维护线程所允许的空闲时间
    poolTaskExecutor.setKeepAliveSeconds(30000);
    poolTaskExecutor.initialize();

    // 测试1
    for (int i = 0; i < 10; i++) {
      poolTaskExecutor.execute(
          new Runnable() {
            @Override
            public void run() {
              for (int j = 0; j < 10; j++) {
                User user = new User("高欢-" + j, 25);
                BusinessResult<String> result = userBusiness.signIn(user);
              }
            }
          });
    }

    for (int i = 0; i < 10; i++) {
      poolTaskExecutor.execute(
          new Runnable() {
            @Override
            public void run() {
              for (int j = 0; j < 2; j++) {
                userBusiness.signIn(null);
              }
            }
          });
    }

    System.out.println("-------------运行成功---------------");
    //        System.exit(1);
  }
  /**
   * Returns a configured instance of {@code AsyncRestTemplate} used to retrieve repository source
   * files from Stash.
   *
   * @return
   */
  @Bean(name = "fileAsyncRestTemplate")
  AsyncRestTemplate fileAsyncRestTemplate() {

    ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
    executor.setCorePoolSize(maxThreads);
    executor.setMaxPoolSize(maxThreads);
    executor.initialize();

    AsyncRestTemplate template = new AsyncRestTemplate(executor);
    template.setErrorHandler(
        new StatusCheckingResponseErrorHandler() {

          @Override
          public void handleClientError(HttpStatus statusCode) {

            if (HttpStatus.NOT_FOUND.equals(statusCode)) {
              throw new RestClientException(
                  messageSource.getMessage(
                      MESSAGE_KEY_NOT_FOUND, null, "Not Found", LocaleContextHolder.getLocale()));
            }

            if (HttpStatus.UNAUTHORIZED.equals(statusCode)) {
              throw new RestClientException(
                  messageSource.getMessage(
                      MESSAGE_KEY_FILE_UNAUTHORIZED,
                      null,
                      "Unauthorized or Not Found",
                      LocaleContextHolder.getLocale()));
            }
          }
        });

    return template;
  }
Example #3
0
 /**
  * Get a task executor for executing tasks asynchronously that don't need to be scheduled at a
  * recurring rate.
  *
  * @param poolSize The number of threads desired for this system. Likely best to do one more than
  *     number of CPUs
  * @return The task executor the system to use
  */
 @Bean
 public AsyncTaskExecutor taskExecutor(
     @Value("${genie.tasks.executor.pool.size:1}") final int poolSize) {
   final ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
   executor.setCorePoolSize(poolSize);
   return executor;
 }
  public void testConcurrentPipeline() throws Exception {
    int total = 200;
    final int group = total / 20;
    MockEndpoint mock = getMockEndpoint("mock:result");
    mock.expectedMessageCount(total);

    ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
    executor.setCorePoolSize(20);
    executor.afterPropertiesSet();
    for (int i = 0; i < 20; i++) {
      final int threadCount = i;
      executor.execute(
          new Runnable() {
            public void run() {
              int start = threadCount * group;
              for (int i = 0; i < group; i++) {
                try {
                  // do some random sleep to simulate spread in user activity
                  Thread.sleep(new Random().nextInt(10));
                } catch (InterruptedException e) {
                  // ignore
                }
                template.sendBody(uri, "" + (start + i));
              }
            }
          });
    }

    mock.assertIsSatisfied();
    mock.expectsNoDuplicates(body());
    executor.shutdown();
  }
Example #5
0
 /** 执行一个服务器的调度 */
 public void executeOneServer(Date date, Integer sysNum) {
   ServerTask task = new ServerTask(date, sysNum);
   ThreadPoolTaskExecutor executer =
       (ThreadPoolTaskExecutor)
           ServiceCacheFactory.getServiceCache().getBeanById("dayTaskExecutor");
   executer.execute(task);
 }
 @Override
 public Executor getAsyncExecutor() {
   ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
   executor.setThreadNamePrefix("Custom-");
   executor.initialize();
   return executor;
 }
 @Override
 public Executor getAsyncExecutor() {
   ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
   executor.setCorePoolSize(Runtime.getRuntime().availableProcessors());
   executor.setMaxPoolSize(Runtime.getRuntime().availableProcessors());
   executor.setThreadNamePrefix("Spring-Async-");
   executor.initialize();
   return executor;
 }
 @Bean(name = "rabbitCleanupThreadPool")
 public ThreadPoolTaskExecutor rabbitCleanupThreadPool() {
   ThreadPoolTaskExecutor t = new ThreadPoolTaskExecutor();
   t.setCorePoolSize(2);
   t.setMaxPoolSize(3);
   t.setQueueCapacity(3);
   t.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
   return t;
 }
  private void testExecutor(
      String channelName, int corePoolSize, int maxPoolSize, int keepAliveSeconds) {

    ThreadPoolTaskExecutor taskExecutor =
        this.appContext.getBean(channelName + "Executor", ThreadPoolTaskExecutor.class);

    assertEquals(corePoolSize, taskExecutor.getCorePoolSize());
    assertEquals(maxPoolSize, taskExecutor.getMaxPoolSize());
    assertEquals(keepAliveSeconds, taskExecutor.getKeepAliveSeconds());
  }
Example #10
0
 @Override
 public void onApplicationEvent(ApplicationEvent event) {
   if (event instanceof ContextRefreshedEvent) {
     taskExecutor = new ThreadPoolTaskExecutor();
     taskExecutor.setMaxPoolSize(100);
     taskExecutor.initialize();
   } else if (event instanceof ContextClosedEvent) {
     taskExecutor.shutdown();
   }
 }
Example #11
0
 @Override
 @Bean(name = "taskExecutor")
 public Executor getAsyncExecutor() {
   log.debug("Creating Async Task Executor");
   ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
   executor.setCorePoolSize(jHipsterProperties.getAsync().getCorePoolSize());
   executor.setMaxPoolSize(jHipsterProperties.getAsync().getMaxPoolSize());
   executor.setQueueCapacity(jHipsterProperties.getAsync().getQueueCapacity());
   executor.setThreadNamePrefix("nailit-Executor-");
   return new ExceptionHandlingAsyncTaskExecutor(executor);
 }
Example #12
0
 @Override
 @Bean(name = "taskExecutor")
 public Executor getAsyncExecutor() {
   log.debug("Creating Async Task Executor");
   ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
   executor.setCorePoolSize(applicationProperties.getAsync().getCorePoolSize());
   executor.setMaxPoolSize(applicationProperties.getAsync().getMaxPoolSize());
   executor.setQueueCapacity(applicationProperties.getAsync().getQueueCapacity());
   executor.setThreadNamePrefix("ppwebtv-Executor-");
   return ExceptionHandlingAsyncTaskExecutor.of(executor);
 }
Example #13
0
 @Override
 @Bean
 public Executor getAsyncExecutor() {
   log.debug("Creating Async Task Executor");
   ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
   executor.setCorePoolSize(propertyResolver.getProperty("corePoolSize", Integer.class, 2));
   executor.setMaxPoolSize(propertyResolver.getProperty("maxPoolSize", Integer.class, 50));
   executor.setQueueCapacity(propertyResolver.getProperty("queueCapacity", Integer.class, 10000));
   executor.setThreadNamePrefix("milkyway-Executor-");
   return new ExceptionHandlingAsyncTaskExecutor(executor);
 }
Example #14
0
 private void doLogin(
     HttpServletRequest request,
     HttpServletResponse response,
     final long uid,
     final long tpId,
     RunType runType,
     boolean persistent)
     throws PassportAccountException {
   Passport passport = passportMapper.selectByPrimaryKey(uid);
   if (null == passport) {
     log.error("Login error. Can not find passport[id=" + uid + "].");
   }
   Date shield = passport.getShieldTime();
   if (shield != null && shield.getTime() > System.currentTimeMillis()) {
     throw new PassportAccountException(PassportAccountException.USER_IS_SHIELD, shield.getTime());
   }
   loginSessionManager.login(request, response, uid, tpId, false, persistent);
   // 更新最后登录时间
   updateLastLoginTime(uid, runType);
   // updateOnlineState(uid);
   addLoginLog(request, uid);
   // 启动一个线程来获取和保存
   if (tpId > 0) {
     taskExecutor.execute(
         new Runnable() {
           @Override
           public void run() {
             // friendService.updateExpiredFriends(uid, tpId);
             userStatusService.updateUserStatus(uid, tpId);
           }
         });
   }
 }
Example #15
0
 public static void audit(Audit audit) {
   HttpServletRequest servletRequest =
       ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
   int buId = ((UserSession) servletRequest.getSession().getAttribute("userSession")).getBuId();
   audit.setBuId(buId);
   taskExecutor.execute(new AuditWorker(audit));
 }
  private Map<Future<RenderResult>, ComponentRenderer> executeRenderers(
      List<RenderableComponent> renderableComponents,
      Map<String, Object> globalContext,
      String pageUrl) {

    Map<Future<RenderResult>, ComponentRenderer> renderableFutures =
        new LinkedHashMap<Future<RenderResult>, ComponentRenderer>(renderableComponents.size());

    int index = 0;

    for (RenderableComponent renderableComponent : renderableComponents) {

      StringBuilder logTag = new StringBuilder("Renderer ");
      logTag.append(index).append(" - page: ").append(pageUrl);

      ComponentRenderer componentRenderer =
          componentRendererFactory.getComponentRenderer(
              renderableComponent, globalContext, logTag.toString());

      Future<RenderResult> future = threadPoolTaskExecutor.submit(componentRenderer);
      renderableFutures.put(future, componentRenderer);

      index++;
    }

    return renderableFutures;
  }
  @Bean
  @Qualifier("dnsClientHttpRequestFactory")
  public SimpleClientHttpRequestFactory dnsClientHttpRequestFactory() {
    SimpleClientHttpRequestFactory clientHttpRequestFactory = new SimpleClientHttpRequestFactory();

    // Set timeouts (1 second to connect, unlimited - for response)
    clientHttpRequestFactory.setConnectTimeout(500);
    clientHttpRequestFactory.setReadTimeout(1000);

    // Setting the ThreadPoolTaskExecutor for the Async calls
    ThreadPoolTaskExecutor pool = new ThreadPoolTaskExecutor();
    pool.setCorePoolSize(5);
    pool.setMaxPoolSize(10);
    pool.setWaitForTasksToCompleteOnShutdown(true);
    pool.initialize();

    // Setting the TaskExecutor to the ThreadPoolTaskExecutor
    clientHttpRequestFactory.setTaskExecutor(pool);

    return clientHttpRequestFactory;
  }
Example #18
0
  @Override
  public Executor getAsyncExecutor() {
    ThreadPoolTaskExecutor scheduler = new ThreadPoolTaskExecutor();
    scheduler.setCorePoolSize(20);
    scheduler.setMaxPoolSize(30);
    scheduler.setThreadNamePrefix("async-task-");
    scheduler.setAwaitTerminationSeconds(60);
    scheduler.setWaitForTasksToCompleteOnShutdown(true);

    scheduler.initialize();

    return scheduler;
  }
 @ManagedOperation
 public void stopExecutors() {
   if (logger.isDebugEnabled()) {
     logger.debug("Stopping executors" + (this.shutdownForced ? "(force)" : ""));
   }
   List<ExecutorService> executorServices = new ArrayList<ExecutorService>();
   Map<String, ThreadPoolTaskExecutor> executors =
       this.applicationContext.getBeansOfType(ThreadPoolTaskExecutor.class);
   for (Entry<String, ThreadPoolTaskExecutor> entry : executors.entrySet()) {
     ThreadPoolTaskExecutor executor = entry.getValue();
     if (executor == this.shutdownExecutor) {
       logger.debug("Skipping shutdown of shutdown executor");
     } else {
       if (logger.isInfoEnabled()) {
         logger.info("Stopping executor " + executor.getThreadNamePrefix());
       }
       ExecutorService executorService = executor.getThreadPoolExecutor();
       executorServices.add(executorService);
       doShutdownExecutorService(executorService);
     }
   }
   waitForExecutors(executorServices);
   logger.debug("Stopped executors");
 }
  @Override
  @Transactional(readOnly = false)
  public void pollScheduledUpdates() {
    if (isShuttingDown) {
      StringBuilder sb =
          new StringBuilder(
                  "module=updateQueue component=pollScheduledUpdates" + " action=updateConnector")
              .append(" message=\"Service is shutting down... Stopping Task Queue polling...\"");
      logger.warn(sb.toString());
      return;
    }
    List<UpdateWorkerTask> updateWorkerTasks =
        JPAUtils.find(
            em,
            UpdateWorkerTask.class,
            "updateWorkerTasks.byStatus",
            Status.SCHEDULED,
            System.currentTimeMillis());
    if (updateWorkerTasks.size() == 0) {
      logger.debug(
          "module=updateQueue component=connectorUpdateService action=pollScheduledUpdates message=\"Nothing to do\"");
      return;
    }
    for (UpdateWorkerTask updateWorkerTask : updateWorkerTasks) {
      logger.info(
          "module=updateQueue component=connectorUpdateService action=pollScheduledUpdates"
              + " message=\"Executing update: "
              + " \""
              + updateWorkerTask);
      setUpdateWorkerTaskStatus(updateWorkerTask.getId(), Status.IN_PROGRESS);

      // TODO: re-think this through
      // retrieve updater for the worker
      // find out wether such an update task is already running
      // if not create the worker
      // let the updater know about the worker
      // execute the worker

      UpdateWorker updateWorker = beanFactory.getBean(UpdateWorker.class);
      updateWorker.task = updateWorkerTask;
      try {
        executor.execute(updateWorker);
      } catch (Throwable t) {
        t.printStackTrace();
      }
    }
  }
 @Override
 public Executor getAsyncExecutor() {
   logger.debug("Enter: getAsynchExecutor");
   ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
   executor.setCorePoolSize(7);
   executor.setMaxPoolSize(42);
   executor.setQueueCapacity(11);
   executor.setThreadNamePrefix("AsyncExecutor-");
   executor.initialize();
   logger.debug("Exit: getAsynchExecutor");
   return executor;
 }
Example #22
0
  @Bean
  public ThreadPoolTaskExecutor taskExecutor() {

    ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();

    executor.setCorePoolSize(4);
    executor.setMaxPoolSize(4);
    executor.setQueueCapacity(1000);
    executor.setThreadNamePrefix("ReporterExecutor-");

    executor.initialize();

    return executor;
  }
 @Override
 public void auditSite(
     Contract contract,
     String siteUrl,
     String clientIp,
     Set<Parameter> parameterSet,
     Locale locale) {
   if (LOGGER.isDebugEnabled()) {
     LOGGER.debug("Site audit on " + siteUrl);
     for (Parameter param : parameterSet) {
       LOGGER.debug(
           "param "
               + param.getValue()
               + " "
               + param.getParameterElement().getParameterElementCode());
     }
   }
   Act act = createAct(contract, ScopeEnum.DOMAIN, clientIp);
   AuditThread auditSiteThread =
       new AuditSiteThread(siteUrl, auditService, act, parameterSet, locale);
   threadPoolTaskExecutor.submit(auditSiteThread);
 }
 /**
  * @param auditTimeoutThread
  * @param act
  * @return
  */
 private Audit submitAuditAndLaunch(AuditTimeoutThread auditTimeoutThread, Act act) {
   synchronized (auditTimeoutThread) {
     Future submitedThread = threadPoolTaskExecutor.submit(auditTimeoutThread);
     while (submitedThread != null && !submitedThread.isDone()) {
       try {
         Thread.sleep(500);
       } catch (InterruptedException ex) {
         LOGGER.error("", ex);
       }
       if (auditTimeoutThread.isDurationExceedsDelay()) {
         LOGGER.debug(
             "Audit Duration ExceedsDelay. The audit result "
                 + "is now managed in an asynchronous way.");
         break;
       }
     }
     if (null != auditTimeoutThread.getException()) {
       LOGGER.error("new KrashAuditException()");
       throw new KrashAuditException(auditTimeoutThread.getException());
     }
     return auditTimeoutThread.getAudit();
   }
 }
 @Override
 public void auditScenario(
     Contract contract,
     Long idScenario,
     String clientIp,
     Set<Parameter> parameterSet,
     Locale locale) {
   if (LOGGER.isDebugEnabled()) {
     LOGGER.debug("Scenarion audit on scenario with id" + idScenario);
     for (Parameter param : parameterSet) {
       LOGGER.debug(
           "param "
               + param.getValue()
               + " "
               + param.getParameterElement().getParameterElementCode());
     }
   }
   Act act = createAct(contract, ScopeEnum.SCENARIO, clientIp);
   Scenario scenario = scenarioDataService.read(idScenario);
   AuditThread auditScenarioThread =
       new AuditScenarioThread(
           scenario.getLabel(), scenario.getContent(), auditService, act, parameterSet, locale);
   threadPoolTaskExecutor.submit(auditScenarioThread);
 }
 @Scheduled(cron = "${cloudLivePresureCron}")
 public void insertBatch() {
   for (int i = 0; i < Integer.parseInt(Env.get("consumeThreadNum")); i++) {
     consumeExecutor.execute(new ConsumeTask());
   }
 }
Example #27
0
 public void onApplicationEvent(ContextClosedEvent event) {
   System.out.println("==================Spring Context is shutting down==============");
   scheduler.shutdown();
   executor.shutdown();
 }
 @Bean(name = YarnContextUtils.TASK_EXECUTOR_BEAN_NAME)
 public TaskExecutor threadPoolTaskExecutor() {
   ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
   executor.setCorePoolSize(2);
   return executor;
 }
Example #29
0
 public static void notify(Notification notification) {
   taskExecutor.execute(new NotificationWorker(notification));
 }