Skip to content

Commit 21d3fb2

Browse files
committed
reactify-core
1 parent 079fd56 commit 21d3fb2

File tree

14 files changed

+205
-288
lines changed

14 files changed

+205
-288
lines changed

reactify-core/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@
2727
<!-- =========================================== -->
2828
<!-- The maven central for all the library -->
2929
<!-- =========================================== -->
30-
<groupId>com.ezbuy.platform</groupId>
30+
<groupId>io.github.hoangtien2k3</groupId>
3131
<artifactId>reactify-core</artifactId>
32-
<version>1.2.5</version>
32+
<version>1.2.7</version>
3333
<name>reactify-core</name>
3434
<description>Java library for developing reactive programming(reactor-core) backend systems in microservices</description>
3535
<url>https://github.com/hoangtien2k3/reactify-core</url>

reactify-core/src/main/java/com/reactify/annotations/logging/LoggerAspectUtils.java

Lines changed: 62 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -123,11 +123,11 @@ public Mono<Object> logAround(ProceedingJoinPoint joinPoint) throws Throwable {
123123

124124
Span newSpan = tracer.nextSpan().name(name);
125125
var result = joinPoint.proceed();
126-
if (result instanceof Mono) {
126+
if (result instanceof Mono<?> monoResult) {
127127
return logMonoResult(
128128
joinPoint,
129129
start,
130-
(Mono<Object>) result,
130+
monoResult.cast(Object.class),
131131
newSpan,
132132
name,
133133
logType,
@@ -136,11 +136,11 @@ public Mono<Object> logAround(ProceedingJoinPoint joinPoint) throws Throwable {
136136
logInput,
137137
title);
138138
}
139-
if (result instanceof Flux) {
139+
if (result instanceof Flux<?> fluxResult) {
140140
return logFluxResult(
141141
joinPoint,
142142
start,
143-
(Flux<Object>) result,
143+
fluxResult.cast(Object.class),
144144
newSpan,
145145
name,
146146
logType,
@@ -149,7 +149,7 @@ public Mono<Object> logAround(ProceedingJoinPoint joinPoint) throws Throwable {
149149
logInput,
150150
title)
151151
.collectList()
152-
.map(list -> (Object) list);
152+
.map(list -> list);
153153
} else {
154154
return Mono.just(result);
155155
}
@@ -264,13 +264,30 @@ private Flux<Object> logFluxResult(
264264
boolean logInput,
265265
String title) {
266266
var contextRef = new AtomicReference<Context>();
267-
return result.doFinally(
268-
o -> logPerf(contextRef, newSpan, name, start, "1", null, logType, actionType, null, title))
269-
.contextWrite(context -> {
270-
contextRef.set(context);
271-
return context;
267+
if (logInput) {
268+
Object[] args = joinPoint.getArgs();
269+
logPerf(contextRef, newSpan, name, start, "INPUT", null, logType, actionType, args, title);
270+
}
271+
return result.doOnNext(output -> {
272+
if (logOutput) {
273+
logPerf(
274+
contextRef,
275+
newSpan,
276+
name,
277+
start,
278+
"OUTPUT",
279+
null,
280+
logType,
281+
actionType,
282+
new Object[] {output},
283+
title);
284+
}
272285
})
273-
.doOnError(o -> logPerf(contextRef, newSpan, name, start, "0", o, logType, actionType, null, title));
286+
.doOnError(error ->
287+
logPerf(contextRef, newSpan, name, start, "0", error, logType, actionType, null, title))
288+
.doFinally(signalType ->
289+
logPerf(contextRef, newSpan, name, start, "1", null, logType, actionType, null, title))
290+
.contextWrite(context -> contextRef.updateAndGet(ctx -> context));
274291
}
275292

276293
/**
@@ -290,15 +307,45 @@ private Flux<Object> logFluxResult(
290307
* @param result
291308
* a string indicating the result status ("0" for success, "1" for
292309
* failure)
293-
* @param o
310+
* @param data
294311
* the output object from the method execution, may be {@code null}
295312
*/
296313
private void logPerf(
297-
AtomicReference<Context> contextRef, Span newSpan, String name, Long start, String result, Object o) {
298-
newSpan.finish();
314+
AtomicReference<Context> contextRef,
315+
Span newSpan,
316+
String name,
317+
long start,
318+
String result,
319+
Object data,
320+
String logType,
321+
String actionType,
322+
String title) {
323+
if (newSpan != null) {
324+
newSpan.finish();
325+
}
299326
long duration = System.currentTimeMillis() - start;
300327
if (duration < 50) return;
301-
logPerf.info("{} {} {} M2 {}", name, duration, result, o == null ? "-" : o.toString());
328+
329+
Context context = contextRef != null ? contextRef.get() : Context.empty();
330+
String contextInfo = context != null ? context.toString() : "-";
331+
String dataInfo = "-";
332+
if (data != null) {
333+
if (data instanceof Throwable error) {
334+
dataInfo = String.format("Error: %s - %s", error.getClass().getSimpleName(), error.getMessage());
335+
} else {
336+
dataInfo = data.toString();
337+
}
338+
}
339+
logPerf.info(
340+
"{} | {} | {} | {} | {} | {} | {} | {}",
341+
title,
342+
name,
343+
duration,
344+
result,
345+
logType,
346+
actionType,
347+
contextInfo,
348+
dataInfo);
302349
}
303350

304351
/**

reactify-core/src/main/java/com/reactify/annotations/logging/LoggerQueue.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,7 @@
5555
@Slf4j
5656
public class LoggerQueue {
5757
private static LoggerQueue mMe = null;
58-
private ArrayBlockingQueue<LoggerDTO> myQueue = null;
59-
private static final Object myLock = new Object();
58+
private final ArrayBlockingQueue<LoggerDTO> myQueue;
6059

6160
@Getter
6261
private int countFalse = 0;
@@ -183,9 +182,7 @@ public void addQueue(
183182
*/
184183
public List<LoggerDTO> getRecords() {
185184
List<LoggerDTO> records = new ArrayList<>();
186-
if (myQueue != null) {
187-
myQueue.drainTo(records, 100000);
188-
}
185+
myQueue.drainTo(records, 100000);
189186
return records;
190187
}
191188

reactify-core/src/main/java/com/reactify/client/BaseSoapClient.java

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -66,37 +66,6 @@ public interface BaseSoapClient<T> {
6666
*/
6767
Mono<String> callRaw(WebClient webClient, Map<String, String> headerList, String payload);
6868

69-
/**
70-
* Parses the SOAP response data into an instance of the specified class type.
71-
*
72-
* @param realData
73-
* the raw response data as a string.
74-
* @param clz
75-
* the class to which the data should be converted.
76-
* @return an instance of type {@code T} containing the parsed response data.
77-
*/
78-
T parseData(String realData, Class<?> clz);
79-
80-
/**
81-
* Calls a specific API to retrieve the customer profile data using the provided
82-
* WebClient instance, HTTP headers, and payload. The response is returned as an
83-
* optional result wrapped in a reactive Mono.
84-
*
85-
* @param webClient
86-
* the WebClient instance used to make the SOAP call.
87-
* @param headerList
88-
* a map of HTTP headers to include in the request.
89-
* @param payload
90-
* the SOAP request payload as a string.
91-
* @param resultClass
92-
* the class of the expected response object type.
93-
* @return a Mono containing an Optional result of type {@code T}, representing
94-
* the customer profile data, or an empty Optional if no data was
95-
* returned.
96-
*/
97-
Mono<Optional<T>> callApiGetProfileKHDN(
98-
WebClient webClient, Map<String, String> headerList, String payload, Class<?> resultClass);
99-
10069
/**
10170
* Makes a SOAP call using version 2 of the API configuration, with the provided
10271
* WebClient instance, HTTP headers, and payload. Returns an optional result

reactify-core/src/main/java/com/reactify/client/impl/BaseRestClientImpl.java

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,12 @@ public Mono<Optional<T>> get(
9494
.map(response -> {
9595
log.info("Rest response {}", response);
9696
if (DataUtil.isNullOrEmpty(response)) {
97-
return this.getDefaultValue();
97+
return Optional.<T>empty();
9898
}
9999
if (DataUtil.safeEqual(resultClass.getSimpleName(), "String")) {
100-
return Optional.of((T) response);
100+
@SuppressWarnings("unchecked")
101+
T stringResponse = (T) response;
102+
return Optional.of(stringResponse);
101103
}
102104
T result = DataUtil.parseStringToObject(response, resultClass);
103105
return Optional.ofNullable(result);
@@ -210,7 +212,7 @@ public Mono<Optional<T>> postFormData(
210212
@Override
211213
public Optional<T> processReturn(String response, Class<?> resultClass) {
212214
if (DataUtil.isNullOrEmpty(response)) {
213-
return this.getDefaultValue();
215+
return Optional.empty();
214216
}
215217
T result = DataUtil.parseStringToObject(response, resultClass);
216218
return Optional.ofNullable(result);
@@ -462,15 +464,6 @@ public Mono<String> getRawWithFixedUri(WebClient webClient, String uri, MultiVal
462464
});
463465
}
464466

465-
/**
466-
* Provides a default empty Optional value.
467-
*
468-
* @return an Optional containing no value.
469-
*/
470-
private Optional<T> getDefaultValue() {
471-
return Optional.empty();
472-
}
473-
474467
/**
475468
* Ensures a safe MultiValueMap for headers, with a default "Content-Type:
476469
* application/json" if empty.

0 commit comments

Comments
 (0)