use of com.alibaba.nacos.api.naming.listener.Event in project nacos by alibaba.
the class NacosNamingServiceTest method testSubscribe1.
@Test
public void testSubscribe1() throws NacosException {
// given
String serviceName = "service1";
EventListener listener = new EventListener() {
@Override
public void onEvent(Event event) {
}
};
// when
client.subscribe(serviceName, listener);
// then
verify(changeNotifier, times(1)).registerListener(Constants.DEFAULT_GROUP, serviceName, "", listener);
verify(proxy, times(1)).subscribe(serviceName, Constants.DEFAULT_GROUP, "");
}
use of com.alibaba.nacos.api.naming.listener.Event in project nacos by alibaba.
the class NamingExample method main.
public static void main(String[] args) throws NacosException, InterruptedException {
Properties properties = new Properties();
properties.setProperty("serverAddr", System.getProperty("serverAddr"));
properties.setProperty("namespace", System.getProperty("namespace"));
NamingService naming = NamingFactory.createNamingService(properties);
naming.registerInstance("nacos.test.3", "11.11.11.11", 8888, "TEST1");
System.out.println("instances after register: " + naming.getAllInstances("nacos.test.3"));
Executor executor = new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(), new ThreadFactory() {
@Override
public Thread newThread(Runnable r) {
Thread thread = new Thread(r);
thread.setName("test-thread");
return thread;
}
});
naming.subscribe("nacos.test.3", new AbstractEventListener() {
// EventListener onEvent is sync to handle, If process too low in onEvent, maybe block other onEvent callback.
// So you can override getExecutor() to async handle event.
@Override
public Executor getExecutor() {
return executor;
}
@Override
public void onEvent(Event event) {
System.out.println("serviceName: " + ((NamingEvent) event).getServiceName());
System.out.println("instances from event: " + ((NamingEvent) event).getInstances());
}
});
naming.deregisterInstance("nacos.test.3", "11.11.11.11", 8888, "TEST1");
Thread.sleep(1000);
System.out.println("instances after deregister: " + naming.getAllInstances("nacos.test.3"));
Thread.sleep(1000);
}
use of com.alibaba.nacos.api.naming.listener.Event in project nacos by alibaba.
the class MultiTenant_ITCase method multipleTenant_unSubscribe.
/**
* @TCDescription : 多租户取消订阅服务
* @TestStep :
* @ExpectResult :
*/
@Test
public void multipleTenant_unSubscribe() throws Exception {
String serviceName = randomDomainName();
EventListener listener = new EventListener() {
@Override
public void onEvent(Event event) {
System.out.println(((NamingEvent) event).getServiceName());
instances = ((NamingEvent) event).getInstances();
}
};
naming1.subscribe(serviceName, listener);
naming1.registerInstance(serviceName, "11.11.11.11", TEST_PORT, "c1");
naming2.registerInstance(serviceName, "33.33.33.33", TEST_PORT, "c1");
while (instances.size() == 0) {
TimeUnit.SECONDS.sleep(1L);
}
Assert.assertEquals(serviceName, naming1.getSubscribeServices().get(0).getName());
Assert.assertEquals(0, naming2.getSubscribeServices().size());
naming1.unsubscribe(serviceName, listener);
TimeUnit.SECONDS.sleep(5L);
Assert.assertEquals(0, naming1.getSubscribeServices().size());
Assert.assertEquals(0, naming2.getSubscribeServices().size());
}
use of com.alibaba.nacos.api.naming.listener.Event in project nacos by alibaba.
the class ServiceListTest_ITCase method getSubscribeServices_deregisterInstance.
/**
* @throws NacosException
* @description 删除注册,获取当前订阅的所有服务
*/
@Test
public void getSubscribeServices_deregisterInstance() throws NacosException, InterruptedException {
listenseCount = 0;
EventListener listener = new EventListener() {
@Override
public void onEvent(Event event) {
System.out.println(((NamingEvent) event).getServiceName());
System.out.println(((NamingEvent) event).getInstances());
listenseCount++;
}
};
List<ServiceInfo> serviceInfoList = naming.getSubscribeServices();
int count = serviceInfoList.size();
String serviceName = randomDomainName();
naming.registerInstance(serviceName, "127.0.0.1", TEST_PORT, "c1");
naming.subscribe(serviceName, listener);
serviceInfoList = naming.getSubscribeServices();
Assert.assertEquals(count + 1, serviceInfoList.size());
naming.deregisterInstance(serviceName, "127.0.0.1", TEST_PORT, "c1");
Assert.assertEquals(count + 1, serviceInfoList.size());
}
use of com.alibaba.nacos.api.naming.listener.Event in project nacos by alibaba.
the class ServiceListTest_ITCase method getSubscribeServices.
/**
* @throws NacosException
* @description 获取当前订阅的所有服务
*/
@Test
public void getSubscribeServices() throws NacosException, InterruptedException {
ListView<String> listView = naming.getServicesOfServer(1, 10);
if (listView != null && listView.getCount() > 0) {
naming.getAllInstances(listView.getData().get(0));
}
List<ServiceInfo> serviceInfoList = naming.getSubscribeServices();
int count = serviceInfoList.size();
String serviceName = randomDomainName();
naming.registerInstance(serviceName, "127.0.0.1", TEST_PORT, "c1");
naming.subscribe(serviceName, new EventListener() {
@Override
public void onEvent(Event event) {
}
});
serviceInfoList = naming.getSubscribeServices();
Assert.assertEquals(count + 1, serviceInfoList.size());
}
Aggregations