Java tutorial
/* * Copyright 2015-2101 gaoxianglong * * 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. */ package com.gxl.shark.resources.watcher; import javax.annotation.Resource; import org.apache.zookeeper.WatchedEvent; import org.apache.zookeeper.Watcher; import org.apache.zookeeper.Watcher.Event.EventType; import org.apache.zookeeper.ZooKeeper; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component; import com.gxl.shark.exception.ResourceException; import com.gxl.shark.resources.conn.DataSourceBean; import com.gxl.shark.resources.register.bean.RegisterBean; /** * sharding???zookeeperwatcher * * @author gaoxianglong * * @version 1.3.7 */ @Component public class ZookeeperWatcher implements Watcher { @Resource(name = "registerDataSource") private RegisterBean registerBean; private ZooKeeper zk_client; private DataSourceBean dataSourceBean; private String nodePath; private Logger logger = LoggerFactory.getLogger(ZookeeperWatcher.class); public void init(ZooKeeper zk_client, DataSourceBean dataSourceBean) { init(zk_client, dataSourceBean.getNodePath()); } public void init(ZooKeeper zk_client, String nodePath) { this.zk_client = zk_client; this.nodePath = nodePath; } @Override public void process(WatchedEvent event) { if (null == zk_client) return; try { Thread.sleep(100); /* ? */ zk_client.exists(nodePath, this); EventType eventType = event.getType(); final String VALUE = "zookeeper?"; switch (eventType) { case NodeCreated: logger.info(VALUE + "[" + event.getPath() + "]"); break; case NodeDataChanged: String nodePathValue = new String(zk_client.getData(nodePath, false, null)); registerBean.register(nodePathValue); logger.info(VALUE + "[" + event.getPath() + "]???"); break; case NodeChildrenChanged: logger.info(VALUE + "[" + event.getPath() + "]???"); break; case NodeDeleted: logger.info(VALUE + "[" + event.getPath() + "]"); default: break; } } catch (Exception e) { throw new ResourceException("zookeeper??[" + e.toString() + "]"); } } }