英文:
rxread java never stop info
问题
public static void main(String[] argc) {
Vertx vertx = Vertx.vertx();
WebClientOptions options = new WebClientOptions();
WebClient client = WebClient.create(vertx, options);
String uri = "/api/command";
String host = "mydomain";
vertx.fileSystem().rxReadFile("file.csv")
.flattenAsObservable(fileContent -> Lists.newArrayList(fileContent.toString().split("\n")))
.map(row -> row.split(";"))
.skip(1)
.map(row -> new JsonObject().put(name, row[0]))
.flatMap(row -> {
createCmd.put("CMD", "CMD");
return client.post(host, uri)
.rxSendJson(createCmd)
.map((HttpResponse<Buffer> r) -> {
Long res = r.bodyAsJsonObject()
.getJsonObject("result")
.getJsonArray("hits")
.getJsonObject(0)
.getLong("res");
return row.put("res", res);
}).toObservable();
})
.doOnComplete(() -> System.out.println("On Complete: Completed the operation"))
.doOnTerminate(() -> System.out.println("On Terminate: Terminated the operation"))
.subscribe(
content -> System.out.println("Content: " + content),
err -> System.out.println("Cannot read the file: " + err.getMessage())
);
System.out.println("Out of the Vertx Read Stream");
}
Please note that the translation is based on the content you provided. If you have any further questions or need assistance with this code, feel free to ask.
英文:
I am trying to parse a csv file and execute a post call, this is what I achieved so far:
public static void main(String[] argc) {
Vertx vertx = Vertx.vertx();
WebClientOptions options = new WebClientOptions();
WebClient client = WebClient.create(vertx, options);
String uri = "/api/command";
String host = "mydomain";
vertx.fileSystem().rxReadFile("file.csv")
.flattenAsObservable(fileContent -> Lists.newArrayList(fileContent.toString().split("\n")))
.map(row -> row.split(";"))
.skip(1)
.map(row -> new JsonObject().put(name, row[0]))
.flatMap(row -> {
createCmd.put("CMD","CMD")
return client.post(host,uri)
.rxSendJson(createCmd)
.map((HttpResponse<Buffer> r) -> {
Long res = r.bodyAsJsonObject()
.getJsonObject("result")
.getJsonArray("hits")
.getJsonObject(0)
.getLong("res");
return row.put("res", res);
}).toObservable();
})
.doOnComplete(() -> System.out.println("On Complete: Completed the operation"))
.doOnTerminate(() -> System.out.println("On Terminate: Terminated the operation"))
.subscribe(
content -> System.out.println("Content: " + content),
err -> System.out.println("Cannot read the file: " + err.getMessage())
);
System.out.println("Out of the Vertx Read Stream");
}
Why this code never reaches the last line?
System.out.println("Out of the Vertx Read Stream");
Am i blocking somehow the execution of the thread and it gets stuck or it is normal behaviour?
EDIT:
I am also getting this Warning
Apr 10, 2020 5:57:51 PM io.vertx.core.impl.BlockedThreadChecker
WARNING: Thread Thread[vert.x-worker-thread-5,5,main]=Thread[vert.x-worker-thread-5,5,main] has been blocked for 75944 ms, time limit is 60000 ms
io.vertx.core.VertxException: Thread blocked
答案1
得分: 0
以下是翻译好的代码部分:
import io.vertx.ext.web.client.WebClientOptions;
import io.vertx.rxjava.core.AbstractVerticle;
import io.vertx.rxjava.core.Vertx;
import io.vertx.rxjava.ext.web.client.WebClient;
import io.vertx.rxjava.core.buffer.Buffer;
import io.vertx.rxjava.ext.web.client.HttpResponse;
import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;
import rx.Observable;
import rx.Single;
import java.util.List;
import java.util.ArrayList;
public class Main extends AbstractVerticle {
@Override
public void start() throws Exception {
Vertx vertx = Vertx.vertx();
WebClientOptions options = new WebClientOptions();
WebClient client = WebClient.create(vertx, options);
String uri = "/api/command";
String host = "mydomain";
vertx.fileSystem().rxReadFile("file.csv")
.flattenAsObservable(fileContent -> {
List<String> rows = new ArrayList<>();
String[] lines = fileContent.toString().split("\n");
for (String line : lines) {
rows.add(line);
}
return Observable.from(rows);
})
.map(row -> row.split(";"))
.skip(1)
.map(row -> {
JsonObject jsonObject = new JsonObject();
jsonObject.put("name", row[0]); // 'name' is used as a key here
return jsonObject;
})
.flatMap(row -> {
JsonObject createCmd = new JsonObject();
createCmd.put("CMD", "CMD");
return client.post(host, uri)
.rxSendJson(createCmd)
.map((HttpResponse<Buffer> r) -> {
Long res = r.bodyAsJsonObject()
.getJsonObject("result")
.getJsonArray("hits")
.getJsonObject(0)
.getLong("res");
return row.put("res", res);
}).toObservable();
})
.doOnComplete(() -> System.out.println("On Complete: Completed the operation"))
.doOnTerminate(() -> System.out.println("On Terminate: Terminated the operation"))
.subscribe(
content -> System.out.println("Content: " + content),
err -> System.out.println("Cannot read the file: " + err.getMessage())
);
System.out.println("Out of the Vertx Read Stream");
}
}
请注意,我对代码中的一些类型和方法进行了适当的翻译和调整,以使其更符合Java的语法和要求。如果您对翻译有任何疑问,请随时提问。
英文:
Just have spend some time and what is mine result
import io.vertx.ext.web.client.WebClientOptions;
import io.vertx.rxjava.core.AbstractVerticle;
import io.vertx.rxjava.core.Vertx;
import io.vertx.rxjava.ext.web.client.WebClient;
public class Main extends AbstractVerticle {
@Override
public void start() throws Exception {
Vertx vertx = Vertx.vertx();
WebClientOptions options = new WebClientOptions();
WebClient client = WebClient.create(vertx, options);
String uri = "/api/command";
String host = "mydomain";
vertx.fileSystem().rxReadFile("file.csv")
.flattenAsObservable(fileContent -> Lists.newArrayList(fileContent.toString().split("\n"))) //The method flattenAsObservable((<no type> fileContent) -> {}) is undefined for the type Single<Buffer>
.map(row -> row.split(";"))
.skip(1)
.map(row -> new JsonObject().put(name, row[0])) // what is name here?
.flatMap(row -> {
createCmd.put("CMD","CMD")
return client.post(host,uri)
.rxSendJson(createCmd)
.map((HttpResponse<Buffer> r) -> {
Long res = r.bodyAsJsonObject()
.getJsonObject("result")
.getJsonArray("hits")
.getJsonObject(0)
.getLong("res");
return row.put("res", res);
}).toObservable();
})
.doOnComplete(() -> System.out.println("On Complete: Completed the operation"))
.doOnTerminate(() -> System.out.println("On Terminate: Terminated the operation"))
.subscribe(
content -> System.out.println("Content: " + content),
err -> System.out.println("Cannot read the file: " + err.getMessage())
);
System.out.println("Out of the Vertx Read Stream");
}
}
专注分享java语言的经验与见解,让所有开发者获益!
评论