首页 > 解决方案 > 自动化 POST 请求的 Java 后台服务

问题描述

我有一个 Java 服务,它从表“A”读取数据(包括 BLOB),将这些数据写入表“B”并将 BLOB 作为 ByteArrayInputStream(基本上是一个小型迁移服务)上传到存储服务器。上传过程成功完成后,两个表上的布尔列都设置为 1。该服务的工作方式是将 POST 请求发送到 REST 服务器,并且对于复制的每一行,都会返回一个位置标头作为响应。处理 POST 请求的资源方法如下所示。

@POST
@Produces({MediaType.APPLICATION_JSON})
public Response migrateToMinio(@Context UriInfo uriInfo) throws Exception {
    tiedostoService = new TiedostoService();
    attachmentService = new AttachmentService();
    List<Tiedosto> tiedostoList = tiedostoService.getAllFiles();
    List<String> responseList = new ArrayList<>();
    Response r;
    Integer newRow;
    String responseData = null;
    int i=1;
    for (Tiedosto tiedosto : tiedostoList) {
        Attachment attachment = new Attachment();
        attachment.setCustomerId(tiedosto.getCustomerId());
        attachment.setSize(tiedosto.getFileSize());
        newRow = attachmentService.createNew(attachment);
        UriBuilder builder = uriInfo.getAbsolutePathBuilder();
        if (newRow == 1) {
            builder.path(Integer.toString(i));
            r = Response.created(builder.build()).build();
            responseData = r.getLocation().toString();
            i++;
        }
        responseList.add(responseData);
    }
    String jsonString = new Gson().toJson(responseList);
    return Response.status(Response.Status.OK).entity(jsonString).build();
}

首先,通过 POST 生成一个 JWT 到 api 并将其用作承载令牌,另一个 POST 被发送到“/rest/attachments”,它在处理一段时间后返回状态 200,就像这样。

在此处输入图像描述

我的问题是,我怎样才能实现一个 java 后台服务(比如一个与服务器在同一物理机器上运行的客户端),它会自动将 POST 请求发送到 REST 服务器以进行迁移过程?在第一次运行时,后台服务应该处理整个表,之后后台服务需要定期运行以检查是否已将新行添加到表中并相应地处理它们。我是一个 Java 菜鸟,非常感谢任何形式的帮助/建议。

标签: javajax-rs

解决方案


我的问题是,我怎样才能实现一个 java 后台服务(比如一个与服务器在同一物理机器上运行的客户端),它会自动将 POST 请求发送到 REST 服务器以进行迁移过程?

您可以使用JAX-RS 客户端 API将 HTTPPOST请求发送到localhost

Client client = ClientBuilder.newClient();
Response res = client.target("http://localhost/your/path/here").request("application/json").post(/* ... Entity that goes in the message body ... */);

在第一次运行时,后台服务应该处理整个表,之后后台服务需要定期运行以检查是否已将新行添加到表中并相应地处理它们。

立即触发第一个(一组)HTTP 请求,然后使用 an以固定速率(或固定间隔,取决于您的特定需求)ExecutorService触发 HTTP :POST

public class ClientService {

    private static final ScheduledExecutorService EXECUTOR = Executors.newScheduledThreadPool(1);

    private static final long INITIAL_DELAY = 10_000;
    private static final long UPDATE_RATE = 10_000;

    public static void main(String[] args) {
        // Fire first (full?) update trigger here
        fireInitialMigration();
        // For subsequent (incremental?) updates, schedule an HTTP POST to occur at a fixed rate:
        EXECUTOR.scheduleAtFixedRate(() -> fireSubsequentUpdate(), INITIAL_DELAY, UPDATE_RATE, TimeUnit.MILLISECONDS);
        // Keep main thread alive
        while (true);
    }

    private static void fireInitialMigration() {
        Client client = ClientBuilder.newClient();
        Response res = client.target("http://localhost/your/path/here").request("application/json").post(/* ... Entity that goes in the message body ... */);
    }

    private static void fireSubsequentUpdate() {
        // Similar to initialMigration(), but change Entity/set of HTTP POSTs according to your needs.
    }
}

推荐阅读