com.zusmart.basic.cycle.support.AbstractStable Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of zusmart-basic Show documentation
Show all versions of zusmart-basic Show documentation
基础模块,提供配置,日志,SPI,图排序,路径匹配,资源扫描,包扫描,常用工具类
package com.zusmart.basic.cycle.support;
import com.zusmart.basic.cycle.Stable;
import com.zusmart.basic.cycle.StableEvent;
import com.zusmart.basic.cycle.StableListener;
import com.zusmart.basic.cycle.StableType;
public abstract class AbstractStable> extends AbstractEventable implements Stable {
private static final String EVENT_START_SUCCESS = "EVENT_START_SUCCESS";
private static final String EVENT_START_FAILURE = "EVENT_START_FAILURE";
private static final String EVENT_CLOSE_SUCCESS = "EVENT_CLOSE_SUCCESS";
private static final String EVENT_CLOSE_FAILURE = "EVENT_CLOSE_FAILURE";
private static final String EVENT_STARTING = "EVENT_STARTING";
private static final String EVENT_CLOSING = "EVENT_CLOSING";
private volatile StableType type = StableType.Closed;
@Override
public final synchronized void start() throws Exception {
if (this.isStarting() || this.isRunning()) {
return;
}
E event = this.createEvent();
try {
this.type = StableType.Staring;
this.fireEvent(EVENT_STARTING, event);
this.doStart();
this.type = StableType.Running;
this.fireEvent(EVENT_START_SUCCESS, event);
} catch (Exception e) {
this.type = StableType.Failed;
this.fireEvent(EVENT_START_FAILURE, event);
throw e;
}
}
@Override
public final synchronized void close() throws Exception {
if (this.isClosing() || this.isClosed()) {
return;
}
E event = this.createEvent();
try {
this.type = StableType.Closing;
this.fireEvent(EVENT_CLOSING, event);
this.doClose();
this.type = StableType.Closed;
this.fireEvent(EVENT_CLOSE_SUCCESS, event);
} catch (Exception e) {
this.type = StableType.Failed;
this.fireEvent(EVENT_CLOSE_FAILURE, event);
throw e;
}
}
@Override
public StableType getStableType() {
return this.type;
}
@Override
public boolean isRunning() {
return this.type == StableType.Running;
}
@Override
public boolean isStarting() {
return this.type == StableType.Staring;
}
@Override
public boolean isClosing() {
return this.type == StableType.Closing;
}
@Override
public boolean isClosed() {
return this.type == StableType.Closed;
}
@Override
public boolean isFailed() {
return this.type == StableType.Failed;
}
@Override
public void attachStartingListener(L listener) {
this.attachEventListener(EVENT_STARTING, listener);
}
@Override
public void detachStartingListener(L listener) {
this.detachEventListener(EVENT_STARTING, listener);
}
@Override
public void attachStartSuccessListener(L listener) {
this.attachEventListener(EVENT_START_SUCCESS, listener);
}
@Override
public void detachStartSuccessListener(L listener) {
this.detachEventListener(EVENT_START_SUCCESS, listener);
}
@Override
public void attachStartFailureListener(L listener) {
this.attachEventListener(EVENT_START_FAILURE, listener);
}
@Override
public void detachStartFaulureListener(L listener) {
this.detachEventListener(EVENT_START_FAILURE, listener);
}
@Override
public void attachClosingListener(L listener) {
this.attachEventListener(EVENT_CLOSING, listener);
}
@Override
public void detachClosingListener(L listener) {
this.detachEventListener(EVENT_CLOSING, listener);
}
@Override
public void attachCloseSuccessListener(L listener) {
this.attachEventListener(EVENT_CLOSE_SUCCESS, listener);
}
@Override
public void detachCloseSuccessListener(L listener) {
this.detachEventListener(EVENT_CLOSE_SUCCESS, listener);
}
@Override
public void attachCloseFailureListener(L listener) {
this.attachEventListener(EVENT_CLOSE_FAILURE, listener);
}
@Override
public void detachCloseFailureListener(L listener) {
this.detachEventListener(EVENT_CLOSE_FAILURE, listener);
}
protected abstract E createEvent();
protected abstract void doStart() throws Exception;
protected abstract void doClose() throws Exception;
}