在Spring Boot中使用Interpreter运行Jolie程序时发生未知操作错误

时间:2019-07-07 17:06:06

标签: java spring-boot jolie

不久前,我得到了this问题的答案,该问题涉及如何结合Jolie和Spring Boot功能。我已经尝试在Jolie中使用LocalCommChannel类和本地inputPort来实现建议的解决方案,但是不幸的是,我在请求中传递的操作无法识别。下面是我的Spring Boot应用程序,它是试图运行解释器和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";
    }
}

main.ol

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上阅读代码?

1 个答案:

答案 0 :(得分:1)

在Java代码中:

  • 您没有使用replaceAll,也没有像我在原始帖子http://fmontesi.github.io/2015/01/30/running-jolie-inside-of-java.html
  • 中那样转义$
  • 您没有启动口译员。自从我写了我的原始博客文章以来,实际上有了一个新的更高级的界面:您可以调用interpreter.start(),返回一个将来可以等待的时间,直到解释器准备接收消息为止。

在朱莉代码中:

  • 您无需调用运行时即可使用本地位置。
  • 在发送响应后(请注意放在方括号中),您将请求-响应操作twice的计算结果放入了。

这是一个对我有用的例子。注意,我使用的是:而不是;作为参数中的路径分隔符(我正在Linux上进行测试),您可能需要更改它。

Example.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();
        }
    }
}

main.ol

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
    }]
}

希望有帮助!