Skip to content

Commit

Permalink
fix LoginEvent twice
Browse files Browse the repository at this point in the history
  • Loading branch information
netbuffer committed Dec 8, 2017
1 parent e26b450 commit d3ceb4b
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 60,7 @@ public IndexController(){
private Logger logger = LoggerFactory.getLogger(this.getClass());

@Autowired
private ApplicationContext applicationContext;
private ApplicationContext applicationContext;

@RequestMapping("/login")
public String login(HttpSession session, HttpServletRequest request,
Expand All @@ -79,7 79,11 @@ public String login(HttpSession session, HttpServletRequest request,
param.put("logintime", new DateTime().toString("yyyy-MM-dd HH:mm:ss"));
param.put("loginip", request.getRemoteAddr());
//publishEvent会依次调用所有的监听器,同步调用,所有监听器执行完毕继续向下执行
applicationContext.publishEvent(new LoginEvent(this,param));
logger.info("发布事件,parent context:{},application:{}",applicationContext.getParent(),applicationContext);
/**
* 在Controller控制器这一层发布事件不要直接用applicationContext.publishEvent(),因为事件会同时添加在root application context中导致重复执行一次
*/
applicationContext.getParent().publishEvent(new LoginEvent(this,param));
if(requri!=null&&requri.length()>0){
String uri=new String(Base64.decodeBase64(requri));
String touri=uri.substring(request.getContextPath().length() 1);
Expand Down
96 changes: 48 additions & 48 deletions src/main/java/cn/com/ttblog/ssmbootstrap_table/util/Config.java
Original file line number Diff line number Diff line change
@@ -1,48 1,48 @@
package cn.com.ttblog.ssmbootstrap_table.util;

import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.annotation.EnableAsync;
import java.lang.reflect.Method;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;

/**
* 启用异步方法
* 发现在xml中配置<task:annotation-driven></task:annotation-driven>不生效,
* 基于java config是生效的
* http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/scheduling/annotation/EnableAsync.html
* http://docs.spring.io/spring/docs/4.2.9.RELEASE/spring-framework-reference/htmlsingle/#scheduling-annotation-support-async
*/
@Configuration
@EnableAsync
public class Config implements AsyncConfigurer {

/**
* 自定义实现Executor
* @return
*/
@Override
public Executor getAsyncExecutor() {
ExecutorService executorService=Executors.newSingleThreadExecutor(new ThreadFactory() {
@Override
public Thread newThread(Runnable r) {
return new Thread(r,"task");
}
});
return executorService;
}

@Override
public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
return new AsyncUncaughtExceptionHandler() {
@Override
public void handleUncaughtException(Throwable throwable, Method method, Object... objects) {
System.out.printf("throwable:%s,method:%s,objects:%s",throwable,method,objects);
}
};
}
}
//package cn.com.ttblog.ssmbootstrap_table.util;
//
//import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.scheduling.annotation.AsyncConfigurer;
//import org.springframework.scheduling.annotation.EnableAsync;
//import java.lang.reflect.Method;
//import java.util.concurrent.Executor;
//import java.util.concurrent.ExecutorService;
//import java.util.concurrent.Executors;
//import java.util.concurrent.ThreadFactory;
//
///**
// * 启用异步方法
// * 发现在xml中配置<task:annotation-driven></task:annotation-driven>不生效,
// * 基于java config是生效的
// * http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/scheduling/annotation/EnableAsync.html
// * http://docs.spring.io/spring/docs/4.2.9.RELEASE/spring-framework-reference/htmlsingle/#scheduling-annotation-support-async
// */
//@Configuration
//@EnableAsync
//public class Config implements AsyncConfigurer {
//
// /**
// * 自定义实现Executor
// * @return
// */
// @Override
// public Executor getAsyncExecutor() {
// ExecutorService executorService=Executors.newSingleThreadExecutor(new ThreadFactory() {
// @Override
// public Thread newThread(Runnable r) {
// return new Thread(r,"task");
// }
// });
// return executorService;
// }
//
// @Override
// public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
// return new AsyncUncaughtExceptionHandler() {
// @Override
// public void handleUncaughtException(Throwable throwable, Method method, Object... objects) {
// System.out.printf("throwable:%s,method:%s,objects:%s",throwable,method,objects);
// }
// };
// }
//}
13 changes: 12 additions & 1 deletion src/main/resources/spring/spring-context.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 56,18 @@
<!--<import resource="spring-jmx.xml" />-->
<import resource="spring-ehcache.xml"/>
<import resource="spring-websocket.xml"/>


<bean id="async-method-pool"
class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
<property name="corePoolSize" value="1" />
<property name="maxPoolSize" value="10" />
<property name="queueCapacity" value="50" />
<property name="threadNamePrefix" value="async-method-pool-" />
<property name="keepAliveSeconds" value="30" />
</bean>

<task:annotation-driven executor="async-method-pool" proxy-target-class="true"></task:annotation-driven>

<!-- jsonpfilter -->
<bean class="com.intera.util.web.servlet.filter.JsonpCallbackFilter"
id="jsonpCallbackFilter" />
Expand Down

0 comments on commit d3ceb4b

Please sign in to comment.