forked from apache/dubbo
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Polish apache#3942 : Add Event Publishing ServiceRegistry implementation
- Loading branch information
1 parent
7ec8cf7
commit 19fe9f7
Showing
7 changed files
with
379 additions
and
13 deletions.
There are no files selected for viewing
145 changes: 145 additions & 0 deletions
145
...ry-api/src/main/java/org/apache/dubbo/registry/client/EventPublishingServiceRegistry.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
...gistry-api/src/main/java/org/apache/dubbo/registry/client/event/ServiceInstanceEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
...c/main/java/org/apache/dubbo/registry/client/event/ServiceInstancePreRegisteredEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
.../src/main/java/org/apache/dubbo/registry/client/event/ServiceInstanceRegisteredEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.