Java tutorial
/* * * Copyright 2018 FJN Corp. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * * Author Date Issue * fs1194361820@163.com 2015-01-01 Initial Version * */ package com.fjn.helper.frameworkex.apache.zookeeper.dispatcher; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.concurrent.BlockingQueue; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.LinkedBlockingQueue; import org.apache.zookeeper.WatchedEvent; import org.apache.zookeeper.Watcher; import org.apache.zookeeper.Watcher.Event.EventType; import org.apache.zookeeper.Watcher.Event.KeeperState; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class Dispatcher implements Watcher, Runnable { private static final Logger logger = LoggerFactory.getLogger(Dispatcher.class); private Map<ServiceType, Service> serviceMap = new ConcurrentHashMap<ServiceType, Service>(); Selector selector = Selector.open(); public void run() { logger.info("Startup ..."); dispatch(); } private void dispatch() { while (true) { List<Event> events = selector.select(); for (Event event : events) { Service service = serviceMap.get(event.eventType); service.doService(event); } } } public void registService(ServiceType type, Service service) { serviceMap.put(type, service); } public void unregistService(ServiceType type) { serviceMap.remove(type); } @Override public void process(WatchedEvent event) { event.getPath(); event.getState(); event.getType(); } void accept(WatchedEvent event) { boolean stateChanged = event.getPath() == null; boolean znodeChanged = event.getPath() != null; boolean dataChanged = event.getType() == EventType.NodeDataChanged || event.getType() == EventType.NodeDeleted || event.getType() == EventType.NodeCreated || event.getType() == EventType.NodeChildrenChanged; if (stateChanged) { if (event.getState() == KeeperState.Expired) { selector.addEvent(new Event(ServiceType.SESSION, new SourceWrapper(event))); } } if (znodeChanged) { } if (dataChanged) { } } static class Selector { private BlockingQueue<Event> eventQueue = new LinkedBlockingQueue<Event>(); private Object lock = new Object(); private Selector() { } List<Event> select() { return select(Long.MAX_VALUE); } public static Selector open() { return new Selector(); } List<Event> select(long timeout) { if (timeout > 0) { if (eventQueue.isEmpty()) { synchronized (lock) { if (eventQueue.isEmpty()) { try { lock.wait(timeout); } catch (InterruptedException e) { // ignore it } } } } } List<Event> events = new ArrayList<Event>(); eventQueue.drainTo(events); return events; } public void addEvent(Event e) { boolean success = eventQueue.offer(e); if (success) { synchronized (lock) { lock.notify(); } } } } class SourceWrapper { private WatchedEvent source; public SourceWrapper(WatchedEvent source) { this.source = source; } public KeeperState getState() { return source.getState(); } public String getPath() { return source.getPath(); } public EventType getType() { return source.getType(); } } class Event implements Comparable<Event> { private SourceWrapper source; private ServiceType eventType; Event(ServiceType type, SourceWrapper source) { this.eventType = type; this.source = source; } public SourceWrapper getSource() { return source; } @Override public int compareTo(Event o) { return this.eventType.priority() - o.eventType.priority(); } } enum ServiceType { SESSION(-1); private int priority; private ServiceType() { this(0); } private ServiceType(int priority) { this.priority = priority; } public int priority() { return priority; } } interface Service { void doService(Event event); } class SessionService implements Service { @Override public void doService(Event event) { } } }