我有一个Java服务,它从表'A‘读取数据(包括BLOB),将这些数据写入表'B’,然后将BLOB作为ByteArrayInputStream上传到存储服务器(基本上是一个小型迁移服务)。成功完成上载过程后,两个表上的布尔列都设置为1。该服务的工作方式是将POST请求发送到REST服务器,并且对于复制的每一行,都会返回一个location头作为响应。处理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新手,真的很感谢任何形式的帮助/建议。
发布于 2018-08-26 03:54:15
我的问题是,我如何实现java后台服务(就像运行在与服务器相同的物理机上的客户端),它会自动将POST请求发送到REST服务器进行迁移过程?
您可以使用JAX-RS client API向localhost
发送HTTP POST
请求
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
:
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.
}
}
https://stackoverflow.com/questions/52020174
复制相似问题