Java 自动执行 POST 请求的后台服务
Java background service to automate POST requests
我有一个 Java 服务,它从 table 'A' 读取数据(包括 BLOB),将这些数据写入 table 'B' 并上传将 BLOB 作为 ByteArrayInputStream 存储到存储服务器(基本上是一个小型迁移服务)。上传过程成功完成后,两个 table 上的布尔列都设置为 1。该服务的工作方式是将 POST 请求发送到 REST 服务器,对于复制的每一行,都会返回一个位置 header 作为响应。处理 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”,它 returns status 200 经过一段时间的处理,像这样。
我的问题是,我如何实现一个 java 后台服务(比如客户端 运行 在与服务器相同的物理机器上)发送 POST 请求自动到 REST 服务器进行迁移过程?在第一个 运行 上,后台服务应该处理整个 table,之后后台服务需要 运行 定期检查是否有新行被添加到 table并相应地处理它们。我是一个 Java 菜鸟,非常感谢任何形式的 help/suggestions。
My question is, how can I implement a java background service(like a
client that would run on the same physical machine as the server) that
would send POST requests automatically to the REST server for
migration process?
您可以使用 JAX-RS client API 将 HTTP POST
请求发送到 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 ... */);
On the first run, the background service should process the entire
table and after that the background service needs to run periodically
to check whether new rows have been added to the table and process
them accordingly.
立即触发第一个(一组)HTTP 请求,然后使用 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.
}
}
我有一个 Java 服务,它从 table 'A' 读取数据(包括 BLOB),将这些数据写入 table 'B' 并上传将 BLOB 作为 ByteArrayInputStream 存储到存储服务器(基本上是一个小型迁移服务)。上传过程成功完成后,两个 table 上的布尔列都设置为 1。该服务的工作方式是将 POST 请求发送到 REST 服务器,对于复制的每一行,都会返回一个位置 header 作为响应。处理 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”,它 returns status 200 经过一段时间的处理,像这样。
我的问题是,我如何实现一个 java 后台服务(比如客户端 运行 在与服务器相同的物理机器上)发送 POST 请求自动到 REST 服务器进行迁移过程?在第一个 运行 上,后台服务应该处理整个 table,之后后台服务需要 运行 定期检查是否有新行被添加到 table并相应地处理它们。我是一个 Java 菜鸟,非常感谢任何形式的 help/suggestions。
My question is, how can I implement a java background service(like a client that would run on the same physical machine as the server) that would send POST requests automatically to the REST server for migration process?
您可以使用 JAX-RS client API 将 HTTP POST
请求发送到 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 ... */);
On the first run, the background service should process the entire table and after that the background service needs to run periodically to check whether new rows have been added to the table and process them accordingly.
立即触发第一个(一组)HTTP 请求,然后使用 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.
}
}