package com.yc.open.config; import com.google.code.kaptcha.impl.DefaultKaptcha; import com.google.code.kaptcha.util.Config; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler; import java.util.Properties; import java.util.concurrent.Executor; import java.util.concurrent.RejectedExecutionHandler; import java.util.concurrent.ThreadPoolExecutor; @Configuration public class APIBeanConfig { protected final Logger log = LoggerFactory.getLogger(this.getClass()); //创建以线程池来执行线程任务 @Bean public Executor threadPoolExecutor() {//线程池 ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); executor.setCorePoolSize(60); executor.setMaxPoolSize(100); executor.setQueueCapacity(20); executor.setThreadNamePrefix("OnbusThread_"); executor.setRejectedExecutionHandler(new RejectedExecutionHandler() { @Override public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) { if (!executor.isShutdown()) { try { //log.info("start get queue:"+executor.getQueue().size()); executor.getQueue().put(r); //log.info("end get queue:"+executor.getQueue().size()); } catch (InterruptedException e) { log.error(e.toString(), e); Thread.currentThread().interrupt(); } } } }); executor.initialize(); return executor; } @Bean public ThreadPoolTaskScheduler threadPoolTaskScheduler() {//定时执行任务 ThreadPoolTaskScheduler executor = new ThreadPoolTaskScheduler(); executor.setPoolSize(20); executor.setThreadNamePrefix("OnBusTask-"); executor.setWaitForTasksToCompleteOnShutdown(true); executor.setAwaitTerminationSeconds(60); executor.initialize(); return executor; } @Bean public DefaultKaptcha defaultKaptcha() {//图片验证码 DefaultKaptcha defaultKaptcha = new DefaultKaptcha(); Properties properties = new Properties(); properties.put("kaptcha.border", "no"); properties.put("kaptcha.border.color", "black"); properties.put("kaptcha.textproducer.font.color", "black"); properties.put("kaptcha.textproducer.font.size", "32"); properties.put("kaptcha.textproducer.char.length", "4"); properties.put("kaptcha.textproducer.char.space", "5"); properties.put("kaptcha.textproducer.char.length", "4"); properties.put("kaptcha.image.width", "110"); properties.put("kaptcha.image.height", "40"); properties.put("kaptcha.textproducer.char.string","ABCDEFGHJKLMNPQRSTUVWXYZ1234567890"); Config config=new Config(properties); defaultKaptcha.setConfig(config); return defaultKaptcha; } }