Skip to content

Commit

Permalink
Polish apache#3942 : Add Event Publishing ServiceRegistry implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
mercyblitz committed May 17, 2019
1 parent 7ec8cf7 commit 19fe9f7
Show file tree
Hide file tree
Showing 7 changed files with 379 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.dubbo.registry.client;

import org.apache.dubbo.common.event.Event;
import org.apache.dubbo.common.event.EventDispatcher;
import org.apache.dubbo.common.event.EventListener;
import org.apache.dubbo.registry.client.event.ServiceInstancePreRegisteredEvent;
import org.apache.dubbo.registry.client.event.ServiceInstanceRegisteredEvent;

import java.util.List;
import java.util.Optional;

import static java.util.Optional.empty;
import static java.util.Optional.of;

/**
* The abstract {@link ServiceRegistry} implementation publishes the {@link Event Dubbo event} on methods executing.
*
* @since 2.7.2
*/
public abstract class EventPublishingServiceRegistry implements ServiceRegistry {

private final EventDispatcher eventDispatcher = EventDispatcher.getDefaultExtension();

@Override
public final void register(ServiceInstance serviceInstance) throws RuntimeException {
executeWithEvents(
of(new ServiceInstancePreRegisteredEvent(this, serviceInstance)),
() -> doRegister(serviceInstance),
of(new ServiceInstanceRegisteredEvent(this, serviceInstance))
);
}

@Override
public final void update(ServiceInstance serviceInstance) throws RuntimeException {
// TODO publish event
executeWithEvents(
empty(),
() -> doUpdate(serviceInstance),
empty()
);
}

@Override
public final void unregister(ServiceInstance serviceInstance) throws RuntimeException {
// TODO publish event
executeWithEvents(
empty(),
() -> doUnregister(serviceInstance),
empty()
);
}

@Override
public final void start() {
// TODO publish event
executeWithEvents(
empty(),
this::doStart,
empty()
);
}

@Override
public final void stop() {
// TODO publish event
executeWithEvents(
empty(),
this::doStop,
empty()
);
}

protected final void executeWithEvents(Optional<? extends Event> beforeEvent,
Runnable action,
Optional<? extends Event> afterEvent) {
beforeEvent.ifPresent(eventDispatcher::dispatch);
action.run();
afterEvent.ifPresent(eventDispatcher::dispatch);
}

/**
* Registers an instance of {@link ServiceInstance}.
*
* @param serviceInstance an instance of {@link ServiceInstance} to be registered
* @throws RuntimeException if failed
*/
protected abstract void doRegister(ServiceInstance serviceInstance) throws RuntimeException;

/**
* Updates the registered {@link ServiceInstance}.
*
* @param serviceInstance the registered {@link ServiceInstance}
* @throws RuntimeException if failed
*/
protected abstract void doUpdate(ServiceInstance serviceInstance) throws RuntimeException;

/**
* Unregisters an instance of {@link ServiceInstance}.
*
* @param serviceInstance an instance of {@link ServiceInstance} to be deregistered
* @throws RuntimeException if failed
*/
protected abstract void doUnregister(ServiceInstance serviceInstance) throws RuntimeException;

/**
* Starts the ServiceRegistry. This is a lifecycle method.
*/
protected abstract void doStart();

/**
* Stops the ServiceRegistry. This is a lifecycle method.
*/
protected abstract void doStop();

@Override
public void addEventListener(EventListener<?> listener) throws NullPointerException, IllegalArgumentException {
eventDispatcher.addEventListener(listener);
}

@Override
public void removeEventListener(EventListener<?> listener) throws NullPointerException, IllegalArgumentException {
eventDispatcher.removeEventListener(listener);
}

@Override
public List<EventListener<?>> getAllEventListeners() {
return eventDispatcher.getAllEventListeners();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@
*/
package org.apache.dubbo.registry.client;

import org.apache.dubbo.common.event.Listenable;

/**
* The common interface to register and unregister for a service registry
*
* @since 2.7.2
*/
public interface ServiceRegistry {
public interface ServiceRegistry extends Listenable {

/**
* A human-readable description of the implementation
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.dubbo.registry.client.event;

import org.apache.dubbo.common.event.Event;
import org.apache.dubbo.registry.client.ServiceInstance;

/**
* The {@link Event Dubbo event} for {@link ServiceInstance an service instance}
*
* @since 2.7.2
*/
public abstract class ServiceInstanceEvent extends Event {

private final ServiceInstance serviceInstance;

/**
* @param serviceInstance {@link ServiceInstance an service instance}
*/
public ServiceInstanceEvent(Object source, ServiceInstance serviceInstance) {
super(source);
this.serviceInstance = serviceInstance;
}

/**
* Get current {@link ServiceInstance service instance}
*
* @return current {@link ServiceInstance service instance}
*/
public ServiceInstance getServiceInstance() {
return serviceInstance;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.dubbo.registry.client.event;

import org.apache.dubbo.registry.client.ServiceInstance;
import org.apache.dubbo.registry.client.ServiceRegistry;


/**
* The before-{@link ServiceRegistry#register(ServiceInstance) register} event for {@link ServiceInstance}
*
* @since 2.7.2
*/
public class ServiceInstancePreRegisteredEvent extends ServiceInstanceEvent {

public ServiceInstancePreRegisteredEvent(Object source, ServiceInstance serviceInstance) {
super(source, serviceInstance);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.dubbo.registry.client.event;

import org.apache.dubbo.registry.client.ServiceInstance;
import org.apache.dubbo.registry.client.ServiceRegistry;


/**
* The after-{@link ServiceRegistry#register(ServiceInstance) register} event for {@link ServiceInstance}
*
* @since 2.7.2
*/
public class ServiceInstanceRegisteredEvent extends ServiceInstanceEvent {

public ServiceInstanceRegisteredEvent(Object source, ServiceInstance serviceInstance) {
super(source, serviceInstance);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,26 +29,26 @@
*/
public class DefaultServiceInstanceTest {

private final DefaultServiceInstance serviceInstance =
public static final DefaultServiceInstance INSTANCE =
new DefaultServiceInstance("A", "127.0.0.1", 8080);

@Test
public void testDefaultValues() {
assertTrue(serviceInstance.isEnabled());
assertTrue(serviceInstance.isHealthy());
assertTrue(serviceInstance.getMetadata().isEmpty());
assertTrue(INSTANCE.isEnabled());
assertTrue(INSTANCE.isHealthy());
assertTrue(INSTANCE.getMetadata().isEmpty());
}

@Test
public void testSetAndGetValues() {
serviceInstance.setEnabled(false);
serviceInstance.setHealthy(false);
INSTANCE.setEnabled(false);
INSTANCE.setHealthy(false);

assertEquals("A", serviceInstance.getServiceName());
assertEquals("127.0.0.1", serviceInstance.getHost());
assertEquals(8080, serviceInstance.getPort());
assertFalse(serviceInstance.isEnabled());
assertFalse(serviceInstance.isHealthy());
assertTrue(serviceInstance.getMetadata().isEmpty());
assertEquals("A", INSTANCE.getServiceName());
assertEquals("127.0.0.1", INSTANCE.getHost());
assertEquals(8080, INSTANCE.getPort());
assertFalse(INSTANCE.isEnabled());
assertFalse(INSTANCE.isHealthy());
assertTrue(INSTANCE.getMetadata().isEmpty());
}
}

0 comments on commit 19fe9f7

Please sign in to comment.