从Java调用clojure(Clojure Interop) [英] calling clojure from Java (Clojure Interop)
问题描述
从Clojoure调用Java非常简单明了,但事实证明反向是不可预测的。
Calling Java from Clojoure is quite simple and straightforward but the inverse has proven to be unpredictable.
他们似乎有两种方式:
1)以下课程
1)the following classes
i) import clojure.java.api.Clojure; ,
ii) import clojure.lang.IFn;
2)将你的clojure编译成uberjar,然后将其导入java
代码。 / p>
2)compile your clojure into an uberjar then import it into the java code.
我选择了第二个选项,因为它更直接。
I have opted for the 2nd option as it's more straight forward.
这是clojure代码
Here is the clojure code
(ns com.test.app.service
(:gen-class
:name com.test.app.service
:main false
:methods [^{:static true} [returned [int] int]]))
(defn returned
[number]
(* 2 number))
(defn -returned
[number]
(returned number))
这是Java代码。
package com.s.profile;
import java.util.*;
import com.microsoft.azure.serverless.functions.annotation.*;
import com.microsoft.azure.serverless.functions.*;
import com.test.app.service;
/**
* Azure Functions with HTTP Trigger.
*/
public class Function {
/**
* This function listens at endpoint "/api/hello". Two ways to invoke it using "curl" command in bash:
* 1. curl -d "HTTP Body" {your host}/api/hello
* 2. curl {your host}/api/hello?name=HTTP%20Query
*/
@FunctionName("hello")
public HttpResponseMessage<String> hello(
@HttpTrigger(name = "req", methods = {"get", "post"}, authLevel = AuthorizationLevel.ANONYMOUS) HttpRequestMessage<Optional<String>> request,
final ExecutionContext context) {
context.getLogger().info("Java HTTP trigger processed a request.");
// Parse query parameter
String query = request.getQueryParameters().get("name");
String name = request.getBody().orElse(query);
if (name == null) {
return request.createResponse(400, "Please pass a name on the query string or in the request body");
} else {
service.returned(4);
context.getLogger().info("process data" );
return request.createResponse(200, "Hellos, " + name );
}
}
}
我什么时候做 service.returned(4);系统永远不会回来。我无法弄清楚为什么对我来说它的功能不会从Clojure返回,但我看不出原因。
When ever I make the "service.returned(4);" the system never returns. I can't quite figure out why to me it comes off like the function doesn't return from Clojure but I can't see the cause.
只是为了添加一些上下文我已经尝试过它的一个简单的hello world java app,它打印出结果并且它可以工作。当我尝试在Azure功能中实现它时。
Just to add some context I have tried it when its a simple hello world java app which just prints out the result and it works. It's when I try implement it in the Azure functions.
推荐答案
我跟着这些指令,它似乎解决了找不到类的错误。似乎在运行命令时
I followed these instructions and it seemed to resolve the error of class not found. It seems as though when running the command
mvn azure-functions:run
它不会自动查找所有导入的库。您必须使用
It doesn't automatically find all imported libraries. You either have to use
- maven-assembly-plugin
- maven-shade-plugin
这篇关于从Java调用clojure(Clojure Interop)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!