JRuby使用经验
来源:互联网 发布:单片机仿真软件有哪些 编辑:程序博客网 时间:2024/05/24 06:37
首先我是一个Java程序员,很喜欢Ruby.
公司由于业务的需要,在Java项目中引入动态语言,目的是可以快速地修改业务逻辑以响应快速变化的业务需求.于是我有幸当了一回JRuby的先锋.当初使用JRuby的时候,我对JRuby项目的了解其实就是知道它可以让Ruby运行在JVM上面,其余细节一概不知,都是在实际使用中一点点地摸索,一点点地积累回来.
在这一过程中,在 dennis_zane 同学身上,我学到了很多与Ruby相关或者不相关的东西,借机感谢一下.
JRuby的中文资料相当的稀少,在 Google上搜索,来来去去的就是介绍了下最基本的怎么从Java中调用Ruby代码,或者在Ruby中使用Java的类库.我从无数次遇到问题 => 解决问题的循环中也有那么一点点的使用心得,记录之,备忘.
* JRuby的入门资料,请访问 JRuby wiki 一般的使用方法这里都有介绍.
* 有两种方法可以使用JRuby,一是用BSF,二是使用JDK 6.BSF的方式已经过时了,JDK6中内置了对脚本语言的支持,默认的Javascript,要使用JRuby还要下载juby-engine.jar,当前最新版本是1.1.6 地址:https://scripting.dev.java.net/files/documents/4957/115972/jruby-engine-1.1.6.zip
=======================================华丽的分割线========================================
如果使用jar打包,在ruby代码中调用java的类
- require "your_jar_file_name.jar"
- import your_packet_name
- require "your_jar_file_name.jar"
- import your_packet_name
java 方法:
- class JavaClazz {
- public void javaMethod(int i) {
- System.out.pintln(i);
- }
- }
- class JavaClazz {
- public void javaMethod(int i) {
- System.out.pintln(i);
- }
- }
在Ruby中如是调用:
- java_clazz = JavaClazz.new
- java_clazz.javaMethod(1)
- java_clazz = JavaClazz.new
- java_clazz.javaMethod(1)
将会抛出类型不匹配的异常,因为所有ruby中的数值,传递到java那里都是 Long 类型,解决办法如下:
- java_clazz = JavaClazz.new
- java_clazz.javaMethod(java.lang.Integer.new(1))
- java_clazz = JavaClazz.new
- java_clazz.javaMethod(java.lang.Integer.new(1))
注:以上代码是运行在 JRuby 1.1.2 版本下,在最新版本 1.2.0中已经没有这个问题了, 多谢 RednaxelaFX 同学的指正.
=======================================华丽的分割线========================================
如果在java中使用了可变参数:
- class JavaClazz {
- public void javaMethod(int i,String... s) {
- ... // your code
- }
- }
- class JavaClazz {
- public void javaMethod(int i,String... s) {
- ... // your code
- }
- }
在ruby中应该这样调用:
- java_clazz = JavaClazz.new
- java_clazz.javaMethod(java.lang.Integer.new(1),'this is a string')
- // 只有一个参数,如果你知道java中的可变参数其实是一个数组的话
- java_clazz.javaMethod(java.lang.Integer.new(1),[].to_java(java.lang.String))
- java_clazz = JavaClazz.new
- java_clazz.javaMethod(java.lang.Integer.new(1),'this is a string')
- // 只有一个参数,如果你知道java中的可变参数其实是一个数组的话
- java_clazz.javaMethod(java.lang.Integer.new(1),[].to_java(java.lang.String))
=======================================华丽的分割线========================================
调用java中的常量,枚举enum
- class JavaClazz {
- public final String CONSTANT = "I can not change!"
- public enum Season { winter, spring, summer, fall }
- }
- class JavaClazz {
- public final String CONSTANT = "I can not change!"
- public enum Season { winter, spring, summer, fall }
- }
- puts JavaClazz::CONSTANT
- puts JavaClazz::Season.winter
- puts JavaClazz::CONSTANT
- puts JavaClazz::Season.winter
=======================================华丽的分割线========================================
如果你想使用ruby核心包,必须正确设置jruby的加载路径,从Sun实现的JRubyScriptEngine.java的源代码可以看到:
- //加载核心包的路径就是放在这个系统属性中的
- System.getProperty("com.sun.script.jruby.loadpath");
- //可以设置自己的路径
- System.setProperty("com.sun.script.jruby.loadpath","/root/.jruby/lib/ruby/1.8")
- //加载核心包的路径就是放在这个系统属性中的
- System.getProperty("com.sun.script.jruby.loadpath");
- //可以设置自己的路径
- System.setProperty("com.sun.script.jruby.loadpath","/root/.jruby/lib/ruby/1.8")
=======================================华丽的分割线========================================
关于官方JRuby引擎的问题
Sun官方实现的脚本引擎在多并发的情况下是会比较慢的,查看JRubyScriptEngine.java的源代码,可以看到eval方法是加上了synchronized
- public synchronized Object eval(Reader reader, ScriptContext ctx)
- throws ScriptException {
- Node node = compileScript(reader, ctx);
- return evalNode(node, ctx);
- }
- public synchronized Object eval(Reader reader, ScriptContext ctx)
- throws ScriptException {
- Node node = compileScript(reader, ctx);
- return evalNode(node, ctx);
- }
我至今想不明白,这个官方实现为什么会加上 synchronized
我自己山寨了一个JRubyScriptEngine的东西,直接调用JRuby的 JavaEmbedUtils 类来执行脚本,还是相当好用的.
- /**
- *
- */
- package org.opensource.script.jruby;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.InputStream;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.Map;
- import org.jruby.Ruby;
- import org.jruby.RubyRuntimeAdapter;
- import org.jruby.javasupport.JavaEmbedUtils;
- import org.jruby.runtime.GlobalVariable;
- import org.jruby.runtime.builtin.IRubyObject;
- /**
- * 山寨版JRubyScriptEngine
- * 弃用官方版的原因是由于它封装的invokeMethod方法使用了synchronized关键字,在多并发的情况下性能极差.
- *
- * @author yanghuan
- *
- */
- public class JRubyScriptEngine {
- private final Ruby runtime;
- private final RubyRuntimeAdapter evaler;
- private final Map<String, IRubyObject> rubyObjectCache = new HashMap<String, IRubyObject>();
- public JRubyScriptEngine() {
- ArrayList<String> loadPaths = new ArrayList<String>();
- loadPaths.add("/root/.jruby/lib/ruby/1.8");
- loadPaths.add("/root/.jruby/lib/ruby/site_ruby/1.8");
- runtime = JavaEmbedUtils.initialize(loadPaths, JavaEmbedUtils
- .createClassCache(this.getClass().getClassLoader()));
- evaler = JavaEmbedUtils.newRuntimeAdapter();
- }
- /**
- * 根据脚本的路径,获取脚本eval后的Ruby对象,首先会从cache中检索,如有即时返回,如果没有则eval脚本,再返回.保证不会重复eval
- *
- * @param fullPath
- * 绝对路径
- * @return
- * @throws FileNotFoundException
- * @throws Exception
- */
- private IRubyObject getRubyObject(final String fullPath) throws FileNotFoundException {
- if (rubyObjectCache.get(fullPath) == null) {
- return evalScript(fullPath);
- }
- return rubyObjectCache.get(fullPath);
- }
- /**
- * 执行脚本,返回脚本对象 把这个方法从
- * #getRubyObject分出来,并且加上synchronized关键字,纯粹是为了防止多并发重复eval脚本
- *
- * @param fullPath
- * @return
- * @throws FileNotFoundException
- */
- private synchronized IRubyObject evalScript(final String fullPath) throws FileNotFoundException {
- if (rubyObjectCache.get(fullPath) == null) {
- File scriptFile = new File(fullPath);
- InputStream in = new FileInputStream(scriptFile);
- IRubyObject rubyObject = evaler.parse(runtime, in, scriptFile.getAbsolutePath(), 1).run();
- rubyObjectCache.put(fullPath, rubyObject);
- return rubyObject;
- }
- return rubyObjectCache.get(fullPath);
- }
- /**
- * 加载脚本
- *
- * @param fullPath
- * @throws FileNotFoundException
- */
- public void load(final String fullPath) throws FileNotFoundException {
- getRubyObject(fullPath);
- }
- /**
- * 清空已加载脚本对象
- *
- * @param fullPath
- */
- public void clean(final String fullPath) {
- if (rubyObjectCache.get(fullPath) != null) {
- rubyObjectCache.remove(fullPath);
- }
- }
- /**
- * 定义全局变量
- *
- * @param name
- * 变量名,不用以$开头
- * @param value
- * 值
- */
- public void defineGlobalVariable(final String name, final Object value) {
- IRubyObject rubyObject = JavaEmbedUtils.javaToRuby(runtime, value);
- /**
- * 这个全局变量的定义有点儿诡异,源代码是这样定义的:globalVariables.define(variable.name(),
- * newIAccessor() {}),所以必须手工加上 $ 开关
- **/
- GlobalVariable variable = new GlobalVariable(runtime, name.startsWith("$") ? name : "$" + name, rubyObject);
- runtime.defineVariable(variable);
- }
- /**
- * 执行脚本中定义的class的方法
- *
- * @param fullPath
- * 脚本绝对路径
- * @param method
- * 方法名
- * @param args
- * 参数
- * @return
- * @throws FileNotFoundException
- */
- public Object invokeMethod(final String fullPath, final String method, Object[] args) throws FileNotFoundException {
- IRubyObject rubyObject = getRubyObject(fullPath);
- return JavaEmbedUtils.invokeMethod(runtime, rubyObject, method, args, Object.class);
- }
- }
- /**
- *
- */
- package org.opensource.script.jruby;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.InputStream;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.Map;
- import org.jruby.Ruby;
- import org.jruby.RubyRuntimeAdapter;
- import org.jruby.javasupport.JavaEmbedUtils;
- import org.jruby.runtime.GlobalVariable;
- import org.jruby.runtime.builtin.IRubyObject;
- /**
- * 山寨版JRubyScriptEngine
- * 弃用官方版的原因是由于它封装的invokeMethod方法使用了synchronized关键字,在多并发的情况下性能极差.
- *
- * @author yanghuan
- *
- */
- public class JRubyScriptEngine {
- private final Ruby runtime;
- private final RubyRuntimeAdapter evaler;
- private final Map<String, IRubyObject> rubyObjectCache = new HashMap<String, IRubyObject>();
- public JRubyScriptEngine() {
- ArrayList<String> loadPaths = new ArrayList<String>();
- loadPaths.add("/root/.jruby/lib/ruby/1.8");
- loadPaths.add("/root/.jruby/lib/ruby/site_ruby/1.8");
- runtime = JavaEmbedUtils.initialize(loadPaths, JavaEmbedUtils
- .createClassCache(this.getClass().getClassLoader()));
- evaler = JavaEmbedUtils.newRuntimeAdapter();
- }
- /**
- * 根据脚本的路径,获取脚本eval后的Ruby对象,首先会从cache中检索,如有即时返回,如果没有则eval脚本,再返回.保证不会重复eval
- *
- * @param fullPath
- * 绝对路径
- * @return
- * @throws FileNotFoundException
- * @throws Exception
- */
- private IRubyObject getRubyObject(final String fullPath) throws FileNotFoundException {
- if (rubyObjectCache.get(fullPath) == null) {
- return evalScript(fullPath);
- }
- return rubyObjectCache.get(fullPath);
- }
- /**
- * 执行脚本,返回脚本对象 把这个方法从
- * #getRubyObject分出来,并且加上synchronized关键字,纯粹是为了防止多并发重复eval脚本
- *
- * @param fullPath
- * @return
- * @throws FileNotFoundException
- */
- private synchronized IRubyObject evalScript(final String fullPath) throws FileNotFoundException {
- if (rubyObjectCache.get(fullPath) == null) {
- File scriptFile = new File(fullPath);
- InputStream in = new FileInputStream(scriptFile);
- IRubyObject rubyObject = evaler.parse(runtime, in, scriptFile.getAbsolutePath(), 1).run();
- rubyObjectCache.put(fullPath, rubyObject);
- return rubyObject;
- }
- return rubyObjectCache.get(fullPath);
- }
- /**
- * 加载脚本
- *
- * @param fullPath
- * @throws FileNotFoundException
- */
- public void load(final String fullPath) throws FileNotFoundException {
- getRubyObject(fullPath);
- }
- /**
- * 清空已加载脚本对象
- *
- * @param fullPath
- */
- public void clean(final String fullPath) {
- if (rubyObjectCache.get(fullPath) != null) {
- rubyObjectCache.remove(fullPath);
- }
- }
- /**
- * 定义全局变量
- *
- * @param name
- * 变量名,不用以$开头
- * @param value
- * 值
- */
- public void defineGlobalVariable(final String name, final Object value) {
- IRubyObject rubyObject = JavaEmbedUtils.javaToRuby(runtime, value);
- /**
- * 这个全局变量的定义有点儿诡异,源代码是这样定义的:globalVariables.define(variable.name(),
- * newIAccessor() {}),所以必须手工加上 $ 开关
- **/
- GlobalVariable variable = new GlobalVariable(runtime, name.startsWith("$") ? name : "$" + name, rubyObject);
- runtime.defineVariable(variable);
- }
- /**
- * 执行脚本中定义的class的方法
- *
- * @param fullPath
- * 脚本绝对路径
- * @param method
- * 方法名
- * @param args
- * 参数
- * @return
- * @throws FileNotFoundException
- */
- public Object invokeMethod(final String fullPath, final String method, Object[] args) throws FileNotFoundException {
- IRubyObject rubyObject = getRubyObject(fullPath);
- return JavaEmbedUtils.invokeMethod(runtime, rubyObject, method, args, Object.class);
- }
- }
- JRuby使用经验
- JRuby使用经验
- 在Rational Function Tester中使用jruby
- JRuby Basics(JRuby基础)
- 使用 JRuby 和 Swing 进行跨平台开发
- 使用 JRuby 和 Swing 进行跨平台开发
- 使用JRuby和Swing进行跨平台开发
- Windows下JRuby on Rails搭建项目&使用Tomcat部署
- JRuby Cookbook
- JRuby是什么
- 走近JRuby
- 什么是JRuby
- JRuby安装
- 使用经验
- JRuby 1.7 Preview 1发布:默认使用Ruby 1.9,通过Invokedynamic改进了性能
- JRuby 1.7.0 已发布:默认为 Ruby 1.9 模式, 能够使用 InvokeDynamic
- JRuby 1.0 发布
- 咱也玩玩JRuby
- gcc编译静态库和动态库
- 郭美美疑云未散 总参一姐迷雾又起
- 图片加水印
- C++继承与派生
- android面试题
- JRuby使用经验
- scrollview的属性和用法
- POSIX 线程详解,第 3 部分
- 页面自动跳转与读取xml文件
- VIM使用小结
- xml解析进一步学习
- Git 安装和部署
- cocos2d-x 服务器时间转本地时间
- 无线定位算法-TDOA