cms添加回调操作

This commit is contained in:
Frank 2023-09-21 13:45:41 +08:00
parent 220e723d4c
commit c81be25465
3 changed files with 23 additions and 19 deletions

View File

@ -10,7 +10,7 @@
</parent> </parent>
<groupId>net.javase.hk</groupId> <groupId>net.javase.hk</groupId>
<artifactId>hk-sup-spring-boot-starter</artifactId> <artifactId>hk-sup-spring-boot-starter</artifactId>
<version>0.0.3</version> <version>0.0.4</version>
<name>hk-sup-spring-boot-starter</name> <name>hk-sup-spring-boot-starter</name>
<description>hk-sup-spring-boot-starter</description> <description>hk-sup-spring-boot-starter</description>
<properties> <properties>

View File

@ -36,13 +36,13 @@ public class CmsServer {
private HCISUPCMS.DEVICE_REGISTER_CB deviceRegisterCb; private HCISUPCMS.DEVICE_REGISTER_CB deviceRegisterCb;
HCISUPCMS.NET_EHOME_CMS_LISTEN_PARAM struCMSListenPara = new HCISUPCMS.NET_EHOME_CMS_LISTEN_PARAM(); HCISUPCMS.NET_EHOME_CMS_LISTEN_PARAM struCMSListenPara = new HCISUPCMS.NET_EHOME_CMS_LISTEN_PARAM();
private Runnable preStartCallback = null; private CmsServerCallback callback = null;
private Runnable stopCallback = null;
public CmsServer(HkSupSdkProperties supSdkProperties, ApplicationContext applicationContext) { public CmsServer(HkSupSdkProperties supSdkProperties, ApplicationContext applicationContext) {
this.supSdkProperties = supSdkProperties; this.supSdkProperties = supSdkProperties;
this.applicationContext = applicationContext; this.applicationContext = applicationContext;
createSDKInstance(); createSDKInstance();
callback = applicationContext.getBeansOfType(CmsServerCallback.class).values().stream().findAny().orElse(null);
} }
/** /**
@ -160,28 +160,16 @@ public class CmsServer {
*/ */
void startCmsServer() { void startCmsServer() {
try { try {
Optional.ofNullable(preStartCallback).ifPresent(Runnable::run); Optional<CmsServerCallback> opt = Optional.ofNullable(callback);
opt.ifPresent(CmsServerCallback::preStart);
cmsInit(); cmsInit();
startCmsListen(); startCmsListen();
opt.ifPresent(CmsServerCallback::finishStart);
} catch (Exception ex) { } catch (Exception ex) {
log.error("启动CMS注册服务失败: " + ex.getMessage(), ex); log.error("启动CMS注册服务失败: " + ex.getMessage(), ex);
} }
} }
/**
* 初始化服务 前置回调
*/
public void preStartCallback(Runnable callback) {
this.preStartCallback = callback;
}
/**
* 设置停止服务后回调
*/
public void stopCallback(Runnable callback) {
this.stopCallback = callback;
}
/** /**
* 停止CMS服务,回收资源 * 停止CMS服务,回收资源
*/ */
@ -192,7 +180,7 @@ public class CmsServer {
hcisupcms.NET_ECMS_StopListen(CmsHandle); hcisupcms.NET_ECMS_StopListen(CmsHandle);
hcisupcms.NET_ECMS_Fini(); hcisupcms.NET_ECMS_Fini();
log.info("停止CMS注册服务成功!"); log.info("停止CMS注册服务成功!");
Optional.ofNullable(stopCallback).ifPresent(Runnable::run); Optional.ofNullable(callback).ifPresent(CmsServerCallback::finishStop);
} }
} catch (Exception ex) { } catch (Exception ex) {
log.error("停止CMS注册服务失败: " + ex.getMessage(), ex); log.error("停止CMS注册服务失败: " + ex.getMessage(), ex);

View File

@ -0,0 +1,16 @@
package net.javase.hksup.starter.service;
/**
* CmsServerCallback
*
* @author Frank
*/
public interface CmsServerCallback {
void preStart();
void finishStart();
void finishStop();
}