1
1
package com .softeer .podoarrival .event .service ;
2
2
3
+ import com .softeer .podoarrival .event .exception .EventClosedException ;
3
4
import com .softeer .podoarrival .event .exception .ExistingUserException ;
4
5
import com .softeer .podoarrival .event .model .dto .ArrivalApplicationResponseDto ;
5
6
import com .softeer .podoarrival .event .model .entity .ArrivalUser ;
14
15
import org .slf4j .Logger ;
15
16
import org .slf4j .LoggerFactory ;
16
17
import org .springframework .stereotype .Service ;
18
+ import org .springframework .transaction .annotation .Transactional ;
17
19
18
20
import java .time .LocalDate ;
21
+ import java .time .LocalDateTime ;
22
+ import java .util .Optional ;
23
+ import java .util .concurrent .CompletableFuture ;
24
+ import java .util .concurrent .ConcurrentHashMap ;
25
+ import java .util .concurrent .atomic .AtomicInteger ;
19
26
20
27
@ Slf4j
21
28
@ Service
@@ -29,9 +36,14 @@ public class ArrivalEventSyncService {
29
36
private boolean CHECK = false ;
30
37
31
38
private final String ARRIVAL_SET = "arrivalset" ;
32
- private static int MAX_ARRIVAL = 30000 ; // default
39
+ private static int MAX_ARRIVAL = 40000 ; // default
40
+ private static int count = 1 ;
33
41
34
- public ArrivalApplicationResponseDto applyEvent (AuthInfo authInfo ) {
42
+ private static AtomicInteger countAtomic = new AtomicInteger (1 );
43
+ private static ConcurrentHashMap <String , Integer > hashMap = new ConcurrentHashMap <>();
44
+
45
+
46
+ public ArrivalApplicationResponseDto applyEventWithRedis (AuthInfo authInfo ) {
35
47
String redisKey = LocalDate .now () + ARRIVAL_SET ;
36
48
37
49
if (CHECK ){
@@ -61,11 +73,71 @@ public ArrivalApplicationResponseDto applyEvent(AuthInfo authInfo) {
61
73
.build ()
62
74
);
63
75
log .info ("[당첨] 유저 전화번호 = {}, 등수 = {}" , authInfo .getPhoneNum (), grade );
64
- try {
65
- Thread .sleep (15 );
66
- } catch (InterruptedException e ) {
67
- throw new RuntimeException (e );
76
+ // try {
77
+ // Thread.sleep(15);
78
+ // } catch (InterruptedException e) {
79
+ // throw new RuntimeException(e);
80
+ // }
81
+ return new ArrivalApplicationResponseDto (true , authInfo .getName (), authInfo .getPhoneNum (), grade );
82
+ } else {
83
+ CHECK = true ;
84
+ return new ArrivalApplicationResponseDto (false , authInfo .getName (), authInfo .getPhoneNum (), grade );
85
+ }
86
+ }
87
+
88
+ @ Transactional
89
+ public synchronized ArrivalApplicationResponseDto applyEventWithSynchronized (AuthInfo authInfo ) {
90
+
91
+ if (CHECK ){
92
+ return new ArrivalApplicationResponseDto (false , authInfo .getName (), authInfo .getPhoneNum (), -1 );
93
+ }
94
+
95
+ Optional <ArrivalUser > findUser = arrivalUserRepository .findByPhoneNum (authInfo .getPhoneNum ());
96
+
97
+ int grade = count ++;
98
+ if (grade <=MAX_ARRIVAL ) {
99
+ if (findUser .isEmpty ()) {
100
+ arrivalUserRepository .save (
101
+ ArrivalUser .builder ()
102
+ .name (authInfo .getName ())
103
+ .phoneNum (authInfo .getPhoneNum ())
104
+ .role (Role .ROLE_USER )
105
+ .arrivalRank (grade )
106
+ .build ()
107
+ );
108
+ return new ArrivalApplicationResponseDto (true , authInfo .getName (), authInfo .getPhoneNum (), grade );
109
+ } else {
110
+ throw new ExistingUserException ("이미 응모한 전화번호입니다." );
68
111
}
112
+ } else {
113
+ CHECK = true ;
114
+ return new ArrivalApplicationResponseDto (false , authInfo .getName (), authInfo .getPhoneNum (), grade );
115
+ }
116
+ }
117
+
118
+ @ Transactional
119
+ public ArrivalApplicationResponseDto applyEventWithJava (AuthInfo authInfo ) {
120
+ if (CHECK ){
121
+ return new ArrivalApplicationResponseDto (false , authInfo .getName (), authInfo .getPhoneNum (), -1 );
122
+ }
123
+
124
+ //첫번째 응답
125
+ if (hashMap .containsKey (authInfo .getPhoneNum ())){
126
+ throw new ExistingUserException ("이미 응모한 전화번호입니다." );
127
+ }
128
+
129
+ int grade = countAtomic .getAndIncrement ();
130
+ // 선착순 순위에 들었다면
131
+ if (grade <= MAX_ARRIVAL ){
132
+ arrivalUserRepository .save (
133
+ ArrivalUser .builder ()
134
+ .name (authInfo .getName ())
135
+ .phoneNum (authInfo .getPhoneNum ())
136
+ .role (Role .ROLE_USER )
137
+ .arrivalRank (grade )
138
+ .build ()
139
+ );
140
+ log .info ("전화번호 = {}" , authInfo .getPhoneNum ());
69
141
return new ArrivalApplicationResponseDto (true , authInfo .getName (), authInfo .getPhoneNum (), grade );
70
142
} else {
71
143
CHECK = true ;
0 commit comments