Search in sources :

Example 6 with FrameworkException

use of com.networknt.exception.FrameworkException in project light-4j by networknt.

the class ZooKeeperRegistry method doRegister.

@Override
protected void doRegister(URL url) {
    try {
        serverLock.lock();
        removeNode(url, ZkNodeType.AVAILABLE_SERVER);
        removeNode(url, ZkNodeType.UNAVAILABLE_SERVER);
        createNode(url, ZkNodeType.UNAVAILABLE_SERVER);
    } catch (Throwable e) {
        throw new FrameworkException(new Status(REGISTER_ZOOKEEPER_ERROR, url, getUrl(), e.getMessage()), e);
    } finally {
        serverLock.unlock();
    }
}
Also used : Status(com.networknt.status.Status) FrameworkException(com.networknt.exception.FrameworkException)

Example 7 with FrameworkException

use of com.networknt.exception.FrameworkException in project light-4j by networknt.

the class ZooKeeperRegistry method discoverService.

@Override
protected List<URL> discoverService(URL url) {
    try {
        String parentPath = ZkUtils.toNodeTypePath(url, ZkNodeType.AVAILABLE_SERVER);
        List<String> currentChilds = new ArrayList<String>();
        if (client.exists(parentPath)) {
            currentChilds = client.getChildren(parentPath);
        }
        return nodeChildsToUrls(parentPath, currentChilds);
    } catch (Throwable e) {
        throw new FrameworkException(new Status(DISCOVER_ZOOKEEPER_SERVICE_ERROR, url, getUrl(), e.getMessage()), e);
    }
}
Also used : Status(com.networknt.status.Status) FrameworkException(com.networknt.exception.FrameworkException) ArrayList(java.util.ArrayList)

Example 8 with FrameworkException

use of com.networknt.exception.FrameworkException in project light-4j by networknt.

the class ZooKeeperRegistry method subscribeService.

@Override
protected void subscribeService(final URL url, final ServiceListener serviceListener) {
    try {
        clientLock.lock();
        ConcurrentHashMap<ServiceListener, IZkChildListener> childChangeListeners = serviceListeners.get(url);
        if (childChangeListeners == null) {
            serviceListeners.putIfAbsent(url, new ConcurrentHashMap<ServiceListener, IZkChildListener>());
            childChangeListeners = serviceListeners.get(url);
        }
        IZkChildListener zkChildListener = childChangeListeners.get(serviceListener);
        if (zkChildListener == null) {
            childChangeListeners.putIfAbsent(serviceListener, new IZkChildListener() {

                @Override
                public void handleChildChange(String parentPath, List<String> currentChilds) {
                    serviceListener.notifyService(url, getUrl(), nodeChildsToUrls(parentPath, currentChilds));
                    if (logger.isInfoEnabled())
                        logger.info(String.format("[ZooKeeperRegistry] service list change: path=%s, currentChilds=%s", parentPath, currentChilds.toString()));
                }
            });
            zkChildListener = childChangeListeners.get(serviceListener);
        }
        // prevent old node unregistered
        removeNode(url, ZkNodeType.CLIENT);
        createNode(url, ZkNodeType.CLIENT);
        String serverTypePath = ZkUtils.toNodeTypePath(url, ZkNodeType.AVAILABLE_SERVER);
        client.subscribeChildChanges(serverTypePath, zkChildListener);
        if (logger.isInfoEnabled())
            logger.info(String.format("[ZooKeeperRegistry] subscribe service: path=%s, info=%s", ZkUtils.toNodePath(url, ZkNodeType.AVAILABLE_SERVER), url.toFullStr()));
    } catch (Throwable e) {
        throw new FrameworkException(new Status(SUBSCRIBE_ZOOKEEPER_SERVICE_ERROR, url, getUrl(), e.getMessage()), e);
    } finally {
        clientLock.unlock();
    }
}
Also used : Status(com.networknt.status.Status) ServiceListener(com.networknt.registry.support.command.ServiceListener) FrameworkException(com.networknt.exception.FrameworkException) IZkChildListener(org.I0Itec.zkclient.IZkChildListener)

Example 9 with FrameworkException

use of com.networknt.exception.FrameworkException in project light-4j by networknt.

the class ZooKeeperRegistry method unsubscribeService.

@Override
protected void unsubscribeService(URL url, ServiceListener serviceListener) {
    try {
        clientLock.lock();
        Map<ServiceListener, IZkChildListener> childChangeListeners = serviceListeners.get(url);
        if (childChangeListeners != null) {
            IZkChildListener zkChildListener = childChangeListeners.get(serviceListener);
            if (zkChildListener != null) {
                client.unsubscribeChildChanges(ZkUtils.toNodeTypePath(url, ZkNodeType.CLIENT), zkChildListener);
                childChangeListeners.remove(serviceListener);
            }
        }
    } catch (Throwable e) {
        throw new FrameworkException(new Status(UNSUBSCRIBE_ZOOKEEPER_SERVICE_ERROR, url, getUrl(), e.getMessage()), e);
    } finally {
        clientLock.unlock();
    }
}
Also used : Status(com.networknt.status.Status) ServiceListener(com.networknt.registry.support.command.ServiceListener) FrameworkException(com.networknt.exception.FrameworkException) IZkChildListener(org.I0Itec.zkclient.IZkChildListener)

Aggregations

FrameworkException (com.networknt.exception.FrameworkException)9 Status (com.networknt.status.Status)9 ServiceListener (com.networknt.registry.support.command.ServiceListener)2 ArrayList (java.util.ArrayList)2 IZkChildListener (org.I0Itec.zkclient.IZkChildListener)2 NotifyListener (com.networknt.registry.NotifyListener)1 URL (com.networknt.registry.URL)1