Search in sources :

Example 1 with ExceptionEvent

use of org.apache.servicecomb.serviceregistry.event.ExceptionEvent in project java-chassis by ServiceComb.

the class MicroserviceWatchTask method doRun.

@Override
public void doRun() {
    // will always run watch when it is ready
    if (!needToWatch()) {
        return;
    }
    srClient.watch(microservice.getServiceId(), (event) -> {
        if (event.failed()) {
            eventBus.post(new ExceptionEvent(event.cause()));
            return;
        }
        MicroserviceInstanceChangedEvent changedEvent = event.result();
        if (isProviderInstancesChanged(changedEvent) && !serviceRegistryConfig.isWatch()) {
            return;
        }
        if (!isProviderInstancesChanged(changedEvent) && !serviceRegistryConfig.isRegistryAutoDiscovery()) {
            return;
        }
        onMicroserviceInstanceChanged(changedEvent);
    }, open -> eventBus.post(new RecoveryEvent()), close -> {
    });
}
Also used : ExceptionEvent(org.apache.servicecomb.serviceregistry.event.ExceptionEvent) MicroserviceInstanceChangedEvent(org.apache.servicecomb.registry.api.event.MicroserviceInstanceChangedEvent) RecoveryEvent(org.apache.servicecomb.serviceregistry.event.RecoveryEvent)

Example 2 with ExceptionEvent

use of org.apache.servicecomb.serviceregistry.event.ExceptionEvent in project incubator-servicecomb-java-chassis by apache.

the class TestMicroserviceWatchTask method testWatchFailed.

@Test
public void testWatchFailed(@Mocked ServiceRegistryConfig serviceRegistryConfig, @Mocked ServiceRegistryClient srClient, @Mocked Microservice microservice) {
    initWatch(serviceRegistryConfig, srClient, microservice);
    new MockUp<ServiceRegistryClient>(srClient) {

        @Mock
        void watch(String selfMicroserviceId, AsyncResultCallback<MicroserviceInstanceChangedEvent> callback, AsyncResultCallback<Void> onOpen, AsyncResultCallback<Void> onClose) {
            callback.fail(new RuntimeExceptionWithoutStackTrace("test failed"));
        }
    };
    Holder<Throwable> holder = new Holder<>();
    eventBus.register(new Object() {

        @Subscribe
        public void onException(ExceptionEvent event) {
            holder.value = event.getThrowable();
        }
    });
    Assert.assertNull(holder.value);
    microserviceWatchTask.run();
    Assert.assertEquals("test failed", holder.value.getMessage());
}
Also used : RuntimeExceptionWithoutStackTrace(org.apache.servicecomb.foundation.test.scaffolding.exception.RuntimeExceptionWithoutStackTrace) ExceptionEvent(org.apache.servicecomb.serviceregistry.event.ExceptionEvent) Holder(org.apache.servicecomb.foundation.common.Holder) MockUp(mockit.MockUp) Subscribe(com.google.common.eventbus.Subscribe) AsyncResultCallback(org.apache.servicecomb.foundation.vertx.AsyncResultCallback) Test(org.junit.Test)

Example 3 with ExceptionEvent

use of org.apache.servicecomb.serviceregistry.event.ExceptionEvent in project incubator-servicecomb-java-chassis by apache.

the class MicroserviceWatchTask method doRun.

@Override
public void doRun() {
    // will always run watch when it is ready
    if (!needToWatch()) {
        return;
    }
    srClient.watch(microservice.getServiceId(), (event) -> {
        if (event.failed()) {
            eventBus.post(new ExceptionEvent(event.cause()));
            return;
        }
        MicroserviceInstanceChangedEvent changedEvent = event.result();
        if (isProviderInstancesChanged(changedEvent) && !serviceRegistryConfig.isWatch()) {
            return;
        }
        if (!isProviderInstancesChanged(changedEvent) && !serviceRegistryConfig.isRegistryAutoDiscovery()) {
            return;
        }
        onMicroserviceInstanceChanged(changedEvent);
    }, open -> eventBus.post(new RecoveryEvent()), close -> {
    });
}
Also used : ExceptionEvent(org.apache.servicecomb.serviceregistry.event.ExceptionEvent) MicroserviceInstanceChangedEvent(org.apache.servicecomb.registry.api.event.MicroserviceInstanceChangedEvent) RecoveryEvent(org.apache.servicecomb.serviceregistry.event.RecoveryEvent)

Example 4 with ExceptionEvent

use of org.apache.servicecomb.serviceregistry.event.ExceptionEvent in project java-chassis by ServiceComb.

the class TestMicroserviceWatchTask method testWatchFailed.

@Test
public void testWatchFailed(@Mocked ServiceRegistryConfig serviceRegistryConfig, @Mocked ServiceRegistryClient srClient, @Mocked Microservice microservice) {
    initWatch(serviceRegistryConfig, srClient, microservice);
    new MockUp<ServiceRegistryClient>(srClient) {

        @Mock
        void watch(String selfMicroserviceId, AsyncResultCallback<MicroserviceInstanceChangedEvent> callback, AsyncResultCallback<Void> onOpen, AsyncResultCallback<Void> onClose) {
            callback.fail(new RuntimeExceptionWithoutStackTrace("test failed"));
        }
    };
    Holder<Throwable> holder = new Holder<>();
    eventBus.register(new Object() {

        @Subscribe
        public void onException(ExceptionEvent event) {
            holder.value = event.getThrowable();
        }
    });
    Assert.assertNull(holder.value);
    microserviceWatchTask.run();
    Assert.assertEquals("test failed", holder.value.getMessage());
}
Also used : RuntimeExceptionWithoutStackTrace(org.apache.servicecomb.foundation.test.scaffolding.exception.RuntimeExceptionWithoutStackTrace) ExceptionEvent(org.apache.servicecomb.serviceregistry.event.ExceptionEvent) Holder(org.apache.servicecomb.foundation.common.Holder) MockUp(mockit.MockUp) Subscribe(com.google.common.eventbus.Subscribe) AsyncResultCallback(org.apache.servicecomb.foundation.vertx.AsyncResultCallback) Test(org.junit.Test)

Aggregations

ExceptionEvent (org.apache.servicecomb.serviceregistry.event.ExceptionEvent)4 Subscribe (com.google.common.eventbus.Subscribe)2 MockUp (mockit.MockUp)2 Holder (org.apache.servicecomb.foundation.common.Holder)2 RuntimeExceptionWithoutStackTrace (org.apache.servicecomb.foundation.test.scaffolding.exception.RuntimeExceptionWithoutStackTrace)2 AsyncResultCallback (org.apache.servicecomb.foundation.vertx.AsyncResultCallback)2 MicroserviceInstanceChangedEvent (org.apache.servicecomb.registry.api.event.MicroserviceInstanceChangedEvent)2 RecoveryEvent (org.apache.servicecomb.serviceregistry.event.RecoveryEvent)2 Test (org.junit.Test)2