不久前,我得到了有关如何结合Jolie和Spring Boot功能的this问题的答案。我已经尝试在Jolie中使用LocalCommChannel类和本地inputPort来实现建议的解决方案,但是不幸的是,我在请求中传递的操作无法识别。下面是我的Spring Boot Application,它是试图运行解释器和Jolie程序的简单控制器。
App.java
@SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
HelloController.java
@RestController
public class HelloController {
@RequestMapping("/")
String hello() {
String jh = System.getenv( "JOLIE_HOME" );
String arg[] = new String[]{
"-l",
".\\lib\\*;$JOLIE_HOME\\lib;$JOLIE_HOME\\javaServices\\*;$JOLIE_HOME\\extensions\\*".replace("$JOLIE_HOME", jh),
"-i",
"$JOLIE_HOME\\include".replace("$JOLIE_HOME", jh),
"main.ol"
};
try {
final Interpreter interpreter = new Interpreter(arg, HelloController.class.getClassLoader(), null);
Value v = Value.create();
v.setFirstChild( "number", 5 );
CommMessage request = CommMessage.createRequest( "twice", "/", v );
LocalCommChannel c = interpreter.commCore().getLocalCommChannel();
c.send( request );
CommMessage response = c.recvResponseFor( request );
if ( response.isFault() ) {
System.out.println("thrown response.fault()");
throw response.fault();
}
return response.value().toString();
} catch (Exception e) {
e.printStackTrace();
}
return "Bad result";
}
}
主程序
include "console.iol"
include "runtime.iol"
execution { concurrent }
type TwiceRequest: void {
.number: int
}
type TwiceResponse: void {
.result: int
}
interface LocalInterface {
RequestResponse:
twice(TwiceRequest)(TwiceResponse)
}
inputPort TwiceIP {
Location: "local"
Interfaces: LocalInterface
}
init
{
getLocalLocation@Runtime()( TwiceIP.location )
}
main {
[twice( request )( response )] {
println@Console("Hello World")();
response.result = request.number * 2
}
}
输出量
2019-07-07 18:20:53.315 WARN 19236 --- [ Thread-3] Jolie : [main.ol] Received
a message for operation twice, not specified in the input port LocalInputPort
at the receiving service. Sending IOException to the caller.
thrown response.fault()
jolie.runtime.FaultException: IOException: Invalid operation: twice
at jolie.net.CommCore$CommChannelHandlerRunnable.handleMessage(CommCore.java:594)
at jolie.net.CommCore$CommChannelHandlerRunnable.run(CommCore.java:620)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
我试图跟踪程序中经历了哪些Jolie类和函数,并且我注意到当调用getLocalCommChannel()时,它会创建新的LocalListener,从而创建全新的和空接口的InputPort,因此我不知道两次操作将如何可见。我怀疑我可能必须在CommCore中使用addLocalInputPort()或在LocalListener中使用mergeInterface()并自行配置,但是我希望Jolie可以在不采取任何措施的情况下解决这一问题。
PS:是否有关于那些Jolie核心Java类的在线文档,或者最好唯一的选择是在github上阅读代码?
最佳答案
在Java代码中:
您没有在使用replaceAll,也没有像我在原始帖子中那样转义$:http://fmontesi.github.io/2015/01/30/running-jolie-inside-of-java.html
您没有启动解释器。自从我写了我的原始博客文章以来,实际上有了一个新的更高级的界面:您可以调用interpreter.start(),返回一个将来可以等待的时间,直到解释器准备接收消息为止。
在Jolie代码中:
您无需调用运行时即可使用本地位置。
发送响应后(请注意放在方括号中),对请求-响应操作twice
进行计算。
这是一个对我有用的例子。请注意,我正在使用:而不是;作为参数中的路径分隔符(我正在Linux上进行测试),您可能需要更改它。
范例.java
public class Example
{
public static void main( String[] args )
{
String jh = System.getenv( "JOLIE_HOME" );
String arg[] = {
"-l",
"./lib/*:$JOLIE_HOME/lib:$JOLIE_HOME/javaServices/*:$JOLIE_HOME/extensions/*".replaceAll("\\$JOLIE_HOME", jh),
"-i",
"$JOLIE_HOME/include".replaceAll("\\$JOLIE_HOME", jh),
"main.ol"
};
try {
final Interpreter interpreter = new Interpreter(arg, Example.class.getClassLoader(), null);
Exception e = interpreter.start().get();
if ( e != null )
throw e;
Value v = Value.create();
v.setFirstChild( "number", 5 );
CommMessage request = CommMessage.createRequest( "twice", "/", v );
LocalCommChannel c = interpreter.commCore().getLocalCommChannel();
c.send( request );
CommMessage response = c.recvResponseFor( request ).get();
if ( response.isFault() ) {
System.out.println("thrown response.fault()");
throw response.fault();
}
System.out.println( response.value().getFirstChild( "result" ).strValue() );
} catch (Exception e) {
e.printStackTrace();
}
}
}
主程序
include "console.iol"
execution { concurrent }
type TwiceRequest: void {
.number: int
}
type TwiceResponse: void {
.result: int
}
interface LocalInterface {
RequestResponse:
twice(TwiceRequest)(TwiceResponse)
}
inputPort TwiceIP {
Location: "local"
Interfaces: LocalInterface
}
main {
[twice( request )( response ) {
println@Console("Hello World")();
response.result = request.number * 2
}]
}
希望有帮助!