1
1
package org .example .project .service .job ;
2
2
3
+ import jakarta .annotation .PostConstruct ;
4
+ import jakarta .annotation .PreDestroy ;
3
5
import lombok .RequiredArgsConstructor ;
6
+ import lombok .extern .slf4j .Slf4j ;
4
7
import org .example .project .configuration .InvestConfig ;
5
8
import org .example .project .domain .Stoncks ;
6
9
import org .example .project .repository .StoncksRepository ;
12
15
import java .math .BigDecimal ;
13
16
import java .time .LocalDateTime ;
14
17
18
+ @ Slf4j
15
19
@ Service
16
20
@ RequiredArgsConstructor
17
21
public class CheckStoncksJob {
18
-
19
22
private final InvestConfig investConfig ;
20
23
private final StoncksRepository stoncksRepository ;
24
+ private InvestApi api ;
25
+
26
+ @ PostConstruct
27
+ public void init () {
28
+ if (investConfig .getToken () != null ) {
29
+ this .api = InvestApi .create (investConfig .getToken ());
30
+ }
31
+ }
21
32
22
33
@ Scheduled (fixedRate = 10000L )
23
- private void getStoncks () {
24
- if (investConfig .getToken () == null )
34
+ public void getStoncks () {
35
+ if (api == null || investConfig .getToken () == null ) {
25
36
return ;
26
- var api = InvestApi .create (investConfig .getToken ());
27
- Portfolio portfolio = api .getOperationsService ().getPortfolioSync (investConfig .getUserId ());
28
- BigDecimal value = portfolio .getTotalAmountPortfolio ().getValue ();
29
- stoncksRepository .save (Stoncks .builder ()
30
- .time (LocalDateTime .now ())
31
- .value (value )
32
- .build ());
37
+ }
38
+
39
+ try {
40
+ Portfolio portfolio = api .getOperationsService ()
41
+ .getPortfolioSync (investConfig .getUserId ());
42
+
43
+ BigDecimal value = portfolio .getTotalAmountPortfolio ().getValue ();
44
+
45
+ stoncksRepository .save (Stoncks .builder ()
46
+ .time (LocalDateTime .now ())
47
+ .value (value )
48
+ .build ());
49
+ } catch (Exception e ) {
50
+ log .error (e .getLocalizedMessage ());
51
+ }
33
52
}
34
- }
53
+ }
0 commit comments