-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* For #10374, Support listener to get changed instances * Update AbstractNamingChangeListener,NamingChaneEventTest * Update InstancesDiffTest * Rename NamingChangeEventTest
- Loading branch information
Showing
10 changed files
with
412 additions
and
18 deletions.
There are no files selected for viewing
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
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
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
80 changes: 80 additions & 0 deletions
80
client/src/main/java/com/alibaba/nacos/client/naming/event/InstancesDiff.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,80 @@ | ||
/* | ||
* Copyright 1999-2023 Alibaba Group Holding Ltd. | ||
* | ||
* Licensed 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 com.alibaba.nacos.client.naming.event; | ||
|
||
import com.alibaba.nacos.api.naming.pojo.Instance; | ||
import com.alibaba.nacos.common.utils.CollectionUtils; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.List; | ||
|
||
/** | ||
* The differences in instances compared to the last callback. | ||
* | ||
* @author lideyou | ||
*/ | ||
public class InstancesDiff { | ||
private final List<Instance> addedInstances = new ArrayList<>(); | ||
|
||
private final List<Instance> removedInstances = new ArrayList<>(); | ||
|
||
private final List<Instance> modifiedInstances = new ArrayList<>(); | ||
|
||
public List<Instance> getAddedInstances() { | ||
return addedInstances; | ||
} | ||
|
||
public void setAddedInstances(Collection<Instance> addedInstances) { | ||
this.addedInstances.clear(); | ||
if (CollectionUtils.isNotEmpty(addedInstances)) { | ||
this.addedInstances.addAll(addedInstances); | ||
} | ||
} | ||
|
||
public List<Instance> getRemovedInstances() { | ||
return removedInstances; | ||
} | ||
|
||
public void setRemovedInstances(Collection<Instance> removedInstances) { | ||
this.removedInstances.clear(); | ||
if (CollectionUtils.isNotEmpty(removedInstances)) { | ||
this.removedInstances.addAll(removedInstances); | ||
} | ||
} | ||
|
||
public List<Instance> getModifiedInstances() { | ||
return modifiedInstances; | ||
} | ||
|
||
public void setModifiedInstances(Collection<Instance> modifiedInstances) { | ||
this.modifiedInstances.clear(); | ||
if (CollectionUtils.isNotEmpty(modifiedInstances)) { | ||
this.modifiedInstances.addAll(modifiedInstances); | ||
} | ||
} | ||
|
||
/** | ||
* Check if any instances have changed. | ||
* @return true if there are instances that have changed | ||
*/ | ||
public boolean hasDifferent() { | ||
return CollectionUtils.isNotEmpty(this.addedInstances) | ||
|| CollectionUtils.isNotEmpty(this.removedInstances) | ||
|| CollectionUtils.isNotEmpty(this.modifiedInstances); | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
.../src/main/java/com/alibaba/nacos/client/naming/listener/AbstractNamingChangeListener.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,40 @@ | ||
/* | ||
* Copyright 1999-2023 Alibaba Group Holding Ltd. | ||
* | ||
* Licensed 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 com.alibaba.nacos.client.naming.listener; | ||
|
||
import com.alibaba.nacos.api.naming.listener.AbstractEventListener; | ||
import com.alibaba.nacos.api.naming.listener.Event; | ||
|
||
/** | ||
* Listener for NamingChangeEvent. | ||
* | ||
* @author lideyou | ||
*/ | ||
public abstract class AbstractNamingChangeListener extends AbstractEventListener { | ||
@Override | ||
public final void onEvent(Event event) { | ||
if (event instanceof NamingChangeEvent) { | ||
onChange((NamingChangeEvent) event); | ||
} | ||
} | ||
|
||
/** | ||
* Callback when instances have changed. | ||
* @param event NamingChangeEvent | ||
*/ | ||
public abstract void onChange(NamingChangeEvent event); | ||
} |
67 changes: 67 additions & 0 deletions
67
client/src/main/java/com/alibaba/nacos/client/naming/listener/NamingChangeEvent.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,67 @@ | ||
/* | ||
* Copyright 1999-2023 Alibaba Group Holding Ltd. | ||
* | ||
* Licensed 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 com.alibaba.nacos.client.naming.listener; | ||
|
||
import com.alibaba.nacos.api.naming.listener.NamingEvent; | ||
import com.alibaba.nacos.api.naming.pojo.Instance; | ||
import com.alibaba.nacos.client.naming.event.InstancesDiff; | ||
import com.alibaba.nacos.common.utils.CollectionUtils; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* Naming Event with instance change information. | ||
* | ||
* @author lideyou | ||
*/ | ||
public class NamingChangeEvent extends NamingEvent { | ||
private final InstancesDiff instancesDiff; | ||
|
||
public NamingChangeEvent(String serviceName, List<Instance> instances, InstancesDiff instancesDiff) { | ||
super(serviceName, instances); | ||
this.instancesDiff = instancesDiff; | ||
} | ||
|
||
public NamingChangeEvent(String serviceName, String groupName, String clusters, List<Instance> instances, InstancesDiff instancesDiff) { | ||
super(serviceName, groupName, clusters, instances); | ||
this.instancesDiff = instancesDiff; | ||
} | ||
|
||
public boolean isAdded() { | ||
return CollectionUtils.isNotEmpty(this.instancesDiff.getAddedInstances()); | ||
} | ||
|
||
public boolean isRemoved() { | ||
return CollectionUtils.isNotEmpty(this.instancesDiff.getRemovedInstances()); | ||
} | ||
|
||
public boolean isModified() { | ||
return CollectionUtils.isNotEmpty(this.instancesDiff.getModifiedInstances()); | ||
} | ||
|
||
public List<Instance> getAddedInstances() { | ||
return this.instancesDiff.getAddedInstances(); | ||
} | ||
|
||
public List<Instance> getRemovedInstances() { | ||
return this.instancesDiff.getRemovedInstances(); | ||
} | ||
|
||
public List<Instance> getModifiedInstances() { | ||
return this.instancesDiff.getModifiedInstances(); | ||
} | ||
} |
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
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.