Ejemplo n.º 1
1
  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);
  }
Ejemplo n.º 2
0
 /** 执行一个服务器的调度 */
 public void executeOneServer(Date date, Integer sysNum) {
   ServerTask task = new ServerTask(date, sysNum);
   ThreadPoolTaskExecutor executer =
       (ThreadPoolTaskExecutor)
           ServiceCacheFactory.getServiceCache().getBeanById("dayTaskExecutor");
   executer.execute(task);
 }
Ejemplo n.º 3
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));
 }
Ejemplo n.º 4
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);
           }
         });
   }
 }
  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();
  }
  @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();
      }
    }
  }
Ejemplo n.º 7
0
 public static void notify(Notification notification) {
   taskExecutor.execute(new NotificationWorker(notification));
 }
 @Scheduled(cron = "${cloudLivePresureCron}")
 public void insertBatch() {
   for (int i = 0; i < Integer.parseInt(Env.get("consumeThreadNum")); i++) {
     consumeExecutor.execute(new ConsumeTask());
   }
 }