Spring 之事件发布与监听
1.同步处理
a.引入Spring的pom依赖(略)
b.创建Event事件类
public class TestEvent1 extends ApplicationEvent { private String message; public TestEvent1(Object source, String message) { super(source); this.message = message; } public String getMessage() { return message; } }
public class TestEvent2 extends ApplicationEvent { private String message; public TestEvent2(Object source, String message) { super(source); this.message = message; } public String getMessage() { return message; } }
c.创建Listener监听类
@Component public class TestListener1 implements ApplicationListener<TestEvent1> { @Override public void onApplicationEvent(TestEvent1 event) { System.out.println("[TestListener1] start ......"); try { Thread.sleep(2000); } catch (Exception e) { e.printStackTrace(); } System.out.println("[TestListener1] receive event: " + event.getMessage()); System.out.println("[TestListener1] end ......"); } }
@Component public class TestListener2 implements ApplicationListener<TestEvent2> { @Override public void onApplicationEvent(TestEvent2 event) { System.out.println("[TestListener2] start ......"); try { Thread.sleep(2000); } catch (Exception e) { e.printStackTrace(); } System.out.println("[TestListener2] receive event: " + event.getMessage()); System.out.println("[TestListener2] end ......"); } }
d.使用
@Component public class CommonEventHandler { @Autowired private ApplicationEventPublisher applicationEventPublisher; public void sendEvent1(String msg){ ApplicationEvent event = new TestEvent1(this,msg); applicationEventPublisher.publishEvent(event); } public void sendEvent2(String msg){ ApplicationEvent event = new TestEvent2(this,msg); applicationEventPublisher.publishEvent(event); } }
e.测试
public class App { public static void main( String[] args ) { ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml"); CommonEventHandler handler = (CommonEventHandler) context.getBean("commonEventHandler"); for (int i = 0; i < 20; i++) { if(i % 3 == 0){ handler.sendEvent1("[Event1]count: " + i); continue; } handler.sendEvent2("[Event2]count: " + i); } } }
2.异步处理
a.在spring.xml中加入线程池相关配置
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.1.xsd"> <!-- 自动扫描的包名 --> <context:component-scan base-package="com.wode" /> <!-- 开启AOP代理 --> <aop:aspectj-autoproxy proxy-target-class="true" /> <!--开启注解处理器 --> <context:annotation-config> </context:annotation-config> <!-- 线程池异步任务 --> <task:annotation-driven executor="executor"/> <task:executor id="executor" pool-size="10" queue-capacity="100"/> </beans>
b.在 Listener 的 onApplicationEvent 方法上加上 @Async 注解
@Component public class TestListener1 implements ApplicationListener<TestEvent1> { @Async @Override public void onApplicationEvent(TestEvent1 event) { System.out.println("[TestListener1] start ......"); try { Thread.sleep(2000); } catch (Exception e) { e.printStackTrace(); } System.out.println("[TestListener1] receive event: " + event.getMessage()); System.out.println("[TestListener1] end ......"); } }
@Component public class TestListener2 implements ApplicationListener<TestEvent2> { @Async @Override public void onApplicationEvent(TestEvent2 event) { System.out.println("[TestListener2] start ......"); try { Thread.sleep(2000); } catch (Exception e) { e.printStackTrace(); } System.out.println("[TestListener2] receive event: " + event.getMessage()); System.out.println("[TestListener2] end ......"); } }