Skip to content

Commit fbb08f6

Browse files
committed
event
1 parent 93a0686 commit fbb08f6

File tree

4 files changed

+72
-7
lines changed

4 files changed

+72
-7
lines changed

src/main/java/cn/com/ttblog/ssmbootstrap_table/controller/IndexController.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,12 @@ public String login(HttpSession session, HttpServletRequest request,
7373
Cookie c = new Cookie(ConfigConstant.USERNAME, username);
7474
c.setMaxAge(86400);
7575
response.addCookie(c);
76-
Map<String, String> param=new HashMap<String,String>();
76+
Map<String, String> param=new HashMap<>();
7777
param.put("loginname", username);
7878
param.put("logintime", new DateTime().toString("yyyy-MM-dd HH:mm:ss"));
7979
param.put("loginip", request.getRemoteAddr());
80-
applicationContext.publishEvent(new LoginEvent(param));
80+
//publishEvent会依次调用所有的监听器,同步调用,所有监听器执行完毕继续向下执行
81+
applicationContext.publishEvent(new LoginEvent(this,param));
8182
if(requri!=null&&requri.length()>0){
8283
String uri=new String(Base64.decodeBase64(requri));
8384
String touri=uri.substring(request.getContextPath().length()+1);
Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,27 @@
11
package cn.com.ttblog.ssmbootstrap_table.event;
22

3+
import org.apache.commons.lang3.builder.ToStringBuilder;
34
import org.springframework.context.ApplicationEvent;
5+
import java.util.Map;
46

57
public class LoginEvent extends ApplicationEvent {
6-
7-
public LoginEvent(Object source) {
8+
9+
private Map data;
10+
11+
public LoginEvent(Object source, Map<String, String> data) {
812
super(source);
13+
this.data=data;
14+
}
15+
16+
public Map getData() {
17+
return data;
18+
}
19+
20+
public void setData(Map data) {
21+
this.data = data;
22+
}
23+
24+
public String toString(){
25+
return ToStringBuilder.reflectionToString(this);
926
}
10-
1127
}
Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,48 @@
11
package cn.com.ttblog.ssmbootstrap_table.util;
22

3+
import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
34
import org.springframework.context.annotation.Configuration;
5+
import org.springframework.scheduling.annotation.AsyncConfigurer;
46
import org.springframework.scheduling.annotation.EnableAsync;
7+
import java.lang.reflect.Method;
8+
import java.util.concurrent.Executor;
9+
import java.util.concurrent.ExecutorService;
10+
import java.util.concurrent.Executors;
11+
import java.util.concurrent.ThreadFactory;
512

613
/**
714
* 启用异步方法
815
* 发现在xml中配置<task:annotation-driven></task:annotation-driven>不生效,
916
* 基于java config是生效的
1017
* http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/scheduling/annotation/EnableAsync.html
18+
* http://docs.spring.io/spring/docs/4.2.9.RELEASE/spring-framework-reference/htmlsingle/#scheduling-annotation-support-async
1119
*/
1220
@Configuration
1321
@EnableAsync
14-
public class Config {
22+
public class Config implements AsyncConfigurer {
23+
24+
/**
25+
* 自定义实现Executor
26+
* @return
27+
*/
28+
@Override
29+
public Executor getAsyncExecutor() {
30+
ExecutorService executorService=Executors.newSingleThreadExecutor(new ThreadFactory() {
31+
@Override
32+
public Thread newThread(Runnable r) {
33+
return new Thread(r,"task");
34+
}
35+
});
36+
return executorService;
37+
}
38+
39+
@Override
40+
public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
41+
return new AsyncUncaughtExceptionHandler() {
42+
@Override
43+
public void handleUncaughtException(Throwable throwable, Method method, Object... objects) {
44+
System.out.printf("throwable:%s,method:%s,objects:%s",throwable,method,objects);
45+
}
46+
};
47+
}
1548
}

src/main/resources/spring/spring-context.xml

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@
33
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
44
xmlns:context="http://www.springframework.org/schema/context"
55
xmlns:aop="http://www.springframework.org/schema/aop"
6-
xsi:schemaLocation="http://www.springframework.org/schema/beans
6+
xmlns:task="http://www.springframework.org/schema/task"
7+
xsi:schemaLocation="
8+
http://www.springframework.org/schema/task
9+
http://www.springframework.org/schema/task/spring-task-4.0.xsd
10+
http://www.springframework.org/schema/beans
711
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
812
http://www.springframework.org/schema/context
913
http://www.springframework.org/schema/context/spring-context-4.0.xsd
@@ -18,6 +22,17 @@
1822
<context:exclude-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice" />
1923
</context:component-scan>
2024

25+
<!--<bean id="threadPoolTaskExecutor"-->
26+
<!--class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">-->
27+
<!--<property name="corePoolSize" value="1" />-->
28+
<!--<property name="maxPoolSize" value="10" />-->
29+
<!--<property name="queueCapacity" value="50" />-->
30+
<!--<property name="threadNamePrefix" value="task-" />-->
31+
<!--<property name="keepAliveSeconds" value="30" />-->
32+
<!--</bean>-->
33+
34+
<!--<task:annotation-driven executor="threadPoolTaskExecutor"></task:annotation-driven>-->
35+
2136
<bean id="configProperties"
2237
class="org.springframework.beans.factory.config.PropertiesFactoryBean">
2338
<property name="locations">

0 commit comments

Comments
 (0)