Skip to content

Commit

Permalink
服务启动与停止代码优化
Browse files Browse the repository at this point in the history
  • Loading branch information
夜色 committed Aug 23, 2017
1 parent 9d5ad83 commit 22ebf52
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 6 deletions.
45 changes: 41 additions & 4 deletions mpush-api/src/main/java/com/mpush/api/service/BaseService.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,17 @@ protected void tryStart(Listener l, FunctionEx function) {
try {
init();
function.apply(listener);
listener.monitor(this);
listener.monitor(this);//主要用于异步,否则应该放置在function.apply(listener)之前
} catch (Throwable e) {
listener.onFailure(e);
throw new ServiceException(e);
}
} else {
listener.onFailure(new ServiceException("service already started."));
if (throwIfStarted()) {
listener.onFailure(new ServiceException("service already started."));
} else {
listener.onSuccess();
}
}
}

Expand All @@ -61,13 +65,17 @@ protected void tryStop(Listener l, FunctionEx function) {
if (started.compareAndSet(true, false)) {
try {
function.apply(listener);
listener.monitor(this);
listener.monitor(this);//主要用于异步,否则应该放置在function.apply(listener)之前
} catch (Throwable e) {
listener.onFailure(e);
throw new ServiceException(e);
}
} else {
listener.onFailure(new ServiceException("service already stopped."));
if (throwIfStopped()) {
listener.onFailure(new ServiceException("service already stopped."));
} else {
listener.onSuccess();
}
}
}

Expand Down Expand Up @@ -111,6 +119,35 @@ protected void doStop(Listener listener) throws Throwable {
listener.onSuccess();
}

/**
* 控制当服务已经启动后,重复调用start方法,是否抛出服务已经启动异常
* 默认是true
*
* @return true:抛出异常
*/
protected boolean throwIfStarted() {
return true;
}

/**
* 控制当服务已经停止后,重复调用stop方法,是否抛出服务已经停止异常
* 默认是true
*
* @return true:抛出异常
*/
protected boolean throwIfStopped() {
return true;
}

/**
* 服务启动停止,超时时间, 默认是10s
*
* @return 超时时间
*/
protected int timeoutMillis() {
return 1000 * 10;
}

protected interface FunctionEx {
void apply(Listener l) throws Throwable;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,16 @@ public void onFailure(Throwable cause) {
: new ServiceException(cause);
}

/**
* 防止服务长时间卡在某个地方,增加超时监控
*
* @param service 服务
*/
public void monitor(BaseService service) {
if (isDone()) return;// 防止Listener被重复执行
runAsync(() -> {
try {
this.get(10, TimeUnit.SECONDS);
this.get(service.timeoutMillis(), TimeUnit.MILLISECONDS);
} catch (Exception e) {
this.onFailure(new ServiceException(String.format("service %s monitor timeout", service.getClass().getSimpleName())));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.mpush.api.Constants;
import com.mpush.api.spi.common.CacheManager;
import com.mpush.tools.Jsons;
import com.mpush.tools.log.Logs;

import java.nio.file.Files;
import java.nio.file.Path;
Expand All @@ -47,6 +48,7 @@ public final class FileCacheManger implements CacheManager {

@Override
public void init() {
Logs.Console.warn("你正在使用的CacheManager只能用于源码测试,生产环境请使用redis 3.x.");
try {
Path dir = Paths.get(this.getClass().getResource("/").toURI());
this.cacheFile = Paths.get(dir.toString(), "cache.dat");
Expand Down
22 changes: 21 additions & 1 deletion mpush-test/src/main/java/com/mpush/test/spi/FileSrd.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@

import com.google.common.collect.Lists;
import com.mpush.api.service.BaseService;
import com.mpush.api.service.Listener;
import com.mpush.api.srd.*;
import com.mpush.tools.Jsons;
import com.mpush.tools.log.Logs;

import java.util.List;

Expand All @@ -36,8 +38,26 @@ public final class FileSrd extends BaseService implements ServiceRegistry, Servi
public static final FileSrd I = new FileSrd();

@Override
public void init() {
public void start(Listener listener) {
if (isRunning()) {
listener.onSuccess();
} else {
super.start(listener);
}
}

@Override
public void stop(Listener listener) {
if (isRunning()) {
super.stop(listener);
} else {
listener.onSuccess();
}
}

@Override
public void init() {
Logs.Console.warn("你正在使用的ServiceRegistry和ServiceDiscovery只能用于源码测试,生产环境请使用zookeeper.");
}

@Override
Expand Down
9 changes: 9 additions & 0 deletions mpush-zk/src/main/java/com/mpush/zk/ZKClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,15 @@ public void start(Listener listener) {
}
}

@Override
public void stop(Listener listener) {
if (isRunning()) {
super.stop(listener);
} else {
listener.onSuccess();
}
}

@Override
protected void doStart(Listener listener) throws Throwable {
client.start();
Expand Down

0 comments on commit 22ebf52

Please sign in to comment.