Search in sources :

Example 1 with CommandListener

use of com.weibo.api.motan.registry.support.command.CommandListener in project motan by weibocom.

the class ZookeeperRegistry method unsubscribeCommand.

@Override
protected void unsubscribeCommand(URL url, CommandListener commandListener) {
    try {
        clientLock.lock();
        Map<CommandListener, IZkDataListener> dataChangeListeners = commandListeners.get(url);
        if (dataChangeListeners != null) {
            IZkDataListener zkDataListener = dataChangeListeners.get(commandListener);
            if (zkDataListener != null) {
                zkClient.unsubscribeDataChanges(ZkUtils.toCommandPath(url), zkDataListener);
                dataChangeListeners.remove(commandListener);
            }
        }
    } catch (Throwable e) {
        throw new MotanFrameworkException(String.format("Failed to unsubscribe command %s to zookeeper(%s), cause: %s", url, getUrl(), e.getMessage()), e);
    } finally {
        clientLock.unlock();
    }
}
Also used : CommandListener(com.weibo.api.motan.registry.support.command.CommandListener) MotanFrameworkException(com.weibo.api.motan.exception.MotanFrameworkException) IZkDataListener(org.I0Itec.zkclient.IZkDataListener)

Example 2 with CommandListener

use of com.weibo.api.motan.registry.support.command.CommandListener in project motan by weibocom.

the class ZookeeperRegistry method subscribeCommand.

@Override
protected void subscribeCommand(final URL url, final CommandListener commandListener) {
    try {
        clientLock.lock();
        ConcurrentHashMap<CommandListener, IZkDataListener> dataChangeListeners = commandListeners.get(url);
        if (dataChangeListeners == null) {
            commandListeners.putIfAbsent(url, new ConcurrentHashMap<CommandListener, IZkDataListener>());
            dataChangeListeners = commandListeners.get(url);
        }
        IZkDataListener zkDataListener = dataChangeListeners.get(commandListener);
        if (zkDataListener == null) {
            dataChangeListeners.putIfAbsent(commandListener, new IZkDataListener() {

                @Override
                public void handleDataChange(String dataPath, Object data) throws Exception {
                    commandListener.notifyCommand(url, (String) data);
                    LoggerUtil.info(String.format("[ZookeeperRegistry] command data change: path=%s, command=%s", dataPath, (String) data));
                }

                @Override
                public void handleDataDeleted(String dataPath) throws Exception {
                    commandListener.notifyCommand(url, null);
                    LoggerUtil.info(String.format("[ZookeeperRegistry] command deleted: path=%s", dataPath));
                }
            });
            zkDataListener = dataChangeListeners.get(commandListener);
        }
        String commandPath = ZkUtils.toCommandPath(url);
        zkClient.subscribeDataChanges(commandPath, zkDataListener);
        LoggerUtil.info(String.format("[ZookeeperRegistry] subscribe command: path=%s, info=%s", commandPath, url.toFullStr()));
    } catch (Throwable e) {
        throw new MotanFrameworkException(String.format("Failed to subscribe %s to zookeeper(%s), cause: %s", url, getUrl(), e.getMessage()), e);
    } finally {
        clientLock.unlock();
    }
}
Also used : CommandListener(com.weibo.api.motan.registry.support.command.CommandListener) MotanFrameworkException(com.weibo.api.motan.exception.MotanFrameworkException) IZkDataListener(org.I0Itec.zkclient.IZkDataListener) MotanFrameworkException(com.weibo.api.motan.exception.MotanFrameworkException)

Example 3 with CommandListener

use of com.weibo.api.motan.registry.support.command.CommandListener in project motan by weibocom.

the class ZookeeperRegistryTest method subAndUnsubCommand.

@Test
public void subAndUnsubCommand() throws Exception {
    final String command = "{\"index\":0,\"mergeGroups\":[\"aaa:1\",\"bbb:1\"],\"pattern\":\"*\",\"routeRules\":[]}\n";
    CommandListener commandListener = new CommandListener() {

        @Override
        public void notifyCommand(URL refUrl, String commandString) {
            if (commandString != null) {
                Assert.assertTrue(commandString.equals(command));
            }
        }
    };
    registry.subscribeCommand(clientUrl, commandListener);
    Assert.assertTrue(containsCommandListener(clientUrl, commandListener));
    String commandPath = ZkUtils.toCommandPath(clientUrl);
    if (!zkClient.exists(commandPath)) {
        zkClient.createPersistent(commandPath, true);
    }
    zkClient.writeData(commandPath, command);
    Thread.sleep(2000);
    zkClient.delete(commandPath);
    registry.unsubscribeCommand(clientUrl, commandListener);
    Assert.assertFalse(containsCommandListener(clientUrl, commandListener));
}
Also used : CommandListener(com.weibo.api.motan.registry.support.command.CommandListener) URL(com.weibo.api.motan.rpc.URL) Test(org.junit.Test)

Example 4 with CommandListener

use of com.weibo.api.motan.registry.support.command.CommandListener in project motan by weibocom.

the class ZookeeperRegistry method reconnectClient.

@SuppressWarnings("rawtypes")
private void reconnectClient() {
    if (serviceListeners != null && !serviceListeners.isEmpty()) {
        try {
            clientLock.lock();
            for (Map.Entry entry : serviceListeners.entrySet()) {
                URL url = (URL) entry.getKey();
                ConcurrentHashMap<ServiceListener, IZkChildListener> childChangeListeners = serviceListeners.get(url);
                if (childChangeListeners != null) {
                    for (Map.Entry e : childChangeListeners.entrySet()) {
                        subscribeService(url, (ServiceListener) e.getKey());
                    }
                }
            }
            for (Map.Entry entry : commandListeners.entrySet()) {
                URL url = (URL) entry.getKey();
                ConcurrentHashMap<CommandListener, IZkDataListener> dataChangeListeners = commandListeners.get(url);
                if (dataChangeListeners != null) {
                    for (Map.Entry e : dataChangeListeners.entrySet()) {
                        subscribeCommand(url, (CommandListener) e.getKey());
                    }
                }
            }
            LoggerUtil.info("[{}] reconnect all clients", registryClassName);
        } finally {
            clientLock.unlock();
        }
    }
}
Also used : CommandListener(com.weibo.api.motan.registry.support.command.CommandListener) ServiceListener(com.weibo.api.motan.registry.support.command.ServiceListener) IZkChildListener(org.I0Itec.zkclient.IZkChildListener) IZkDataListener(org.I0Itec.zkclient.IZkDataListener) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) URL(com.weibo.api.motan.rpc.URL)

Example 5 with CommandListener

use of com.weibo.api.motan.registry.support.command.CommandListener in project motan by weibocom.

the class ConsulRegistryTest method subAndUnsubCommand.

@Test
public void subAndUnsubCommand() throws Exception {
    final String command = "{\"index\":0,\"mergeGroups\":[\"aaa:1\",\"bbb:1\"],\"pattern\":\"*\",\"routeRules\":[]}\n";
    CommandListener commandListener = new CommandListener() {

        @Override
        public void notifyCommand(URL refUrl, String commandString) {
            if (commandString != null) {
                Assert.assertTrue(commandString.equals(command));
            }
        }
    };
    registry.subscribeCommand(clientUrl, commandListener);
    Assert.assertTrue(containsCommandListener(serviceUrl, clientUrl, commandListener));
    client.setKVValue(clientUrl.getGroup(), command);
    Thread.sleep(2000);
    client.removeKVValue(clientUrl.getGroup());
    registry.unsubscribeCommand(clientUrl, commandListener);
    Assert.assertFalse(containsCommandListener(serviceUrl, clientUrl, commandListener));
}
Also used : CommandListener(com.weibo.api.motan.registry.support.command.CommandListener) URL(com.weibo.api.motan.rpc.URL) Test(org.junit.Test)

Aggregations

CommandListener (com.weibo.api.motan.registry.support.command.CommandListener)5 URL (com.weibo.api.motan.rpc.URL)3 IZkDataListener (org.I0Itec.zkclient.IZkDataListener)3 MotanFrameworkException (com.weibo.api.motan.exception.MotanFrameworkException)2 Test (org.junit.Test)2 ServiceListener (com.weibo.api.motan.registry.support.command.ServiceListener)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 IZkChildListener (org.I0Itec.zkclient.IZkChildListener)1