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); }
@Override public Executor getAsyncExecutor() { ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); executor.setThreadNamePrefix("Custom-"); executor.initialize(); return executor; }
/** * 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; }
@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; }
@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(); } }
@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; }
@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; }
@Bean public ThreadPoolTaskExecutor taskExecutor() { ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); executor.setCorePoolSize(4); executor.setMaxPoolSize(4); executor.setQueueCapacity(1000); executor.setThreadNamePrefix("ReporterExecutor-"); executor.initialize(); return executor; }
@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; }