@@ -45,9 +45,13 @@ public class TransferService {
4545
4646 @ PostConstruct
4747 private void initController () {
48- try { //todo get from Environment instead...
49- withdrawUri = new URI (System .getenv ("withdraw.account.service.url" ));
50- depositUri = new URI (System .getenv ("deposit.account.service.url" ));
48+ try {
49+ withdrawUri = new URI (ApplicationConfig .accountWithdrawUrl );
50+ depositUri = new URI (ApplicationConfig .accountDepositUrl );
51+ transferCancelUri = new URI (ApplicationConfig .transferCancelURL );
52+ transferConfirmUri = new URI (ApplicationConfig .transferConfirmURL );
53+ transferProcessCancelUri = new URI (ApplicationConfig .transferCancelProcessURL );
54+ transferProcessConfirmUri = new URI (ApplicationConfig .transferConfirmProcessURL );
5155 } catch (URISyntaxException ex ) {
5256 throw new IllegalStateException ("Failed to initialize " + TransferService .class .getName (), ex );
5357 }
@@ -56,40 +60,65 @@ private void initController() {
5660 @ POST
5761 @ Path ("/transfer" )
5862 @ Produces (MediaType .APPLICATION_JSON )
59- @ LRA (value = LRA .Type .REQUIRES_NEW )
60- public Response bookTrip (@ QueryParam ("fromAccount" ) String fromAccount ,
61- @ QueryParam ("toAccount" ) String toAccount ,
62- @ QueryParam ("amount" ) long amount ,
63- @ Context UriInfo uriInfo ,
64- @ HeaderParam (LRA_HTTP_CONTEXT_HEADER ) String lraId ,
65- @ Context ContainerRequestContext containerRequestContext )
66- {
63+ @ LRA (value = LRA .Type .REQUIRES_NEW , end = false )
64+ public Response transfer (@ QueryParam ("fromAccount" ) long fromAccount ,
65+ @ QueryParam ("toAccount" ) long toAccount ,
66+ @ QueryParam ("amount" ) long amount ,
67+ @ HeaderParam (LRA_HTTP_CONTEXT_HEADER ) String lraId ) {
6768 if (lraId == null ) {
6869 return Response .serverError ().entity ("Failed to create LRA" ).build ();
6970 }
70- log .info ("Started new LRA : " + lraId );
71- withdraw (fromAccount , amount );
72- deposit (toAccount , amount );
73- return Response .ok ("transfer successful" ).build ();
71+ log .info ("Started new LRA/transfer Id: " + lraId );
72+
73+ boolean isCompensate = false ;
74+ String returnString = "" ;
75+
76+ // perform the withdrawal
77+ returnString += withdraw (fromAccount , amount );
78+ log .info (returnString );
79+ if (returnString .contains ("succeeded" )) {
80+ // if it worked, perform the deposit
81+ returnString += " " + deposit (toAccount , amount );
82+ log .info (returnString );
83+ if (returnString .contains ("failed" ))
84+ isCompensate = true ; // deposit failed
85+ } else
86+ isCompensate = true ; // withdraw failed
87+ log .info ("LRA/transfer action will be " + (isCompensate ? "cancel" : "confirm" ));
88+
89+ // call complete or cancel based on outcome of previous actions
90+ WebTarget webTarget = ClientBuilder .newClient ().target (isCompensate ? transferCancelUri : transferConfirmUri );
91+ webTarget .request ().header (TRANSFER_ID , lraId )
92+ .post (Entity .text ("" )).readEntity (String .class );
93+
94+ // return status
95+ return Response .ok ("transfer status:" + returnString ).build ();
7496
7597 }
76- private String withdraw (String accountName , long depositAmount ) {
77- log .info ("withdraw accountName = " + accountName + ", depositAmount = " + depositAmount );
78- WebTarget webTarget =
79- ClientBuilder .newClient ().target (withdrawUri ).path ("/" )
80- .queryParam ("accountName" , accountName )
81- .queryParam ("withdrawAmount" , depositAmount );
82- String withdrawOutcome = webTarget .request ().post (Entity .text ("" )).getEntity ().toString ();
98+
99+ private String withdraw (long accountId , long amount ) {
100+ log .info ("withdraw accountId = " + accountId + ", amount = " + amount );
101+ WebTarget webTarget = ClientBuilder .newClient ().target (withdrawUri ).path ("/" )
102+ .queryParam ("accountId" , accountId )
103+ .queryParam ("amount" , amount );
104+ URI lraId = Current .peek ();
105+ log .info ("withdraw lraId = " + lraId );
106+ String withdrawOutcome = webTarget .request ().header (LRA_HTTP_CONTEXT_HEADER , lraId )
107+ .post (Entity .text ("" )).readEntity (String .class );
83108 return withdrawOutcome ;
84109 }
85- private String deposit (String accountName , long depositAmount ) {
86- log .info ("deposit accountName = " + accountName + ", depositAmount = " + depositAmount );
87- WebTarget webTarget =
88- ClientBuilder .newClient ().target (depositUri ).path ("/" )
89- .queryParam ("accountName" , accountName )
90- .queryParam ("depositAmount" , depositAmount );
91- String withdrawOutcome = webTarget .request ().post (Entity .text ("" )).getEntity ().toString ();
92- return withdrawOutcome ;
110+
111+ private String deposit (long accountId , long amount ) {
112+ log .info ("deposit accountId = " + accountId + ", amount = " + amount );
113+ WebTarget webTarget = ClientBuilder .newClient ().target (depositUri ).path ("/" )
114+ .queryParam ("accountId" , accountId )
115+ .queryParam ("amount" , amount );
116+ URI lraId = Current .peek ();
117+ log .info ("deposit lraId = " + lraId );
118+ String depositOutcome = webTarget .request ().header (LRA_HTTP_CONTEXT_HEADER , lraId )
119+ .post (Entity .text ("" )).readEntity (String .class );
120+ ;
121+ return depositOutcome ;
93122 }
94123
95124 @ POST
0 commit comments