Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/spring bean function support #25

Merged
merged 4 commits into from
Nov 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 55 additions & 16 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,18 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.28.0-GA</version>
<!-- <type>bundle</type>-->
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>transmittable-thread-local</artifactId>
<version>2.12.6</version>
<scope>test</scope>
</dependency>
</dependencies>

<licenses>
Expand Down Expand Up @@ -122,6 +134,47 @@
</snapshotRepository>
</distributionManagement>


<profiles>
<!-- idea右侧maven页签勾选自己的定制化属性,每个开发者根据本地情况可以配一套 -->
<profile>
<id>dc.mao</id>
<properties>
<doc.path>${java.home}/bin/javadoc</doc.path>
</properties>
</profile>

<!-- 原pom参数,默认执行 -->
<profile>
<id>cloud</id>
<activation>
<!-- 默认激活此profile -->
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<doc.path>${java.home}/../bin/javadoc</doc.path>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-gpg-plugin</artifactId>
<version>1.5</version>
<executions>
<execution>
<id>sign-artifacts</id>
<phase>verify</phase>
<goals>
<goal>sign</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>

<build>
<plugins>
<plugin>
Expand Down Expand Up @@ -172,7 +225,8 @@
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.9.1</version>
<configuration>
<javadocExecutable>${java.home}/../bin/javadoc</javadocExecutable>
<javadocExecutable>${doc.path}</javadocExecutable>
<source>8</source>
</configuration>
<executions>
<execution>
Expand All @@ -183,21 +237,6 @@
</execution>
</executions>
</plugin>
<!-- 若需要本地执行 mvn install 需要将下方gpg插件注释掉 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-gpg-plugin</artifactId>
<version>1.5</version>
<executions>
<execution>
<id>sign-artifacts</id>
<phase>verify</phase>
<goals>
<goal>sign</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ public class SystemLogAspect {
@Autowired(required = false)
private LogRecordErrorHandlerService logRecordErrorHandlerService;

@Autowired
private SystemLogThreadWrapper systemLogThreadWrapper;

private final SpelExpressionParser parser = new SpelExpressionParser();

private final DefaultParameterNameDiscoverer discoverer = new DefaultParameterNameDiscoverer();
Expand Down Expand Up @@ -160,7 +163,7 @@ public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
Long finalExecutionTime = executionTime;
Consumer<LogDTO> createLogFunction = logDTO -> createLog(logDTO, finalExecutionTime);
if (logRecordThreadPool != null) {
logDTOList.forEach(logDTO -> logRecordThreadPool.getLogRecordPoolExecutor().execute(() -> createLogFunction.accept(logDTO)));
logDTOList.forEach(logDTO -> logRecordThreadPool.getLogRecordPoolExecutor().execute(systemLogThreadWrapper.createLog(createLogFunction, logDTO)));
} else {
logDTOList.forEach(createLogFunction);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package cn.monitor4all.logRecord.aop;

import cn.monitor4all.logRecord.bean.LogDTO;

import java.util.function.Consumer;

/**
*
* 业务中有可能需要包装任务,将trace id传递给多线程执行的任务
* ex:
* <p>@Configuration<br>
* <p>public class LogRecordConfig<br>
* <p>&emsp;&emsp;@Bean<br>
* <p>&emsp;&emsp;public SystemLogThreadWrapper systemLogThreadWrapper() {<br>
* <p>&emsp;&emsp;&emsp;&emsp;return new SystemLogThreadWrapper() {<br>
* <p>&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;@Override<br>
* <p>&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;public Runnable createLog(Consumer&#60;LogDTO&#62; consumer, LogDTO logDTO) {<br>
* <p>&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;return RunnableWrapper.of(new SystemLogThreadWrapper(){}.createLog(consumer, logDTO));<br>
* <p>&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;}<br>
* <p>&emsp;&emsp;&emsp;&emsp;};<br>
* <p>&emsp;&emsp;}<br>
* <p>}<br>
* @author : dechao.mao
* @since : Created in 10:30 2022/11/10
*/
public interface SystemLogThreadWrapper {

/**
* 暴露多线程执行任务,方便业务层对多线程任务包装
* 也可以在任务前后执行一些动作
* @param consumer 日志处理业务
* @param logDTO 日志输出对象
* @return 输出日志任务
*/
default Runnable createLog(Consumer<LogDTO> consumer, LogDTO logDTO) {
return () -> {
consumer.accept(logDTO);
};
}

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package cn.monitor4all.logRecord.configuration;

import cn.monitor4all.logRecord.aop.SystemLogThreadWrapper;
import cn.monitor4all.logRecord.function.CustomFunctionRegistrar;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

Expand All @@ -13,4 +15,11 @@ public class CustomFunctionConfiguration {
public CustomFunctionRegistrar registrar() {
return new CustomFunctionRegistrar();
}


@Bean
@ConditionalOnMissingBean
public SystemLogThreadWrapper createLogConsumer() {
return new SystemLogThreadWrapper(){};
}
}
Loading