Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * JMS Bean which invokes a Ruby Script
- */
- package com.vocel.ruby.client;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.io.Reader;
- import javax.ejb.ActivationConfigProperty;
- import javax.ejb.MessageDriven;
- import javax.jms.Message;
- import javax.jms.TextMessage;
- import javax.jms.MessageListener;
- import javax.script.ScriptContext;
- import javax.script.ScriptEngine;
- import javax.script.ScriptEngineManager;
- /**
- *
- * @author bstewart
- */
- @MessageDriven(mappedName = "firstJMSQ", activationConfig = {
- @ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge"),
- @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue")
- })
- public class TestClientMDBBean implements MessageListener {
- public TestClientMDBBean() {
- }
- public void onMessage(Message message) {
- InputStream iStream = null;
- try {
- String msg = ((TextMessage) message).getText();
- System.out.println("This MDB is working : " + msg);
- String jruby_home = "/opt/jruby-1.1.2";
- System.out.println("Set System Properties ");
- System.setProperty("jruby.home", jruby_home);
- System.setProperty("jruby.base", jruby_home);
- System.setProperty("jruby.lib", jruby_home + "/lib");
- System.setProperty("jruby.script", "jruby");
- System.setProperty("jruby.shell", "/bin/sh");
- System.out.println("Going to Create RUby Script");
- ClassLoader loader = getClass().getClassLoader();
- iStream = loader.getResourceAsStream("com/vocel/ruby/app.rb");
- Reader reader = new InputStreamReader(iStream, "UTF-8");
- System.out.println("Going to eval reader");
- ScriptEngineManager m = new ScriptEngineManager();
- ScriptEngine rubyEngine = m.getEngineByName("jruby");
- if (rubyEngine != null) {
- rubyEngine.getContext().setAttribute("label", new Integer(4), ScriptContext.ENGINE_SCOPE);
- rubyEngine.eval(reader);
- } else {
- System.out.println("Could not instantiate JRUBY script engine");
- }
- } catch (Exception e) {
- if (iStream != null){
- try {
- iStream.close();
- }
- catch(IOException io){
- io.printStackTrace();
- }
- }
- e.printStackTrace();
- }
- finally{
- if (iStream != null){
- try {
- iStream.close();
- }
- catch(IOException io){
- io.printStackTrace();
- }
- }
- }
- }
- }
- /*
- * Ruby Script
- */
- require 'rubygems'
- require 'active_record'
- require 'active_record/connection_adapters/jdbc_adapter'
- ActiveRecord::Base.establish_connection(
- :adapter => 'jdbc',
- :driver => 'com.mysql.jdbc.Driver',
- :url => 'jdbc:mysql://localhost/test_client_development',
- :username => 'root'
- )
- class Subscriber < ActiveRecord::Base
- end
- p "Find All Subscribers"
- subs = Subscriber.find(:all)
- subs.each do |sub|
- puts "Subscriber: #{sub.id}"
- end
Add Comment
Please, Sign In to add comment