You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

157 lines
6.3 KiB

  1. package com.lecoo.kjg.web;
  2. import com.jeesite.common.config.Global;
  3. import com.jeesite.common.io.PropertiesUtils;
  4. import com.lecoo.kjg.web.config.BusinessLoggerInterceptor;
  5. import com.lecoo.kjg.web.sys.bean.filter.BodyReaderRequestFilter;
  6. import com.lecoo.kjg.web.sys.service.support.DeviceServiceSupport;
  7. import com.lecoo.kjg.web.utils.ResponseCodeUtil;
  8. import org.apache.commons.lang3.concurrent.BasicThreadFactory;
  9. import org.slf4j.Logger;
  10. import org.slf4j.LoggerFactory;
  11. import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
  12. import org.springframework.beans.factory.annotation.Autowired;
  13. import org.springframework.beans.factory.annotation.Value;
  14. import org.springframework.boot.CommandLineRunner;
  15. import org.springframework.boot.SpringApplication;
  16. import org.springframework.boot.autoconfigure.SpringBootApplication;
  17. import org.springframework.boot.builder.SpringApplicationBuilder;
  18. import org.springframework.boot.web.servlet.FilterRegistrationBean;
  19. import org.springframework.boot.web.servlet.MultipartConfigFactory;
  20. import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
  21. import org.springframework.context.annotation.Bean;
  22. import org.springframework.data.redis.core.StringRedisTemplate;
  23. import org.springframework.http.client.BufferingClientHttpRequestFactory;
  24. import org.springframework.http.client.SimpleClientHttpRequestFactory;
  25. import org.springframework.http.converter.StringHttpMessageConverter;
  26. import org.springframework.scheduling.TaskScheduler;
  27. import org.springframework.scheduling.annotation.EnableAsync;
  28. import org.springframework.scheduling.annotation.EnableScheduling;
  29. import org.springframework.scheduling.concurrent.ConcurrentTaskScheduler;
  30. import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
  31. import org.springframework.web.client.DefaultResponseErrorHandler;
  32. import org.springframework.web.client.RestTemplate;
  33. import javax.servlet.MultipartConfigElement;
  34. import java.nio.charset.Charset;
  35. import java.util.Collections;
  36. import java.util.Map;
  37. import java.util.TimeZone;
  38. import java.util.concurrent.ScheduledExecutorService;
  39. import java.util.concurrent.ScheduledThreadPoolExecutor;
  40. /**
  41. * 1
  42. * 项目启动类,
  43. *
  44. * @author ***
  45. * @version 2018-7-10
  46. * <p>
  47. * 此项目中的jeesite.yml文件可以自行配置,相较于jeesite-core.yml优先级更高,
  48. * 一但配置了jeesite.yml就自动覆盖jeesite-core.yml中的配置 ---个人见解
  49. */
  50. //注意此启动类中数组的第0个元素出必须加上不能删除,否则项目报错
  51. @SpringBootApplication(scanBasePackages = {"com.jeesite.modules", "com.lecoo.kjg.web"})
  52. @EnableAsync
  53. @EnableScheduling
  54. @EnableBatchProcessing
  55. public class Application extends SpringBootServletInitializer implements CommandLineRunner {
  56. private final static Logger log = LoggerFactory.getLogger(Application.class);
  57. @Value("${spring.profiles.active}")
  58. private String activeProfile;
  59. @Autowired
  60. BusinessLoggerInterceptor actionTrackInterceptor;
  61. @Autowired
  62. StringRedisTemplate stringRedisTemplate;
  63. @Autowired
  64. DeviceServiceSupport deviceServiceSupport;
  65. public static void main(String[] args) {
  66. TimeZone.setDefault(TimeZone.getTimeZone("Asia/Shanghai"));
  67. SpringApplication app = new SpringApplication(Application.class);
  68. app.setDefaultProperties(PropertiesUtils.getInstance().getProperties());
  69. app.run(args);
  70. }
  71. /**
  72. * 业务状态码和提联想ThinkFace办公系统示文字
  73. *
  74. * @return Map
  75. */
  76. @Bean
  77. public Map<Integer, Map<String, String>> responseMessageMap() {
  78. log.info("Active {} profile", activeProfile);
  79. return ResponseCodeUtil.getResponseCodeAndMsgMap();
  80. }
  81. @Bean
  82. public RestTemplate restTemplate() {
  83. SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
  84. requestFactory.setReadTimeout(25000);
  85. BufferingClientHttpRequestFactory bufferingClientHttpRequestFactory = new BufferingClientHttpRequestFactory(requestFactory);
  86. RestTemplate restTemplate = new RestTemplate(bufferingClientHttpRequestFactory);
  87. restTemplate.getMessageConverters().add(1, new StringHttpMessageConverter(Charset.forName("UTF-8")));
  88. restTemplate.setInterceptors(Collections.singletonList(actionTrackInterceptor));
  89. restTemplate.setErrorHandler(new DefaultResponseErrorHandler());
  90. return restTemplate;
  91. }
  92. @Bean
  93. public FilterRegistrationBean getFilterRegistrationBean(){
  94. FilterRegistrationBean bean = new FilterRegistrationBean(new BodyReaderRequestFilter());
  95. //bean.addUrlPatterns(new String[]{"*.do","*.jsp"});
  96. bean.addUrlPatterns("/api*");
  97. return bean;
  98. }
  99. @Override
  100. protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
  101. // 错误页面有容器来处理,而不是SpringBoot
  102. this.setRegisterErrorPageFilter(false);
  103. builder.properties(PropertiesUtils.getInstance().getProperties());
  104. return builder.sources(Application.class);
  105. }
  106. /**
  107. * 自定义处理Websocket消息队列线程池
  108. *
  109. * @return
  110. */
  111. @Bean("backExecutor")
  112. public ThreadPoolTaskExecutor backExecutor() {
  113. ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
  114. executor.setThreadNamePrefix("WS-MyExecutor-");
  115. executor.setCorePoolSize(3);
  116. executor.setMaxPoolSize(50);
  117. executor.setQueueCapacity(100);
  118. executor.setKeepAliveSeconds(300);
  119. return executor;
  120. }
  121. @Bean
  122. public TaskScheduler taskScheduler() {
  123. ScheduledExecutorService executorService = new ScheduledThreadPoolExecutor(3,
  124. new BasicThreadFactory.Builder().namingPattern("schedule-pool-%d").daemon(true).build());
  125. return new ConcurrentTaskScheduler(executorService);
  126. }
  127. @Bean
  128. public MultipartConfigElement multipartConfigElement() {
  129. MultipartConfigFactory factory = new MultipartConfigFactory();
  130. factory.setMaxFileSize("512MB"); // KB,MB
  131. factory.setMaxRequestSize("512MB");
  132. return factory.createMultipartConfig();
  133. }
  134. @Override
  135. public void run(String... args) throws Exception {
  136. deviceServiceSupport.putRedisData();
  137. }
  138. }