I have created a two SAP CAP Java App. Now i want to call a function of 2nd CAP Java application from the 1st CAP Java application locally to check whether the Correlation IDs are same or not.
Currently I am using RestTemplate to call the function but i have to manually pass the Correlation Id in the header otherwise the Correlation Ids are not same.
1st App (running on port 8080):
package customer.bookstore.handlers;
import java.util.Collections;
import java.util.UUID;
import org.apache.commons.codec.binary.Base64;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import com.sap.cds.Result;
import com.sap.cds.ql.Select;
import com.sap.cds.ql.cqn.CqnSelect;
import com.sap.cds.services.handler.EventHandler;
import com.sap.cds.services.handler.annotations.On;
import com.sap.cds.services.handler.annotations.ServiceName;
import com.sap.cds.services.persistence.PersistenceService;
import com.sap.hcp.cf.logging.common.LogContext;
import cds.gen.booksservice.Authors;
import cds.gen.booksservice.Authors_;
import cds.gen.booksservice.GetAuthorContext;
@Component
@ServiceName("BooksService")
public class BooksService implements EventHandler {
private Logger LOG = LoggerFactory.getLogger(BooksService.class);
private static final String CORRELATION_ID_HEADER_NAME = "X-CorrelationID";
@Autowired
PersistenceService db;
@Autowired
OrdersService ordersService;
@Autowired
RestTemplate restTemplate;
(event = GetAuthorContext.CDS_NAME)
private void getAuthor(GetAuthorContext context) {
LOG.info("***Inside getAuthor()***");
String correlationId = LogContext.getCorrelationId();
LOG.info("***Correlation ID***: {}", correlationId);
if (correlationId == null) {
correlationId = UUID.randomUUID().toString();
}
LOG.info("Using Correlation ID: {}", correlationId);
CqnSelect selectDynamic = Select.from(Authors_.class).columns("firstname")
.byId("286ae4b1-b845-4af8-9827-4eb04fe5f380");
Result getAuthorResult = db.run(selectDynamic);
LOG.info("***getBookResult***: {}", getAuthorResult);
Authors author = getAuthorResult.single(Authors.class);
LOG.info("***book-info***: {}", author);
LOG.error("***book-error***: {}", author);
LOG.debug("***book-debug***: {}", author);
String plainCreds = "authenticated:";
byte[] plainCredsBytes = plainCreds.getBytes();
byte[] base64CredsBytes = Base64.encodeBase64(plainCredsBytes);
String base64Creds = new String(base64CredsBytes);
HttpHeaders headers = new HttpHeaders();
headers.add("Authorization", "Basic " + base64Creds);
headers.add(CORRELATION_ID_HEADER_NAME, correlationId);
headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
// // Call the external CAP Java app using RestTemplate
String url = "http://localhost:8081/odata/v4/BookService/getBook()";
HttpEntity<String> request = new HttpEntity<>(headers);
try {
ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.GET, request, String.class);
if (response.getStatusCode().is2xxSuccessful()) {
LOG.info("***External API Response - author***: {}", response.getBody());
} else {
LOG.error("***Failed to call external API*** Status: {}", response.getStatusCode());
}
} catch (Exception e) {
LOG.error("***Error calling external API***: {}", e);
}
context.setResult(author);
}
}
2nd App (running on port 8081):
package customer.books.handlers;
import java.util.UUID;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.sap.cds.Result;
import com.sap.cds.ql.Select;
import com.sap.cds.ql.cqn.CqnSelect;
import com.sap.cds.services.handler.EventHandler;
import com.sap.cds.services.handler.annotations.On;
import com.sap.cds.services.handler.annotations.ServiceName;
import com.sap.cds.services.persistence.PersistenceService;
import com.sap.cds.services.request.ParameterInfo;
import com.sap.hcp.cf.logging.common.LogContext;
import cds.gen.bookservice.Books;
import cds.gen.bookservice.Books_;
import cds.gen.bookservice.GetBookContext;
@Component
@ServiceName("BookService")
public class BookServiceHandler implements EventHandler {
private Logger LOG = LoggerFactory.getLogger(BookServiceHandler.class);
@Autowired
PersistenceService db;
@Autowired
ParameterInfo parameterInfo;
(event = GetBookContext.CDS_NAME)
private void getBook(GetBookContext context){
ParameterInfo parameterInfo = context.getParameterInfo();
LOG.info("***REST - Inside getBook()*** : {}", parameterInfo.getHeaders());
CqnSelect selectDynamic = Select.from(Books_.class).columns("name").byId(1);
Result getBookResult = db.run(selectDynamic);
LOG.info("***REST - getBookResult***: {}", getBookResult);
Books book = getBookResult.single(Books.class);
LOG.info("***REST - book-info***: {}", book);
LOG.error("***REST - book-error***: {}", book);
LOG.debug("***REST - book-debug***: {}", book);
context.setResult(book);
}
}
I don't want to use Rest Template instead i want to use the SAP CAP suggested Remote Services
to basically test whether the Correlation Ids are same or not.
Reference: https://cap.cloud.sap/docs/java/operating-applications/observability#correlation-ids
Please guide me to achieve it?
It would be of great help.
Thanks,
Abhinav.
Request clarification before answering.
| User | Count |
|---|---|
| 10 | |
| 5 | |
| 5 | |
| 5 | |
| 4 | |
| 2 | |
| 2 | |
| 2 | |
| 1 | |
| 1 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.