【发布时间】:2021-05-25 17:45:33
【问题描述】:
我是 Akka HTTP 的初学者,我不太了解如何在路由中使用 actor,因为 Akka Http 中的示例都非常特殊。 我想让用户输入一个 5 个整数和 1 个字母的数字。如果它尊重这一点,它将显示数字,如果不是,系统参与者将发送一条消息。
我使用了很多不同的方式来使其发挥作用,但我也遇到了演员的问题和 if (numCP.contains("[0-9]{7}[AZ]{1}" )) 即使我将其更改为 1 个整数并写入 1 个整数,它也永远不会受到尊重。
这是我的代码,请原谅我的大量评论:
public class HttpServerActorCP extends AllDirectives {
public static ActorSystem<Printer.PrintMe> system = ActorSystem.create(Behaviors.empty(), "routes");
//public static ActorRef<Printer.PrintMe> ref = system;;
public boolean isnumCP(String numCP) {
// if (numCP.contains("[0-9]{7}+[a-zA-Z]{1}") && numCP.length() == 8) {
if (numCP.contains("[0-9A-Z]+") && numCP.length() == 8) {
return true;
}
return false;
}
final Function<Entry, String> appel = entry -> entry.getKey() + " = '" + entry.getValue() + "'";
/*
* private CompletionStage<Optional<String>> getJob(Long jobId) { return
* AskPattern.ask( Nouv, replyTo -> String, Duration.ofSeconds(3),
* system.scheduler()); }
*/
public static void main(String[] args) throws Exception {
final Http http = Http.get(system);
HttpServerActorCP app = new HttpServerActorCP();
final CompletionStage<ServerBinding> binding = http.newServerAt("localhost", 8080).bind(app.createRoute());
System.out.println("Server online at http://localhost:8080/\nPress RETURN to stop...");
System.in.read();
binding.thenCompose(ServerBinding::unbind).thenAccept(unbound -> system.terminate());
}
@SuppressWarnings("unlikely-arg-type")
private Route createRoute() {
/*final Route route1 = extractRequestContext(ctx -> {
ctx.getLog().debug("Using access to additional context available, like the logger.");
final HttpRequest request = ctx.getRequest();
return complete("Request method is " + request.method().name() +" and content-type is " + request.entity().getContentType());
}
);// tests:
*/
/*
* final Route route2 = mapRequest(req -> req.withMethod(HttpMethods.POST), ()
* -> extractRequest(req -> complete("The request method was " +
* req.method().name())));
*/
return concat(
/*
* path(segment("cem").slash("file").slash(integerSegment()), numCP ->
* anyOf(Directives::get, Directives::put, () -> extractMethod(methode -> //
* complete("Requête reçu de la part de "+ numCP + " avec la méthode "
* +methode))),
*/
// pathPrefix("cem", () ->
// concat(
// put(() ->
// path("file", () -> {//{if (file.isPresent){ return
/*
* mapRequest(req -> req.withMethod(HttpMethods.POST), () -> extractRequest(req
* -> complete("The request method was " + req.method().name()
* //+requestEntityPresent(req) ))),
*/
parameterList(numCP -> {
// if (numCP.isnumCP == true) {
// if (numCP.contains("[0-9]{7}+[a-zA-Z]{1}") //&& ((CharSequence)
// numCP).length() == 8
if (numCP.contains("[0-9]{7}[A-Z]{1}")
// if (isnumCP(numCP) == true
) {
// parameterRequiredValue(StringUnmarshallers.BOOLEAN, true, "action", () ->
// parameter("path", path ->
final String pString = numCP.stream().map(arg0 -> {
try {
return appel.apply(arg0);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}).collect(Collectors.joining(", "));
// return complete("Le numCP est " +numCP+ " il a les droits d'accès à "// +path
return complete("Les parametres sont " + pString);
//ref.tell(new Printer.PrintMe("");
}
else { return system.tell(new Printer.PrintMe("Entrez un bon numero de CP"));//("Entrez un bon numéro CP")
};
})
这是我的消息打印机类,因为我不能让我的演员只使用字符串:
public class Printer {
public static class PrintMe {
public final String message;
public PrintMe(String message) {
this.message = message;
}
}
public static Behavior<PrintMe> create() {
return Behaviors.setup(
context ->
Behaviors.receive(PrintMe.class)
.onMessage(
PrintMe.class,
printMe -> {
context.getLog().info(printMe.message);
return Behaviors.same();
})
.build());
}
}
我在使用 tell 时遇到的错误是“无法返回 void 结果”。 那么你能帮我知道使用演员有什么问题以及为什么 if contains 不起作用(我总是有消息“entrez un on numero de cp”)?
【问题讨论】:
标签: java http routes akka actor