不久前,我得到了this问题的答案,该问题涉及如何结合Jolie和Spring Boot功能。我已经尝试在Jolie中使用LocalCommChannel类和本地inputPort来实现建议的解决方案,但是不幸的是,我在请求中传递的操作无法识别。下面是我的Spring Boot应用程序,它是试图运行解释器和Jolie程序的简单控制器。
@SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
@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上阅读代码?
答案 0 :(得分:1)
在Java代码中:
在朱莉代码中:
twice
的计算结果放入了。这是一个对我有用的例子。注意,我使用的是:而不是;作为参数中的路径分隔符(我正在Linux上进行测试),您可能需要更改它。
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
}]
}
希望有帮助!