java 執行JavaScript 以及容器話的問題

來源:互聯網
上載者:User

標籤:引擎   shanghai   linu   9.png   javascrip   localtime   mat   man   containe   

1. 可選方案   a. jdk 6 開始內建的Rhino 引擎  b. jdk8 替換的nashorn (效能高,對於ECMA 支援更好)  c. java 版的nodejs vertx.io 2. 實踐操作
public static void main(String[] args) {        ScriptEngineManager manager = new ScriptEngineManager();        ScriptEngine engine = manager.getEngineByName("nashorn");        try {            int value=2;            SimpleBindings bind=new SimpleBindings();            SimpleBindings bind2=new SimpleBindings();            SimpleBindings bind3=new SimpleBindings();            bind.put("點數", value);            bind2.put("a", value);            bind2.put("b", value);            bind3.put("a1", value+1);            bind3.put("b1", value+2);             System.out.println(engine.eval("點數<50?50:Math.ceil(56/50)*50",bind));            System.out.println(engine.eval("demosum=function(a,b){return a+b}; demosum(a,b);",bind2));    // 定義方法,並調用支援參數綁定            System.out.println(engine.eval("(function(a1,b1){function demosub(a,b){ return a+b;}return demosub(a1,b1)})(a1,b1)",bind3));    // 使用js 立即執行函數          } catch (ScriptException e) {            e.printStackTrace();        }    }
 3. docker 容器處理  docker 容器實踐有一個原則就是容器的鏡像要儘可能的小一般大家會選擇使用 alpine linux 發行版本對於Dockerfile 如下:
FROM frolvlad/alpine-oraclejdk8:fullVOLUME /tmpADD demo.class demo.classRUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtimeENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","app"]
 注意鏡像的tag 選擇使用 full 執行結果 



java 執行JavaScript 以及容器話的問題

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.