如何创建不浪费资源的Web挂钩微服务?

我正在开发一个Web挂钩微服务,我将从另一个微服务接收数据。

我更担心我的代码是否可扩展,因为我期望有大量的数据。

从下面的代码中,我是否使用了正确的方法?

这是我的代码段:

// WebhookResource.java
@Path("push/transaction")
@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public Response pushTransactionToWebhookURL(TransactionsRequestBody transactionRequestBody) {
    LOG.info("===========Start push/transaction===========");
    Response response;
    Map<String,Object> data = new HashMap<>(INITIAL_CAPACITY);
    SuccessResponse successResponse = new SuccessResponse();
    successResponse.setStatus(StatusEnum.SUCCESS);
    String jsonResponseString = "";
    ObjectMapper mapper = new ObjectMapper();
    ObjectWriter writer = mapper.writer().withDefaultPrettyPrinter();
    data.put("message","transactions payload received");
    data.put("responseCode","00");
    data.put("responseDescription","successful");
    successResponse.setData(data);
    synchronized(this){
        webHookService.pushTransactionToWebhookURL(transactionRequestBody);
    }
    try {
        jsonResponseString = writer.writeValueAsString(successResponse);
    } catch (IOException ex) {
        LOG.severe(ex.getMessage());
    }
    response = Response.ok(successResponse,MediaType.APPLICATION_JSON).build();
    LOG.info(String.format("response=%s",jsonResponseString));
    LOG.info("===========End push/transaction===========");
    return response;
}

// WebhookService.java
public void pushTransactionToWebhookURL(TransactionsRequestBody transactionsRequestBody) {
    boolean saved = false;
    int responseStatusCode = 0;
    int retries = 0;
    int successStatusCode = HTTP_STATUS_200.getStatusCode();
    ObjectMapper mapper = new ObjectMapper();
    ObjectWriter writer = mapper.writer().withDefaultPrettyPrinter();
    String jsonString = "";
    webHookURL = transactionsRequestBody.getWebhookUrl();
    eventType = transactionsRequestBody.getEventType();
    TransactionsWebhookEvent twEvent = new TransactionsWebhookEvent();
    if (Objects.nonNull(webHookURL) && Objects.nonNull(eventType)) {
        webTarget = client.target(webHookURL);
        twEvent.setEventType(transactionsRequestBody.getEventType().toLowerCase());
        twEvent.setTransactions(transactionsRequestBody.getTransactions());
        try {
            LOG.info("Converting transaction webhook event to String");
            jsonString = writer.writeValueAsString(twEvent);
            LOG.info("Transaction webhook event successfully converted=" + jsonString);
        } catch (IOException ex) {
            LOG.severe(String.format("Error converting transaction payload: %s",ex.getMessage()));
        }
        while (responseStatusCode != successStatusCode) {
            if (Objects.nonNull(twEvent.getTransactions())) {
                LOG.info("Attempting to send transaction event to webhook URL: "
                        + webHookURL);
                LOG.info("Request Body=" + jsonString);
                Response response = webTarget.request(MediaType.APPLICATION_JSON)
                        .post(
                                Entity.entity(twEvent,MediaType.APPLICATION_JSON)
                        );
                responseStatusCode = response.getStatus();
                if (successStatusCode != responseStatusCode) {
                    LOG.info("Response Status Code=" + responseStatusCode);
                    LOG.info("retrying webhook URL...");
                    retries += 1;
                    LOG.info("Retry Count=" + retries);
                    if (retries == webhookRetryLimit) {
                        // save to database here
                        LOG.info("Retry limit has been reached. Saving to DB...");
                        Transaction transaction = new Transaction();
                        transaction.setJsonData(jsonString);
                        transaction.setWebhookUrl(webHookURL);
                        transaction.setStatus(StatusEnum.FAILURE.getStatus());
                        saved = persistDao.save(transaction);
                        if (saved) {
                            LOG.info("Transaction payload successfully persisted.");
                        }
                        break;
                    }
                } else {
                    LOG.info("Response Status Code=" + responseStatusCode);
                    LOG.info("Saving to DB...");
                    Transaction transaction = new Transaction();
                    transaction.setJsonData(jsonString);
                    transaction.setWebhookUrl(webHookURL);
                    transaction.setStatus(StatusEnum.SUCCESS.getStatus());
                    saved = persistDao.save(transaction);
                    if (saved) {
                        LOG.info("Transaction payload successfully persisted.");
                    }
                    break;
                }
            } else {
                LOG.severe("Transaction payload is empty");
                break;
            }
        }
    }
}
lixu0394 回答:如何创建不浪费资源的Web挂钩微服务?

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3019736.html

大家都在问