Commit fc0312eb by Nepxion

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

parent 31847c48
...@@ -33,7 +33,8 @@ import org.springframework.web.bind.annotation.RestController; ...@@ -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.adapter.PluginAdapter;
import com.nepxion.discovery.plugin.framework.context.PluginContextAware; import com.nepxion.discovery.plugin.framework.context.PluginContextAware;
import com.nepxion.discovery.plugin.framework.event.PluginEventWapper; 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 @RestController
@Api(tags = { "版本接口" }) @Api(tags = { "版本接口" })
...@@ -58,7 +59,7 @@ public class VersionEndpoint implements MvcEndpoint { ...@@ -58,7 +59,7 @@ public class VersionEndpoint implements MvcEndpoint {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Discovery control is disabled"); 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"); return ResponseEntity.ok().body("OK");
} }
...@@ -73,7 +74,7 @@ public class VersionEndpoint implements MvcEndpoint { ...@@ -73,7 +74,7 @@ public class VersionEndpoint implements MvcEndpoint {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Discovery control is disabled"); 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"); return ResponseEntity.ok().body("OK");
} }
......
...@@ -26,11 +26,19 @@ public class PluginEventWapper { ...@@ -26,11 +26,19 @@ public class PluginEventWapper {
} }
} }
public void fireVersionChanged(VersionChangedEvent versionChangedEvent, boolean async) { public void fireVersionUpdated(VersionUpdatedEvent versionUpdatedEvent, boolean async) {
if (async) { if (async) {
pluginPublisher.asyncPublish(versionChangedEvent); pluginPublisher.asyncPublish(versionUpdatedEvent);
} else { } 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; ...@@ -11,6 +11,7 @@ package com.nepxion.discovery.plugin.framework.event;
import java.io.InputStream; import java.io.InputStream;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
...@@ -57,7 +58,7 @@ public class PluginSubscriber { ...@@ -57,7 +58,7 @@ public class PluginSubscriber {
return; return;
} }
LOG.info("********** Remote config change has been subscribed **********"); LOG.info("********** Remote config changing has been triggered **********");
if (ruleChangedEvent == null) { if (ruleChangedEvent == null) {
throw new PluginException("RuleChangedEvent can't be null"); throw new PluginException("RuleChangedEvent can't be null");
...@@ -76,7 +77,7 @@ public class PluginSubscriber { ...@@ -76,7 +77,7 @@ public class PluginSubscriber {
} }
@Subscribe @Subscribe
public void onVersionChanged(VersionChangedEvent versionChangedEvent) { public void onVersionUpdated(VersionUpdatedEvent versionUpdatedEvent) {
Boolean discoveryControlEnabled = pluginContextAware.isDiscoveryControlEnabled(); Boolean discoveryControlEnabled = pluginContextAware.isDiscoveryControlEnabled();
if (!discoveryControlEnabled) { if (!discoveryControlEnabled) {
LOG.info("********** Discovery control is disabled, ignore to subscribe **********"); LOG.info("********** Discovery control is disabled, ignore to subscribe **********");
...@@ -84,30 +85,64 @@ public class PluginSubscriber { ...@@ -84,30 +85,64 @@ public class PluginSubscriber {
return; return;
} }
LOG.info("********** Version change has been subscribed **********"); LOG.info("********** Version updating has been triggered **********");
if (versionChangedEvent == null) { if (versionUpdatedEvent == null) {
throw new PluginException("VersionChangedEvent can't be null"); throw new PluginException("VersionUpdatedEvent can't be null");
} }
VersionChangedEvent.EventType eventType = versionChangedEvent.getEventType(); String dynamicVersion = versionUpdatedEvent.getDynamicVersion();
switch (eventType) { String localVersion = versionUpdatedEvent.getLocalVersion();
case VERSION_UPDATE:
String version = versionChangedEvent.getVersion();
pluginAdapter.setDynamicVersion(version);
LOG.info("********** Version has been updated, new version is {} **********", version); if (StringUtils.isEmpty(localVersion)) {
pluginAdapter.setDynamicVersion(dynamicVersion);
break; refreshLoadBalancer();
case VERSION_CLEAR:
pluginAdapter.clearDynamicVersion();
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();
LOG.info("********** Version has been updated, new version is {} **********", dynamicVersion);
}
}
}
@Subscribe
public void onVersionCleared(VersionClearedEvent versionClearedEvent) {
Boolean discoveryControlEnabled = pluginContextAware.isDiscoveryControlEnabled();
if (!discoveryControlEnabled) {
LOG.info("********** Discovery control is disabled, ignore to subscribe **********");
return;
}
break; 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(); 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() { private void refreshLoadBalancer() {
......
...@@ -13,39 +13,22 @@ import java.io.Serializable; ...@@ -13,39 +13,22 @@ import java.io.Serializable;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import com.nepxion.discovery.plugin.framework.exception.PluginException; public class VersionClearedEvent implements Serializable {
public class VersionChangedEvent implements Serializable {
private static final long serialVersionUID = 5079797986381461496L; private static final long serialVersionUID = 5079797986381461496L;
public enum EventType { private String localVersion;
VERSION_UPDATE,
VERSION_CLEAR;
}
private EventType eventType;
private String version;
public VersionChangedEvent(EventType eventType) { public VersionClearedEvent() {
this(eventType, null); this(null);
} }
public VersionChangedEvent(EventType eventType, String version) { public VersionClearedEvent(String localVersion) {
if (eventType == EventType.VERSION_UPDATE && StringUtils.isEmpty(version)) { if (StringUtils.isNotEmpty(localVersion)) {
throw new PluginException("Version value can't be null or empty while updating"); this.localVersion = localVersion.trim();
} }
this.eventType = eventType;
if (StringUtils.isNotEmpty(version)) {
this.version = version.trim();
}
}
public EventType getEventType() {
return eventType;
} }
public String getVersion() { public String getLocalVersion() {
return version; 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