File tree 8 files changed +183
-0
lines changed
test/java/hello/advanced/trace/strategy/code
8 files changed +183
-0
lines changed Original file line number Diff line number Diff line change
1
+ package hello .advanced .app .v5 ;
2
+
3
+ import hello .advanced .trace .callback .TraceCallback ;
4
+ import hello .advanced .trace .callback .TraceTemplate ;
5
+ import hello .advanced .trace .logTrace .LogTrace ;
6
+ import hello .advanced .trace .template .AbstractTemplate ;
7
+ import org .springframework .web .bind .annotation .GetMapping ;
8
+ import org .springframework .web .bind .annotation .RestController ;
9
+
10
+ @ RestController
11
+ public class OrderControllerV5 {
12
+
13
+ private final OrderServiceV5 orderServiceV5 ;
14
+ private final TraceTemplate template ;
15
+
16
+ public OrderControllerV5 (OrderServiceV5 orderServiceV5 , LogTrace logTrace ) {
17
+ this .orderServiceV5 = orderServiceV5 ;
18
+ this .template = new TraceTemplate (logTrace );
19
+ }
20
+
21
+
22
+ @ GetMapping ("/v5/request" )
23
+ public String request (String itemId ) {
24
+ return template .execute ("OrderController.request()" , () -> {
25
+ orderServiceV5 .orderItem (itemId );
26
+ return "ok" ;
27
+ });
28
+ }
29
+
30
+ }
Original file line number Diff line number Diff line change
1
+ package hello .advanced .app .v5 ;
2
+
3
+ import hello .advanced .trace .callback .TraceTemplate ;
4
+ import hello .advanced .trace .logTrace .LogTrace ;
5
+ import hello .advanced .trace .template .AbstractTemplate ;
6
+ import lombok .RequiredArgsConstructor ;
7
+ import org .springframework .stereotype .Repository ;
8
+
9
+
10
+ @ Repository
11
+ public class OrderRepositoryV5 {
12
+ private final TraceTemplate template ;
13
+
14
+ public OrderRepositoryV5 (LogTrace trace ) {
15
+ this .template = new TraceTemplate (trace );
16
+ }
17
+
18
+ public void save (String itemId ) {
19
+ template .execute ("OrderRepositoryV4.orderItem()" , () -> {
20
+ if (itemId .equals ("ex" )) {
21
+ throw new IllegalStateException ("예외 발생" );
22
+ }
23
+ sleep (1000 );
24
+ return null ;
25
+ });
26
+ }
27
+
28
+ private void sleep (int millis ){
29
+ try {
30
+ Thread .sleep (millis );
31
+ } catch (InterruptedException e ) {
32
+ throw new RuntimeException (e );
33
+ }
34
+ }
35
+ }
Original file line number Diff line number Diff line change
1
+ package hello .advanced .app .v5 ;
2
+
3
+ import hello .advanced .trace .callback .TraceTemplate ;
4
+ import hello .advanced .trace .logTrace .LogTrace ;
5
+ import hello .advanced .trace .template .AbstractTemplate ;
6
+ import lombok .RequiredArgsConstructor ;
7
+ import org .springframework .stereotype .Service ;
8
+
9
+ @ Service
10
+ public class OrderServiceV5 {
11
+
12
+ private final OrderRepositoryV5 orderRepositoryV5 ;
13
+ private final TraceTemplate template ;
14
+
15
+ public OrderServiceV5 (OrderRepositoryV5 orderRepositoryV5 , LogTrace logTrace ) {
16
+ this .orderRepositoryV5 = orderRepositoryV5 ;
17
+ this .template = new TraceTemplate (logTrace );
18
+ }
19
+
20
+ public void orderItem (String itemId ) {
21
+ template .execute ("OrderService.orderItem()" , () -> {
22
+ orderRepositoryV5 .save (itemId );
23
+ return null ;
24
+ });
25
+ }
26
+
27
+ }
Original file line number Diff line number Diff line change
1
+ package hello .advanced .trace .callback ;
2
+
3
+ public interface TraceCallback <T > {
4
+
5
+ T call ();
6
+
7
+ }
Original file line number Diff line number Diff line change
1
+ package hello .advanced .trace .callback ;
2
+
3
+ import hello .advanced .trace .TraceStatus ;
4
+ import hello .advanced .trace .logTrace .LogTrace ;
5
+ import lombok .RequiredArgsConstructor ;
6
+ import lombok .extern .slf4j .Slf4j ;
7
+
8
+ @ RequiredArgsConstructor
9
+ @ Slf4j
10
+ public class TraceTemplate {
11
+
12
+ private final LogTrace trace ;
13
+
14
+ public <T > T execute (String message , TraceCallback <T > callback ) {
15
+ TraceStatus status = null ;
16
+ try {
17
+ status = trace .begin (message );
18
+ T result = callback .call ();
19
+ trace .end (status );
20
+ return result ;
21
+ } catch (Exception e ) {
22
+ trace .exception (status , e );
23
+ throw e ;
24
+ }
25
+ }
26
+
27
+ }
Original file line number Diff line number Diff line change
1
+ package hello .advanced .trace .strategy .code ;
2
+
3
+ import hello .advanced .trace .strategy .code .template .Callback ;
4
+ import hello .advanced .trace .strategy .code .template .TimeLogTemplate ;
5
+ import lombok .extern .slf4j .Slf4j ;
6
+ import org .junit .jupiter .api .Test ;
7
+
8
+ @ Slf4j
9
+ public class TemplateCallbackTest {
10
+
11
+ @ Test
12
+ void callBackV1 () {
13
+ TimeLogTemplate template = new TimeLogTemplate ();
14
+ template .execute (new Callback () {
15
+ @ Override
16
+ public void call () {
17
+ log .info ("비즈니스 로직1 실행" );
18
+ }
19
+ });
20
+
21
+ template .execute (new Callback () {
22
+ @ Override
23
+ public void call () {
24
+ log .info ("비즈니스 로직2 실행" );
25
+ }
26
+ });
27
+ }
28
+
29
+ @ Test
30
+ void callBackV2 () {
31
+ TimeLogTemplate template = new TimeLogTemplate ();
32
+ template .execute (() -> log .info ("비즈니스 로직1 실행" ));
33
+ template .execute (() -> log .info ("비즈니스 로직2실행" ));
34
+ }
35
+ }
Original file line number Diff line number Diff line change
1
+ package hello .advanced .trace .strategy .code .template ;
2
+
3
+ public interface Callback {
4
+
5
+ void call ();
6
+ }
Original file line number Diff line number Diff line change
1
+ package hello .advanced .trace .strategy .code .template ;
2
+
3
+ import hello .advanced .trace .strategy .code .strategy .Strategy ;
4
+ import lombok .extern .slf4j .Slf4j ;
5
+
6
+ @ Slf4j
7
+ public class TimeLogTemplate {
8
+
9
+ public void execute (Callback callback ) {
10
+ long startTime = System .currentTimeMillis ();
11
+ callback .call ();
12
+ long endTime = System .currentTimeMillis ();
13
+ long resultTime = endTime - startTime ;
14
+ log .info ("resultTime = {}" , resultTime );
15
+ }
16
+ }
You can’t perform that action at this time.
0 commit comments