mirror of
https://gitee.com/san-bing/JChargePointProtocol
synced 2026-05-08 20:10:01 +08:00
云快充1.5.0 初始化
This commit is contained in:
@@ -0,0 +1,30 @@
|
||||
/**
|
||||
* 抖音关注:程序员三丙
|
||||
* 知识星球:https://t.zsxq.com/j9b21
|
||||
*/
|
||||
package sanbing.jcpp.protocol.provider;
|
||||
|
||||
import sanbing.jcpp.protocol.domain.ProtocolSession;
|
||||
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
/**
|
||||
* @author baigod
|
||||
*/
|
||||
public interface ProtocolSessionRegistryProvider {
|
||||
|
||||
/**
|
||||
* 注册会话
|
||||
*/
|
||||
void register(ProtocolSession protocolSession);
|
||||
|
||||
void unregister(UUID sessionId);
|
||||
|
||||
CompletableFuture<ProtocolSession> get(UUID sessionId);
|
||||
|
||||
/**
|
||||
* 活跃会话
|
||||
*/
|
||||
void activate(ProtocolSession protocolSession);
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
/**
|
||||
* 抖音关注:程序员三丙
|
||||
* 知识星球:https://t.zsxq.com/j9b21
|
||||
*/
|
||||
package sanbing.jcpp.protocol.provider;
|
||||
|
||||
import sanbing.jcpp.protocol.cfg.ProtocolCfg;
|
||||
|
||||
/**
|
||||
* @author baigod
|
||||
*/
|
||||
public interface ProtocolsConfigProvider {
|
||||
|
||||
ProtocolCfg loadConfig(String protocol);
|
||||
}
|
||||
@@ -0,0 +1,115 @@
|
||||
/**
|
||||
* 抖音关注:程序员三丙
|
||||
* 知识星球:https://t.zsxq.com/j9b21
|
||||
*/
|
||||
package sanbing.jcpp.protocol.provider.impl;
|
||||
|
||||
import com.github.benmanes.caffeine.cache.AsyncCache;
|
||||
import com.github.benmanes.caffeine.cache.Caffeine;
|
||||
import jakarta.annotation.PostConstruct;
|
||||
import jakarta.annotation.PreDestroy;
|
||||
import lombok.Getter;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Service;
|
||||
import sanbing.jcpp.infrastructure.util.async.JCPPThreadFactory;
|
||||
import sanbing.jcpp.infrastructure.util.config.ThreadPoolConfiguration;
|
||||
import sanbing.jcpp.protocol.domain.ProtocolSession;
|
||||
import sanbing.jcpp.protocol.domain.SessionCloseReason;
|
||||
import sanbing.jcpp.protocol.provider.ProtocolSessionRegistryProvider;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
|
||||
/**
|
||||
* @author baigod
|
||||
*/
|
||||
@Service
|
||||
@Slf4j
|
||||
public class DefaultProtocolSessionRegistryProvider implements ProtocolSessionRegistryProvider {
|
||||
private static final int INIT_CACHE_LIMIT = 100_000;
|
||||
|
||||
@Value("${service.protocols.sessions.default-inactivity-timeout-in-sec}")
|
||||
private int defaultInactivityTimeoutInSec;
|
||||
|
||||
@Value("${service.protocols.sessions.default-state-check-interval-in-sec}")
|
||||
private int defaultStateCheckIntervalInSec;
|
||||
|
||||
@Getter
|
||||
private final AsyncCache<UUID, ProtocolSession> SESSION_CACHE = buildCache();
|
||||
|
||||
private final ScheduledExecutorService scheduledExecutorService = Executors.newSingleThreadScheduledExecutor(JCPPThreadFactory.forName("session-state-checker"));
|
||||
|
||||
@PostConstruct
|
||||
public void init() {
|
||||
scheduledExecutorService.scheduleAtFixedRate(() ->
|
||||
SESSION_CACHE.asMap().forEach((id, sessionCompletableFuture) ->
|
||||
sessionCompletableFuture.whenComplete((protocolSession, throwable) -> {
|
||||
if (throwable == null && protocolSession != null) {
|
||||
if (protocolSession.getLastActivityTime().isBefore(LocalDateTime.now().minusSeconds(defaultInactivityTimeoutInSec))) {
|
||||
protocolSession.close(SessionCloseReason.INACTIVE);
|
||||
unregister(protocolSession.getId());
|
||||
}
|
||||
}
|
||||
})
|
||||
), defaultStateCheckIntervalInSec, defaultStateCheckIntervalInSec, TimeUnit.SECONDS);
|
||||
}
|
||||
|
||||
@PreDestroy
|
||||
public void destroy() {
|
||||
scheduledExecutorService.shutdownNow();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void register(ProtocolSession protocolSession) {
|
||||
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Registering session {}", protocolSession);
|
||||
}
|
||||
|
||||
SESSION_CACHE.put(protocolSession.getId(), CompletableFuture.supplyAsync(() -> protocolSession, ThreadPoolConfiguration.JCPP_COMMON_THREAD_POOL));
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void unregister(UUID sessionId) {
|
||||
|
||||
log.info("Unregistering session {}", sessionId);
|
||||
|
||||
SESSION_CACHE.synchronous().invalidate(sessionId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompletableFuture<ProtocolSession> get(UUID sessionId) {
|
||||
|
||||
log.debug("Get session {}", sessionId);
|
||||
|
||||
return SESSION_CACHE.get(sessionId, uuid -> null);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void activate(ProtocolSession protocolSession) {
|
||||
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Activating session {}", protocolSession);
|
||||
}
|
||||
|
||||
protocolSession.setLastActivityTime(LocalDateTime.now());
|
||||
|
||||
SESSION_CACHE.put(protocolSession.getId(), CompletableFuture.supplyAsync(() -> protocolSession, ThreadPoolConfiguration.JCPP_COMMON_THREAD_POOL));
|
||||
}
|
||||
|
||||
private AsyncCache<UUID, ProtocolSession> buildCache() {
|
||||
return Caffeine.newBuilder()
|
||||
.initialCapacity(INIT_CACHE_LIMIT)
|
||||
.maximumSize(INIT_CACHE_LIMIT * 10)
|
||||
.executor(ThreadPoolConfiguration.JCPP_COMMON_THREAD_POOL)
|
||||
.buildAsync();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
/**
|
||||
* 抖音关注:程序员三丙
|
||||
* 知识星球:https://t.zsxq.com/j9b21
|
||||
*/
|
||||
package sanbing.jcpp.protocol.provider.impl;
|
||||
|
||||
import lombok.Setter;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.stereotype.Service;
|
||||
import sanbing.jcpp.infrastructure.util.config.ConstraintValidator;
|
||||
import sanbing.jcpp.infrastructure.util.jackson.JacksonUtil;
|
||||
import sanbing.jcpp.protocol.cfg.ProtocolCfg;
|
||||
import sanbing.jcpp.protocol.provider.ProtocolsConfigProvider;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
@Setter
|
||||
@Service
|
||||
@Slf4j
|
||||
@ConfigurationProperties("service")
|
||||
public class DefaultProtocolsConfigProvider implements ProtocolsConfigProvider {
|
||||
|
||||
private Map<String, ProtocolCfg> protocols;
|
||||
|
||||
@Override
|
||||
public ProtocolCfg loadConfig(String protocol) {
|
||||
|
||||
ProtocolCfg protocolCfg = protocols.get(protocol);
|
||||
|
||||
log.info("load {}'s configuration: \n{}", protocol, JacksonUtil.toPrettyString(protocolCfg));
|
||||
|
||||
ConstraintValidator.validateFields(protocolCfg, "'" + protocol + "' configuration is invalid:");
|
||||
|
||||
return protocolCfg;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user