import com.mars.util.ClassMethod; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.util.EventListener; import java.util.EventObject; import java.util.HashSet; import java.util.Set; import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; public class EventTest222 { public static void main(String[] args) { EventSource eventSource = new EventSource(); TestEventListener listener = new TestEventListener(); eventSource.addListener(listener); TestEventListener2 listener2 = new TestEventListener2(); eventSource.addListener(listener2); int count = 0; while (true) { if (count % 3 == 1) eventSource.publishEvent(new TestEvent(123, "一個事件")); else eventSource.publishEvent(new TestEvent2(456, "一個事件")); count++; if (count > 10000) break; } } } abstract class MarsEvent extends EventObject { public MarsEvent(Object source) { super(source); } public abstract String eventType(); public abstract String getMsg(); } class Constants { public static final String EVENT_TYPE_1 = "1"; public static final String EVENT_TYPE_2 = "2"; public static final String EVENT_TYPE_3 = "3"; } /** * 事件對象 */ class TestEvent extends MarsEvent { private String msg; /** * Constructs a prototypical Event. * * @param source The object on which the Event initially occurred. * @param msg 附加消息 * @throws IllegalArgumentException if source is null. */ public TestEvent(Object source, String msg) { super(source); this.msg = msg; } public String getMsg() { return "eventType = " + eventType() + " || " + this.msg; } @Override public String eventType() { return Constants.EVENT_TYPE_1; } } class TestEvent2 extends MarsEvent { public static final String EventType = "2"; private String msg; /** * Constructs a prototypical Event. * * @param source The object on which the Event initially occurred. * @param msg 附加消息 * @throws IllegalArgumentException if source is null. */ public TestEvent2(Object source, String msg) { super(source); this.msg = msg; } public String getMsg() { return "eventType = " + eventType() + " || " + this.msg; } @Override public String eventType() { return Constants.EVENT_TYPE_2; } } interface MarsEventListener extends EventListener { String eventType(); } /** * 事件監(jiān)聽者,按照 Java 規(guī)范應(yīng)實(shí)現(xiàn) EventListener 接口 */ class TestEventListener implements MarsEventListener { private static final Logger LOGGER = LoggerFactory.getLogger(TestEventListener.class); public void handleEvent(MarsEvent event) { LOGGER.info(ClassMethod.loggerInfo("TestEvent, msg is:" + event.getMsg())); } @Override public String eventType() { return Constants.EVENT_TYPE_1; } } class TestEventListener2 implements MarsEventListener { private static final Logger LOGGER = LoggerFactory.getLogger(TestEventListener2.class); public void handleEvent(MarsEvent event) { LOGGER.info(ClassMethod.loggerInfo("TestEvent222, msg222 is :" + event.getMsg())); } @Override public String eventType() { return Constants.EVENT_TYPE_2; } } /** * 事件源 */ class EventSource { private Set<MarsEventListener> listenerSet = new HashSet<>(); public void addListener(MarsEventListener listener) { listenerSet.add(listener); } public void publishEvent(MarsEvent event) { threadPool.submit(new HandleEventThread(event)); } public EventSource() { } private static final LinkedBlockingQueue<Runnable> threadQueue = new LinkedBlockingQueue<Runnable>(); private static final ThreadPoolExecutor threadPool = new ThreadPoolExecutor(3, 3, 60, TimeUnit.SECONDS, threadQueue, new ThreadPoolExecutor.DiscardOldestPolicy()); class HandleEventThread implements Runnable { private MarsEvent event; public HandleEventThread(MarsEvent event) { this.event = event; } public void run() { MarsEventListener listener = null; for (MarsEventListener eventListener : listenerSet) { if (event.eventType().equals(eventListener.eventType())) { listener = eventListener; break; } } if (Constants.EVENT_TYPE_1.equals(event.eventType())) { ((TestEventListener) listener).handleEvent(event); } else if (Constants.EVENT_TYPE_2.equals(event.eventType())) { ((TestEventListener2) listener).handleEvent(event); } } } } |
|