Skip to content

Commit

Permalink
style: 优化代码
Browse files Browse the repository at this point in the history
  • Loading branch information
KouShenhai committed Jul 21, 2024
1 parent 5c43c92 commit f5bf9d0
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,24 +48,6 @@ public class SpringContextUtil implements ApplicationContextAware, DisposableBea

private final Environment environment;

public String getAppName() {
try {
return ObjectUtil.requireNotNull(environment.getProperty("spring.application.name"));
}
catch (Exception e) {
return "application";
}
}

@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
SpringContextUtil.applicationContext = applicationContext;
}

public ApplicationContext getApplicationContext() {
return applicationContext;
}

/**
* 获取工厂.
* @return 工厂
Expand Down Expand Up @@ -163,23 +145,40 @@ public static void removeBean(String beanName) {
}
}

/**
* 销毁上下文.
*/
@Override
public void destroy() {
applicationContext = null;
}

/**
* 推送事件.
* @param event 事件
*/
public static void publishEvent(ApplicationEvent event) {
if (ObjectUtil.isNotNull(applicationContext)) {
// log.info("发布Spring事件");
applicationContext.publishEvent(event);
}
}

public String getAppName() {
try {
return ObjectUtil.requireNotNull(environment.getProperty("spring.application.name"));
}
catch (Exception e) {
return "application";
}
}

public ApplicationContext getApplicationContext() {
return applicationContext;
}

@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
SpringContextUtil.applicationContext = applicationContext;
}

/**
* 销毁上下文.
*/
@Override
public void destroy() {
applicationContext = null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package org.laokou.common.core.utils;

import lombok.extern.slf4j.Slf4j;
import org.laokou.common.i18n.utils.ObjectUtil;

import java.util.concurrent.ExecutorService;
Expand All @@ -28,6 +29,7 @@
*
* @author laokou
*/
@Slf4j
public class ThreadUtil {

/**
Expand All @@ -47,6 +49,9 @@ public static void shutdown(ExecutorService executorService, int timeout) {
executorService.shutdownNow();
Thread.currentThread().interrupt();
}
finally {
log.info("关闭线程池");
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,34 +58,34 @@ public class RSAUtil {

static {
try (InputStream inputStream1 = ResourceUtil.getResource("/conf/publicKey.scr").getInputStream();
InputStream inputStream2 = ResourceUtil.getResource("/conf/privateKey.scr").getInputStream()) {
InputStream inputStream2 = ResourceUtil.getResource("/conf/privateKey.scr").getInputStream()) {
PUBLIC_KEY = new String(inputStream1.readAllBytes(), StandardCharsets.UTF_8).trim();
PRIVATE_KEY = new String(inputStream2.readAllBytes(), StandardCharsets.UTF_8).trim();
} catch (IOException e) {
}
catch (IOException e) {
throw new RuntimeException(e);
}
}

/**
* 根据私钥解密.
*
* @param body 数据
* @param key 私钥
* @param key 私钥
* @return 解密后的字符串
*/
public static String decryptByPrivateKey(String body, String key) {
try {
byte[] privateKey = StringUtil.isNotEmpty(key) ? decryptBase64(key) : decryptBase64(PRIVATE_KEY);
byte[] bytes = decryptByPrivateKey(decryptBase64(body), privateKey);
return new String(ObjectUtil.requireNotNull(bytes), StandardCharsets.UTF_8);
} catch (Exception e) {
}
catch (Exception e) {
return body;
}
}

/**
* 根据私钥解密.
*
* @param body 数据
* @return 解密后的字符串
*/
Expand All @@ -95,24 +95,23 @@ public static String decryptByPrivateKey(String body) {

/**
* 根据公钥加密.
*
* @param body 数据
* @param key 公钥
* @param key 公钥
* @return 加密后的字符串
*/
public static String encryptByPublicKey(String body, String key) {
try {
byte[] publicKey = StringUtil.isNotEmpty(key) ? decryptBase64(key) : decryptBase64(PUBLIC_KEY);
byte[] bytes = encryptByPublicKey(body.getBytes(StandardCharsets.UTF_8), publicKey);
return encryptBase64(bytes);
} catch (Exception e) {
}
catch (Exception e) {
return body;
}
}

/**
* 根据公钥加密.
*
* @param body 数据
* @return 加密后的字符串
*/
Expand All @@ -122,7 +121,6 @@ public static String encryptByPublicKey(String body) {

/**
* 获取公钥.
*
* @return 公钥
*/
public static String getPublicKey() {
Expand All @@ -131,7 +129,6 @@ public static String getPublicKey() {

/**
* base64解密.
*
* @param body 数据
* @return 解密后的字符串
*/
Expand All @@ -141,7 +138,6 @@ private static byte[] decryptBase64(String body) {

/**
* base64加密.
*
* @param bodyBytes 数据
* @return 加密后的字符串
*/
Expand All @@ -151,9 +147,8 @@ private static String encryptBase64(byte[] bodyBytes) {

/**
* 根据公钥加密.
*
* @param bodyBytes 加密字符
* @param keyBytes 公钥
* @param keyBytes 公钥
* @return 加密后的字符串
*/
@SneakyThrows
Expand All @@ -168,9 +163,8 @@ private static byte[] encryptByPublicKey(byte[] bodyBytes, byte[] keyBytes) {

/**
* 根据私钥解密.
*
* @param bodyBytes 加密字符
* @param keyBytes 私钥
* @param keyBytes 私钥
* @return 解密后的字符串
*/
@SneakyThrows
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executor;

import static org.apache.rocketmq.spring.annotation.ConsumeMode.CONCURRENTLY;
import static org.apache.rocketmq.spring.annotation.MessageModel.CLUSTERING;
Expand All @@ -46,6 +49,8 @@
messageModel = CLUSTERING, consumeMode = CONCURRENTLY)
public class ModifyDomainEventHandler implements RocketMQListener<MessageExt> {

private final Executor executor;

private final DomainEventService domainEventService;

@Override
Expand All @@ -54,11 +59,13 @@ public void onMessage(MessageExt messageExt) {
ThreadContext.put(TRACE_ID, traceId);
String msg = new String(messageExt.getBody(), StandardCharsets.UTF_8);
try {
DefaultDomainEvent defaultDomainEvent = JacksonUtil.toBean(msg, DefaultDomainEvent.class);
domainEventService.update(new DomainEventA(EMPTY, defaultDomainEvent));
CompletableFuture.runAsync(() -> {
DefaultDomainEvent defaultDomainEvent = JacksonUtil.toBean(msg, DefaultDomainEvent.class);
domainEventService.update(new DomainEventA(EMPTY, defaultDomainEvent));
}, executor).thenRunAsync(ThreadContext::clearMap, executor).get();
}
finally {
ThreadContext.clearMap();
catch (InterruptedException | ExecutionException e) {
throw new RuntimeException(e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,8 @@
import org.laokou.common.i18n.utils.LogUtil;
import org.laokou.common.mybatisplus.utils.TransactionalUtil;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import static org.laokou.common.core.config.TaskExecutorAutoConfig.THREAD_POOL_TASK_EXECUTOR_NAME;

/**
* @author laokou
*/
Expand Down Expand Up @@ -73,7 +70,6 @@ public void create(DomainEventA domainEventA) {
}

@Override
@Async(THREAD_POOL_TASK_EXECUTOR_NAME)
public void update(DomainEventA domainEventA) {
try {
DynamicDataSourceContextHolder.push(DOMAIN);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
public class ShutdownFilter implements Filter, org.springframework.web.server.WebFilter {

private static final ScheduledExecutorService NEWED_SCHEDULED_THREAD_POOL = Executors.newScheduledThreadPool(1);

private final SpringContextUtil springContextUtil;

@Override
Expand Down Expand Up @@ -97,9 +98,9 @@ private boolean open() {
// 一分钟内没完成 或 计数器为0 -> 结束
if (IdGenerator.SystemClock.now() - start >= second || ShutdownHolder.get() == 0) {
ThreadUtil.shutdown(NEWED_SCHEDULED_THREAD_POOL, 10);
// 关闭应用
log.info("关闭应用");
int exitCode = SpringApplication.exit(springContextUtil.getApplicationContext(),
new ExitCodeGeneratorImpl());
new ExitCodeGeneratorImpl());
System.exit(exitCode);
}
}, 0, 1, TimeUnit.SECONDS);
Expand Down

0 comments on commit f5bf9d0

Please sign in to comment.