利用spring,自己实现的一个观察者模式,写着玩玩,目的是为了加深理解,下次用Spring自带的玩一玩。
首先我们定义一个侦听类接口
package com.hyenas.common.listener; import java.util.Map; public interface Observer { public boolean isAsyn(); public void excute(Map<String, Object> params);
}
抽象侦听类
package com.hyenas.common.listener; public abstract class AbstractObserver implements Observer { private boolean asyn = false; public void setAsyn(boolean asyn) {
this.asyn = asyn;
} @Override
public boolean isAsyn() {
return asyn;
} }
事件管理类
package com.hyenas.common.listener; import java.util.List;
import java.util.Map; public class EventManager { private Map<String, List<Observer>> events; public void setEvents(Map<String, List<Observer>> events) {
this.events = events;
} public void dispather(String eventName, Map<String, Object> params) {
if (events == null || events.isEmpty()) {
return;
} List<Observer> observers = events.get(eventName);
for (Observer observer : observers) {
if (observer.isAsyn()) {
EventManagerThead eventManagerThead = new EventManagerThead(
observer, params);
Thread t = new Thread(eventManagerThead);
t.start();
} else {
observer.excute(params);
}
}
} class EventManagerThead implements Runnable { private Observer observer;
private Map<String, Object> params; public EventManagerThead(Observer observer, Map<String, Object> params) {
super();
this.observer = observer;
this.params = params;
} @Override
public void run() {
observer.excute(params);
} }
}
然后我们定义一个侦听者的实现类
package com.hyenas.common.listener; import java.util.Map;
import java.util.Map.Entry; public class ObserverTest extends AbstractObserver { @Override
public void excute(Map<String, Object> params) { System.out.println("execute ObserverTest"); for (Entry<String, Object> entry : params.entrySet()) {
System.out.println(entry.getKey() + "->" + entry.getValue());
} System.out.println("thread name ObserverTest:"
+ Thread.currentThread().getName());
} }
Spring 配置文件 : listener.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:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <bean id="eventManager" class="com.hyenas.common.listener.EventManager">
<property name="events">
<map>
<entry key="observerTest">
<list>
<ref bean="observerTest"/>
</list>
</entry>
</map>
</property>
</bean> <bean id="observerTest" class="com.hyenas.common.listener.ObserverTest">
<property name="asyn" value="true"></property>
</bean> </beans>
好了,我们来测试一下:
package com.hyenas.common.listener; import java.util.HashMap;
import java.util.Map; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Test {
public static void main(String[] args) { ClassPathXmlApplicationContext ctx = null; try { String[] locations = { "listener.xml" };
ctx = new ClassPathXmlApplicationContext(locations); EventManager eventManager = (EventManager) ctx
.getBean("eventManager"); System.out.println("thread name main:"
+ Thread.currentThread().getName()); Map<String, Object> map = new HashMap<String, Object>();
map.put("paramKey", "paramValue"); eventManager.dispather("observerTest", map);
} finally {
if (ctx != null) {
ctx.close();
}
} }
}
运行结果:
thread name main:main
execute ObserverTest
paramKey->paramValue
thread name ObserverTest:Thread-0
里面的多线程操作可以换成线程池,spring 有一个类ThreadPoolTaskExecutor大家可以试一试