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();
}
}
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();
}
}
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));
}
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();
}
}
}
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));
}
Aggregations