From 71daa7857e7a06d496cbeced761778082f88571b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=BD=97=E7=A5=A5?= <1366971433@qq.com> Date: Thu, 27 Dec 2018 15:08:24 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=20spring=20=E5=AE=9A?= =?UTF-8?q?=E6=97=B6=E4=BB=BB=E5=8A=A1=E7=94=A8=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- spring/spring-scheduling-annotation/pom.xml | 61 ++++++++++++ .../config/DispatcherServletInitializer.java | 23 +++++ .../com/heibaiying/config/ServletConfig.java | 93 +++++++++++++++++++ .../main/java/com/heibaiying/task/Task.java | 43 +++++++++ spring/spring-scheduling/pom.xml | 60 ++++++++++++ .../main/java/com/heibaiying/task/Task.java | 42 +++++++++ .../src/main/resources/springApplication.xml | 62 +++++++++++++ .../src/main/webapp/WEB-INF/web.xml | 24 +++++ .../src/main/webapp/index.jsp | 9 ++ spring/spring-websocket/pom.xml | 68 ++++++++++++++ .../websocket/CustomHandshakeInterceptor.java | 29 ++++++ .../heibaiying/websocket/CustomerHandler.java | 21 +++++ .../src/main/resources/springApplication.xml | 45 +++++++++ .../src/main/webapp/WEB-INF/web.xml | 24 +++++ .../src/main/webapp/index.jsp | 37 ++++++++ 15 files changed, 641 insertions(+) create mode 100644 spring/spring-scheduling-annotation/pom.xml create mode 100644 spring/spring-scheduling-annotation/src/main/java/com/heibaiying/config/DispatcherServletInitializer.java create mode 100644 spring/spring-scheduling-annotation/src/main/java/com/heibaiying/config/ServletConfig.java create mode 100644 spring/spring-scheduling-annotation/src/main/java/com/heibaiying/task/Task.java create mode 100644 spring/spring-scheduling/pom.xml create mode 100644 spring/spring-scheduling/src/main/java/com/heibaiying/task/Task.java create mode 100644 spring/spring-scheduling/src/main/resources/springApplication.xml create mode 100644 spring/spring-scheduling/src/main/webapp/WEB-INF/web.xml create mode 100644 spring/spring-scheduling/src/main/webapp/index.jsp create mode 100644 spring/spring-websocket/pom.xml create mode 100644 spring/spring-websocket/src/main/java/com/heibaiying/websocket/CustomHandshakeInterceptor.java create mode 100644 spring/spring-websocket/src/main/java/com/heibaiying/websocket/CustomerHandler.java create mode 100644 spring/spring-websocket/src/main/resources/springApplication.xml create mode 100644 spring/spring-websocket/src/main/webapp/WEB-INF/web.xml create mode 100644 spring/spring-websocket/src/main/webapp/index.jsp diff --git a/spring/spring-scheduling-annotation/pom.xml b/spring/spring-scheduling-annotation/pom.xml new file mode 100644 index 0000000..93aaaa0 --- /dev/null +++ b/spring/spring-scheduling-annotation/pom.xml @@ -0,0 +1,61 @@ + + + 4.0.0 + + com.heibaiying + spring-scheduling-annotation + 1.0-SNAPSHOT + + + + org.apache.maven.plugins + maven-compiler-plugin + + 8 + 8 + + + + + + + 5.1.3.RELEASE + + + + + org.springframework + spring-context + ${spring-base-version} + + + org.springframework + spring-beans + ${spring-base-version} + + + org.springframework + spring-core + ${spring-base-version} + + + org.springframework + spring-web + ${spring-base-version} + + + org.springframework + spring-webmvc + ${spring-base-version} + + + javax.servlet + javax.servlet-api + 4.0.1 + provided + + + + \ No newline at end of file diff --git a/spring/spring-scheduling-annotation/src/main/java/com/heibaiying/config/DispatcherServletInitializer.java b/spring/spring-scheduling-annotation/src/main/java/com/heibaiying/config/DispatcherServletInitializer.java new file mode 100644 index 0000000..fb2f8b7 --- /dev/null +++ b/spring/spring-scheduling-annotation/src/main/java/com/heibaiying/config/DispatcherServletInitializer.java @@ -0,0 +1,23 @@ +package com.heibaiying.config; + +import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer; + +/** + * @author : heibaiying + */ + +public class DispatcherServletInitializer extends AbstractAnnotationConfigDispatcherServletInitializer { + + protected Class[] getRootConfigClasses() { + return new Class[0]; + } + + protected Class[] getServletConfigClasses() { + return new Class[]{ServletConfig.class}; + } + + protected String[] getServletMappings() { + return new String[]{"/"}; + } + +} diff --git a/spring/spring-scheduling-annotation/src/main/java/com/heibaiying/config/ServletConfig.java b/spring/spring-scheduling-annotation/src/main/java/com/heibaiying/config/ServletConfig.java new file mode 100644 index 0000000..e876914 --- /dev/null +++ b/spring/spring-scheduling-annotation/src/main/java/com/heibaiying/config/ServletConfig.java @@ -0,0 +1,93 @@ +package com.heibaiying.config; + +import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.scheduling.annotation.AsyncConfigurer; +import org.springframework.scheduling.annotation.EnableAsync; +import org.springframework.scheduling.annotation.EnableScheduling; +import org.springframework.scheduling.annotation.SchedulingConfigurer; +import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; +import org.springframework.scheduling.config.ScheduledTaskRegistrar; +import org.springframework.web.servlet.config.annotation.EnableWebMvc; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; + +import javax.annotation.PreDestroy; +import java.lang.reflect.Method; +import java.util.concurrent.Executor; +import java.util.concurrent.Executors; + +/** + * @author : heibaiying + * spring 主配置类 + */ +@Configuration +@EnableWebMvc +@EnableScheduling //启用Spring的计划任务执行功能 +@EnableAsync //启用Spring的异步方法执行功能 +@ComponentScan(basePackages = {"com.heibaiying.task"}) +public class ServletConfig implements WebMvcConfigurer, AsyncConfigurer, SchedulingConfigurer { + + private ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); + + // 任务执行器线程池配置 + @Override + public Executor getAsyncExecutor() { + executor.setCorePoolSize(5); + executor.setMaxPoolSize(10); + executor.setQueueCapacity(100); + executor.setThreadNamePrefix("MyExecutor-"); + executor.initialize(); + return executor; + } + + // 这个方法可以监听到异步程序发生的错误 + @Override + public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() { + return new AsyncUncaughtExceptionHandler() { + @Override + public void handleUncaughtException(Throwable ex, Method method, Object... params) { + System.out.println(method.getName() + "发生错误:" + ex.getMessage()); + } + }; + } + + // 如果程序结束,需要关闭线程池 不然程序无法完全退出 只能kill才能完全退出 + @PreDestroy + public void destroy() { + if (executor != null) { + executor.shutdown(); + } + } + + // 调度程序线程池配置 + @Override + public void configureTasks(ScheduledTaskRegistrar taskRegistrar) { + taskRegistrar.setScheduler(taskExecutor()); + } + + // 如果程序结束,需要关闭线程池 + @Bean(destroyMethod = "shutdown") + public Executor taskExecutor() { + return Executors.newScheduledThreadPool(50); + } + + + /*关于调度程序线程池作用说明 + * + * 按照例子 我们有methodA 、 methodB 、methodC 三个方法 其中 methodB 是耗时的方法,且不支持异步 + * 如果不声明调度程序线程池 则methodB 会阻塞 methodA 、methodC 方法的执行 因为调度程序是单线程的 + */ + + + /*关于任务执行线程池作用说明 + * + * 按照例子 如果我们声明 methodB 是按照 fixedRate=5000 方法执行的 ,理论上不管任务耗时多久,任务都应该是 + * 每5秒执行一次,但是实际上任务是被加入执行队列,也不会立即被执行,因为默认执行任务是单线程的,这个时候需要开启 + * @EnableAsync 并指定方法是 @Async 异步的,并且配置执行任务线程池(如果不配置就使用默认的线程池配置) + */ + + /*配置建议 如果不需要细粒度的控制 以上代码配置都不是必须的 但是建议耗时方法采用异步执行*/ + +} diff --git a/spring/spring-scheduling-annotation/src/main/java/com/heibaiying/task/Task.java b/spring/spring-scheduling-annotation/src/main/java/com/heibaiying/task/Task.java new file mode 100644 index 0000000..be90cd3 --- /dev/null +++ b/spring/spring-scheduling-annotation/src/main/java/com/heibaiying/task/Task.java @@ -0,0 +1,43 @@ +package com.heibaiying.task; + +import org.springframework.scheduling.annotation.Async; +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.stereotype.Component; + +import java.time.LocalDateTime; + +/** + * @author : heibaiying + */ +@Component +public class Task { + + /** + * 基于间隔的触发器,其中间隔是从上一个任务的 完成时间 开始计算, 时间单位值以毫秒为单位。 + */ + @Scheduled(fixedDelay = 5000, initialDelay = 1000) + public void methodA() { + Thread thread = Thread.currentThread(); + System.out.println(String.format("线程名称:%s ; 线程ID:%s ; 调用方法:%s ; 调用时间:%s", + thread.getName(), thread.getId(), "methodA方法执行", LocalDateTime.now())); + } + + /** + * 基于间隔的触发器,其中间隔是从上一个任务的 开始时间 开始测量的。 + */ + @Scheduled(fixedRate = 5000) + @Async + public void methodB() throws InterruptedException { + Thread thread = Thread.currentThread(); + System.out.println(String.format("线程名称:%s ; 线程ID:%s ; 调用方法:%s ; 调用时间:%s", + thread.getName(), thread.getId(), "methodB方法执行", LocalDateTime.now())); + Thread.sleep(10 * 1000); + } + + @Scheduled(cron = "0/10 * * * * ?") + public void methodC() { + Thread thread = Thread.currentThread(); + System.out.println(String.format("线程名称:%s ; 线程ID:%s ; 调用方法:%s ; 调用时间:%s", + thread.getName(), thread.getId(), "methodC方法执行", LocalDateTime.now())); + } +} diff --git a/spring/spring-scheduling/pom.xml b/spring/spring-scheduling/pom.xml new file mode 100644 index 0000000..60c7996 --- /dev/null +++ b/spring/spring-scheduling/pom.xml @@ -0,0 +1,60 @@ + + + 4.0.0 + + com.heibaiying + spring-scheduling + 1.0-SNAPSHOT + + + + org.apache.maven.plugins + maven-compiler-plugin + + 8 + 8 + + + + + + 5.1.3.RELEASE + + + + + org.springframework + spring-context + ${spring-base-version} + + + org.springframework + spring-beans + ${spring-base-version} + + + org.springframework + spring-core + ${spring-base-version} + + + org.springframework + spring-web + ${spring-base-version} + + + org.springframework + spring-webmvc + ${spring-base-version} + + + javax.servlet + javax.servlet-api + 4.0.1 + provided + + + + \ No newline at end of file diff --git a/spring/spring-scheduling/src/main/java/com/heibaiying/task/Task.java b/spring/spring-scheduling/src/main/java/com/heibaiying/task/Task.java new file mode 100644 index 0000000..128e862 --- /dev/null +++ b/spring/spring-scheduling/src/main/java/com/heibaiying/task/Task.java @@ -0,0 +1,42 @@ +package com.heibaiying.task; + +import org.springframework.scheduling.annotation.Async; +import org.springframework.scheduling.annotation.Scheduled; + +import java.time.LocalDateTime; + +/** + * @author : heibaiying + * @description : + */ +public class Task { + + /** + * 基于间隔的触发器,其中间隔是从上一个任务的 完成时间 开始计算, 时间单位值以毫秒为单位。 + */ + @Scheduled(fixedDelay = 5000, initialDelay = 1000) + public void methodA() { + Thread thread = Thread.currentThread(); + System.out.println(String.format("线程名称:%s ; 线程ID:%s ; 调用方法:%s ; 调用时间:%s", + thread.getName(), thread.getId(), "methodA方法执行", LocalDateTime.now())); + } + + /** + * 基于间隔的触发器,其中间隔是从上一个任务的 开始时间 开始测量的。 + */ + @Scheduled(fixedRate = 5000) + @Async + public void methodB() throws InterruptedException { + Thread thread = Thread.currentThread(); + System.out.println(String.format("线程名称:%s ; 线程ID:%s ; 调用方法:%s ; 调用时间:%s", + thread.getName(), thread.getId(), "methodB方法执行", LocalDateTime.now())); + Thread.sleep(10 * 1000); + } + + @Scheduled(cron = "0/10 * * * * ?") + public void methodC() { + Thread thread = Thread.currentThread(); + System.out.println(String.format("线程名称:%s ; 线程ID:%s ; 调用方法:%s ; 调用时间:%s", + thread.getName(), thread.getId(), "methodC方法执行", LocalDateTime.now())); + } +} diff --git a/spring/spring-scheduling/src/main/resources/springApplication.xml b/spring/spring-scheduling/src/main/resources/springApplication.xml new file mode 100644 index 0000000..ee4b955 --- /dev/null +++ b/spring/spring-scheduling/src/main/resources/springApplication.xml @@ -0,0 +1,62 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/spring/spring-scheduling/src/main/webapp/WEB-INF/web.xml b/spring/spring-scheduling/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 0000000..c5e8f38 --- /dev/null +++ b/spring/spring-scheduling/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,24 @@ + + + + + + springMvc + org.springframework.web.servlet.DispatcherServlet + + contextConfigLocation + classpath:springApplication.xml + + 1 + + + + springMvc + / + + + \ No newline at end of file diff --git a/spring/spring-scheduling/src/main/webapp/index.jsp b/spring/spring-scheduling/src/main/webapp/index.jsp new file mode 100644 index 0000000..2b36dc2 --- /dev/null +++ b/spring/spring-scheduling/src/main/webapp/index.jsp @@ -0,0 +1,9 @@ +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + Title + + + hello spring scheduling! + + diff --git a/spring/spring-websocket/pom.xml b/spring/spring-websocket/pom.xml new file mode 100644 index 0000000..efadbc9 --- /dev/null +++ b/spring/spring-websocket/pom.xml @@ -0,0 +1,68 @@ + + + 4.0.0 + + com.heibaiying + spring-websocket + 1.0-SNAPSHOT + + + + org.apache.maven.plugins + maven-compiler-plugin + + 8 + 8 + + + + + + + + 5.1.3.RELEASE + + + + + org.springframework + spring-context + ${spring-base-version} + + + org.springframework + spring-beans + ${spring-base-version} + + + org.springframework + spring-core + ${spring-base-version} + + + org.springframework + spring-web + ${spring-base-version} + + + org.springframework + spring-webmvc + ${spring-base-version} + + + javax.servlet + javax.servlet-api + 4.0.1 + provided + + + + org.springframework + spring-websocket + 5.1.3.RELEASE + + + + \ No newline at end of file diff --git a/spring/spring-websocket/src/main/java/com/heibaiying/websocket/CustomHandshakeInterceptor.java b/spring/spring-websocket/src/main/java/com/heibaiying/websocket/CustomHandshakeInterceptor.java new file mode 100644 index 0000000..5dae0f5 --- /dev/null +++ b/spring/spring-websocket/src/main/java/com/heibaiying/websocket/CustomHandshakeInterceptor.java @@ -0,0 +1,29 @@ +package com.heibaiying.websocket; + +import org.springframework.http.server.ServerHttpRequest; +import org.springframework.http.server.ServerHttpResponse; +import org.springframework.web.socket.WebSocketHandler; +import org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor; + +import java.net.InetSocketAddress; +import java.util.Map; + +/** + * @author : heibaiying + * @description : 握手拦截器 + */ +public class CustomHandshakeInterceptor extends HttpSessionHandshakeInterceptor { + + /*@Override + public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Map attributes) throws Exception { + InetSocketAddress remoteAddress = request.getRemoteAddress(); + System.out.println(remoteAddress); + return true; + }*/ + + @Override + public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Map attributes) throws Exception { + return super.beforeHandshake(request, response, wsHandler, attributes); + } +} + diff --git a/spring/spring-websocket/src/main/java/com/heibaiying/websocket/CustomerHandler.java b/spring/spring-websocket/src/main/java/com/heibaiying/websocket/CustomerHandler.java new file mode 100644 index 0000000..5b52723 --- /dev/null +++ b/spring/spring-websocket/src/main/java/com/heibaiying/websocket/CustomerHandler.java @@ -0,0 +1,21 @@ +package com.heibaiying.websocket; + +import org.springframework.web.socket.TextMessage; +import org.springframework.web.socket.WebSocketSession; +import org.springframework.web.socket.handler.TextWebSocketHandler; + +import java.time.LocalDateTime; + +/** + * @author : heibaiying + * @description : 自定义消息处理器 + */ +public class CustomerHandler extends TextWebSocketHandler { + + @Override + protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception { + String stringMessage = new String(message.asBytes()); + System.out.println("服务端收到消息:" + stringMessage); + session.sendMessage(new TextMessage(stringMessage+LocalDateTime.now())); + } +} diff --git a/spring/spring-websocket/src/main/resources/springApplication.xml b/spring/spring-websocket/src/main/resources/springApplication.xml new file mode 100644 index 0000000..80023cc --- /dev/null +++ b/spring/spring-websocket/src/main/resources/springApplication.xml @@ -0,0 +1,45 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/spring/spring-websocket/src/main/webapp/WEB-INF/web.xml b/spring/spring-websocket/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 0000000..c5e8f38 --- /dev/null +++ b/spring/spring-websocket/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,24 @@ + + + + + + springMvc + org.springframework.web.servlet.DispatcherServlet + + contextConfigLocation + classpath:springApplication.xml + + 1 + + + + springMvc + / + + + \ No newline at end of file diff --git a/spring/spring-websocket/src/main/webapp/index.jsp b/spring/spring-websocket/src/main/webapp/index.jsp new file mode 100644 index 0000000..1f75321 --- /dev/null +++ b/spring/spring-websocket/src/main/webapp/index.jsp @@ -0,0 +1,37 @@ +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + Title + + + + +
+ +
+ + +