Commit fc0312eb by Nepxion

把版本更新和清除拆分成两个事件

parent 31847c48
......@@ -33,7 +33,8 @@ import org.springframework.web.bind.annotation.RestController;
import com.nepxion.discovery.plugin.framework.adapter.PluginAdapter;
import com.nepxion.discovery.plugin.framework.context.PluginContextAware;
import com.nepxion.discovery.plugin.framework.event.PluginEventWapper;
import com.nepxion.discovery.plugin.framework.event.VersionChangedEvent;
import com.nepxion.discovery.plugin.framework.event.VersionClearedEvent;
import com.nepxion.discovery.plugin.framework.event.VersionUpdatedEvent;
@RestController
@Api(tags = { "版本接口" })
......@@ -58,7 +59,7 @@ public class VersionEndpoint implements MvcEndpoint {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Discovery control is disabled");
}
pluginEventWapper.fireVersionChanged(new VersionChangedEvent(VersionChangedEvent.EventType.VERSION_UPDATE, version), true);
pluginEventWapper.fireVersionUpdated(new VersionUpdatedEvent(version), true);
return ResponseEntity.ok().body("OK");
}
......@@ -73,7 +74,7 @@ public class VersionEndpoint implements MvcEndpoint {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Discovery control is disabled");
}
pluginEventWapper.fireVersionChanged(new VersionChangedEvent(VersionChangedEvent.EventType.VERSION_CLEAR), true);
pluginEventWapper.fireVersionCleared(new VersionClearedEvent(), true);
return ResponseEntity.ok().body("OK");
}
......
......@@ -26,11 +26,19 @@ public class PluginEventWapper {
}
}
public void fireVersionChanged(VersionChangedEvent versionChangedEvent, boolean async) {
public void fireVersionUpdated(VersionUpdatedEvent versionUpdatedEvent, boolean async) {
if (async) {
pluginPublisher.asyncPublish(versionChangedEvent);
pluginPublisher.asyncPublish(versionUpdatedEvent);
} else {
pluginSubscriber.onVersionChanged(versionChangedEvent);
pluginSubscriber.onVersionUpdated(versionUpdatedEvent);
}
}
public void fireVersionCleared(VersionClearedEvent versionClearedEvent, boolean async) {
if (async) {
pluginPublisher.asyncPublish(versionClearedEvent);
} else {
pluginSubscriber.onVersionCleared(versionClearedEvent);
}
}
}
\ No newline at end of file
......@@ -11,6 +11,7 @@ package com.nepxion.discovery.plugin.framework.event;
import java.io.InputStream;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
......@@ -57,7 +58,7 @@ public class PluginSubscriber {
return;
}
LOG.info("********** Remote config change has been subscribed **********");
LOG.info("********** Remote config changing has been triggered **********");
if (ruleChangedEvent == null) {
throw new PluginException("RuleChangedEvent can't be null");
......@@ -76,7 +77,7 @@ public class PluginSubscriber {
}
@Subscribe
public void onVersionChanged(VersionChangedEvent versionChangedEvent) {
public void onVersionUpdated(VersionUpdatedEvent versionUpdatedEvent) {
Boolean discoveryControlEnabled = pluginContextAware.isDiscoveryControlEnabled();
if (!discoveryControlEnabled) {
LOG.info("********** Discovery control is disabled, ignore to subscribe **********");
......@@ -84,30 +85,64 @@ public class PluginSubscriber {
return;
}
LOG.info("********** Version change has been subscribed **********");
LOG.info("********** Version updating has been triggered **********");
if (versionChangedEvent == null) {
throw new PluginException("VersionChangedEvent can't be null");
if (versionUpdatedEvent == null) {
throw new PluginException("VersionUpdatedEvent can't be null");
}
VersionChangedEvent.EventType eventType = versionChangedEvent.getEventType();
switch (eventType) {
case VERSION_UPDATE:
String version = versionChangedEvent.getVersion();
pluginAdapter.setDynamicVersion(version);
String dynamicVersion = versionUpdatedEvent.getDynamicVersion();
String localVersion = versionUpdatedEvent.getLocalVersion();
LOG.info("********** Version has been updated, new version is {} **********", version);
if (StringUtils.isEmpty(localVersion)) {
pluginAdapter.setDynamicVersion(dynamicVersion);
break;
case VERSION_CLEAR:
pluginAdapter.clearDynamicVersion();
refreshLoadBalancer();
LOG.info("********** Version has been cleared **********");
LOG.info("********** Version has been updated, new version is {} **********", dynamicVersion);
} else {
if (StringUtils.equals(pluginAdapter.getLocalVersion(), localVersion)) {
pluginAdapter.setDynamicVersion(dynamicVersion);
refreshLoadBalancer();
break;
LOG.info("********** Version has been updated, new version is {} **********", dynamicVersion);
}
}
}
refreshLoadBalancer();
@Subscribe
public void onVersionCleared(VersionClearedEvent versionClearedEvent) {
Boolean discoveryControlEnabled = pluginContextAware.isDiscoveryControlEnabled();
if (!discoveryControlEnabled) {
LOG.info("********** Discovery control is disabled, ignore to subscribe **********");
return;
}
LOG.info("********** Version clearing has been triggered **********");
if (versionClearedEvent == null) {
throw new PluginException("VersionClearedEvent can't be null");
}
String localVersion = versionClearedEvent.getLocalVersion();
if (StringUtils.isEmpty(localVersion)) {
pluginAdapter.clearDynamicVersion();
refreshLoadBalancer();
LOG.info("********** Version has been cleared **********");
} else {
if (StringUtils.equals(pluginAdapter.getLocalVersion(), localVersion)) {
pluginAdapter.clearDynamicVersion();
refreshLoadBalancer();
LOG.info("********** Version has been cleared **********");
}
}
}
private void refreshLoadBalancer() {
......
......@@ -13,39 +13,22 @@ import java.io.Serializable;
import org.apache.commons.lang3.StringUtils;
import com.nepxion.discovery.plugin.framework.exception.PluginException;
public class VersionChangedEvent implements Serializable {
public class VersionClearedEvent implements Serializable {
private static final long serialVersionUID = 5079797986381461496L;
public enum EventType {
VERSION_UPDATE,
VERSION_CLEAR;
}
private EventType eventType;
private String version;
private String localVersion;
public VersionChangedEvent(EventType eventType) {
this(eventType, null);
public VersionClearedEvent() {
this(null);
}
public VersionChangedEvent(EventType eventType, String version) {
if (eventType == EventType.VERSION_UPDATE && StringUtils.isEmpty(version)) {
throw new PluginException("Version value can't be null or empty while updating");
public VersionClearedEvent(String localVersion) {
if (StringUtils.isNotEmpty(localVersion)) {
this.localVersion = localVersion.trim();
}
this.eventType = eventType;
if (StringUtils.isNotEmpty(version)) {
this.version = version.trim();
}
}
public EventType getEventType() {
return eventType;
}
public String getVersion() {
return version;
public String getLocalVersion() {
return localVersion;
}
}
\ No newline at end of file
package com.nepxion.discovery.plugin.framework.event;
/**
* <p>Title: Nepxion Discovery</p>
* <p>Description: Nepxion Discovery</p>
* <p>Copyright: Copyright (c) 2017-2050</p>
* <p>Company: Nepxion</p>
* @author Haojun Ren
* @version 1.0
*/
import java.io.Serializable;
import org.apache.commons.lang3.StringUtils;
import com.nepxion.discovery.plugin.framework.exception.PluginException;
public class VersionUpdatedEvent implements Serializable {
private static final long serialVersionUID = 7749946311426379329L;
private String dynamicVersion;
private String localVersion;
public VersionUpdatedEvent(String dynamicVersion) {
this(dynamicVersion, null);
}
public VersionUpdatedEvent(String dynamicVersion, String localVersion) {
if (StringUtils.isNotEmpty(dynamicVersion)) {
this.dynamicVersion = dynamicVersion.trim();
}
if (StringUtils.isEmpty(this.dynamicVersion)) {
throw new PluginException("Dynamic version can't be null or empty while updating");
}
if (StringUtils.isNotEmpty(localVersion)) {
this.localVersion = localVersion.trim();
}
}
public String getDynamicVersion() {
return dynamicVersion;
}
public String getLocalVersion() {
return localVersion;
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment