首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >用于自动执行POST请求的Java后台服务

用于自动执行POST请求的Java后台服务
EN

Stack Overflow用户
提问于 2018-08-26 02:34:16
回答 1查看 603关注 0票数 1

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

代码语言:javascript
复制
@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新手,真的很感谢任何形式的帮助/建议。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-08-26 03:54:15

我的问题是,我如何实现java后台服务(就像运行在与服务器相同的物理机上的客户端),它会自动将POST请求发送到REST服务器进行迁移过程?

您可以使用JAX-RS client APIlocalhost发送HTTP POST请求

代码语言:javascript
复制
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请求,然后使用ExecutorService以固定速率(或固定间隔,取决于您的特定需求)触发HTTP POST

代码语言:javascript
复制
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.
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52020174

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档