Skip to content

Commit eedc7dc

Browse files
authored
updates for lab 4 late breaking fixes (#604)
* updates related to lab 4 late fix Signed-off-by: Mark Nelson <[email protected]>
1 parent 8411e51 commit eedc7dc

File tree

4 files changed

+96
-61
lines changed

4 files changed

+96
-61
lines changed

cloudbank-v2/spring-apps/account/src/main/resources/application.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ spring:
1515
format_sql: true
1616
show-sql: true
1717
datasource:
18-
url: ${CONNECT_STRING}
19-
username: ${DB_USERNAME}
20-
password: ${DB_PASSWORD}
18+
url: ${spring.datasource.url}
19+
username: ${spring.datasource.username}
20+
password: ${spring.datasource.password}
2121
driver-class-name: oracle.jdbc.OracleDriver
2222
type: oracle.ucp.jdbc.PoolDataSource
2323
oracleucp:

cloudbank-v2/spring-apps/transfer/src/main/java/com/example/transfer/ApplicationConfig.java

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,33 @@
33

44
package com.example.transfer;
55

6-
import io.narayana.lra.client.NarayanaLRAClient;
76
import org.springframework.beans.factory.annotation.Value;
8-
import org.springframework.context.annotation.Bean;
97
import org.springframework.context.annotation.Configuration;
108

11-
import java.net.URISyntaxException;
12-
import java.util.logging.Logger;
9+
import io.narayana.lra.client.NarayanaLRAClient;
1310

1411
@Configuration
1512
public class ApplicationConfig {
16-
private static final Logger log = Logger.getLogger(ApplicationConfig.class.getName());
13+
static String accountWithdrawUrl;
14+
static String accountDepositUrl;
15+
static String transferCancelURL;
16+
static String transferCancelProcessURL;
17+
static String transferConfirmURL;
18+
static String transferConfirmProcessURL;
1719

18-
public ApplicationConfig(@Value("${lra.coordinator.url}") String lraCoordinatorUrl) {
19-
log.info(NarayanaLRAClient.LRA_COORDINATOR_URL_KEY + " = " + lraCoordinatorUrl);
20+
public ApplicationConfig(@Value("${lra.coordinator.url}") String lraCoordinatorUrl,
21+
@Value("${account.withdraw.url}") String accountWithdrawUrl,
22+
@Value("${account.deposit.url}") String accountDepositUrl,
23+
@Value("${transfer.cancel.url}") String transferCancelURL,
24+
@Value("${transfer.cancel.process.url}") String transferCancelProcessURL,
25+
@Value("${transfer.confirm.url}") String transferConfirmURL,
26+
@Value("${transfer.confirm.process.url}") String transferConfirmProcessURL) {
2027
System.getProperties().setProperty(NarayanaLRAClient.LRA_COORDINATOR_URL_KEY, lraCoordinatorUrl);
28+
this.accountWithdrawUrl = accountWithdrawUrl;
29+
this.accountDepositUrl = accountDepositUrl;
30+
this.transferCancelURL = transferCancelURL;
31+
this.transferCancelProcessURL = transferCancelProcessURL;
32+
this.transferConfirmURL = transferConfirmURL;
33+
this.transferConfirmProcessURL = transferConfirmProcessURL;
2134
}
22-
23-
@Bean
24-
public NarayanaLRAClient NarayanaLRAClient() throws URISyntaxException {
25-
return new NarayanaLRAClient();
26-
}
27-
2835
}

cloudbank-v2/spring-apps/transfer/src/main/java/com/example/transfer/TransferService.java

Lines changed: 59 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -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

cloudbank-v2/spring-apps/transfer/src/main/resources/application.yaml

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,20 @@
44
server:
55
port: 8080
66

7-
mp.lra:
8-
coordinator.url: http://otmm-tcs.otmm.svc.cluster.local:9000/api/v1/lra-coordinator
9-
propagation.active: true
10-
participant.url: http://localhost:8080
11-
coordinator.headers-propagation.prefix: ["x-b3-", "oracle-tmm-", "authorization", "refresh-"]
12-
13-
deposit:
14-
account:
15-
service:
16-
url: http://account.application:8080/deposit
17-
withdraw:
18-
account:
19-
service:
20-
url: http://account.application:8080/withdraw
21-
7+
account:
8+
deposit:
9+
url: http://account.application:8080/deposit/deposit
10+
withdraw:
11+
url: http://account.application:8080/withdraw/withdraw
12+
transfer:
13+
cancel:
14+
url: http://transfer.application:8080/cancel
15+
process:
16+
url: http://transfer.application:8080/processcancel
17+
confirm:
18+
url: http://transfer.application:8080/confirm
19+
process:
20+
url: http://transfer.application:8080/processconfirm
2221
lra:
2322
coordinator:
2423
url: http://otmm-tcs.otmm.svc.cluster.local:9000/api/v1/lra-coordinator

0 commit comments

Comments
 (0)