Guest User

Untitled

a guest
May 19th, 2018
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.33 KB | None | 0 0
  1.  
  2. /*
  3. * JMS Bean which invokes a Ruby Script
  4. */
  5.  
  6.  
  7.  
  8. package com.vocel.ruby.client;
  9.  
  10. import java.io.IOException;
  11. import java.io.InputStream;
  12. import java.io.InputStreamReader;
  13. import java.io.Reader;
  14. import javax.ejb.ActivationConfigProperty;
  15. import javax.ejb.MessageDriven;
  16. import javax.jms.Message;
  17. import javax.jms.TextMessage;
  18. import javax.jms.MessageListener;
  19.  
  20.  
  21. import javax.script.ScriptContext;
  22. import javax.script.ScriptEngine;
  23. import javax.script.ScriptEngineManager;
  24.  
  25. /**
  26. *
  27. * @author bstewart
  28. */
  29. @MessageDriven(mappedName = "firstJMSQ", activationConfig = {
  30. @ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge"),
  31. @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue")
  32. })
  33. public class TestClientMDBBean implements MessageListener {
  34.  
  35. public TestClientMDBBean() {
  36. }
  37.  
  38. public void onMessage(Message message) {
  39.  
  40. InputStream iStream = null;
  41. try {
  42. String msg = ((TextMessage) message).getText();
  43. System.out.println("This MDB is working : " + msg);
  44.  
  45. String jruby_home = "/opt/jruby-1.1.2";
  46. System.out.println("Set System Properties ");
  47. System.setProperty("jruby.home", jruby_home);
  48. System.setProperty("jruby.base", jruby_home);
  49. System.setProperty("jruby.lib", jruby_home + "/lib");
  50. System.setProperty("jruby.script", "jruby");
  51. System.setProperty("jruby.shell", "/bin/sh");
  52.  
  53. System.out.println("Going to Create RUby Script");
  54. ClassLoader loader = getClass().getClassLoader();
  55. iStream = loader.getResourceAsStream("com/vocel/ruby/app.rb");
  56. Reader reader = new InputStreamReader(iStream, "UTF-8");
  57.  
  58. System.out.println("Going to eval reader");
  59. ScriptEngineManager m = new ScriptEngineManager();
  60. ScriptEngine rubyEngine = m.getEngineByName("jruby");
  61. if (rubyEngine != null) {
  62. rubyEngine.getContext().setAttribute("label", new Integer(4), ScriptContext.ENGINE_SCOPE);
  63. rubyEngine.eval(reader);
  64.  
  65. } else {
  66. System.out.println("Could not instantiate JRUBY script engine");
  67. }
  68. } catch (Exception e) {
  69. if (iStream != null){
  70. try {
  71. iStream.close();
  72. }
  73. catch(IOException io){
  74. io.printStackTrace();
  75. }
  76. }
  77. e.printStackTrace();
  78. }
  79. finally{
  80.  
  81. if (iStream != null){
  82. try {
  83. iStream.close();
  84. }
  85. catch(IOException io){
  86. io.printStackTrace();
  87. }
  88. }
  89. }
  90.  
  91. }
  92. }
  93.  
  94.  
  95.  
  96.  
  97. /*
  98. * Ruby Script
  99. */
  100.  
  101. require 'rubygems'
  102. require 'active_record'
  103. require 'active_record/connection_adapters/jdbc_adapter'
  104.  
  105. ActiveRecord::Base.establish_connection(
  106. :adapter => 'jdbc',
  107. :driver => 'com.mysql.jdbc.Driver',
  108. :url => 'jdbc:mysql://localhost/test_client_development',
  109. :username => 'root'
  110. )
  111.  
  112. class Subscriber < ActiveRecord::Base
  113. end
  114.  
  115. p "Find All Subscribers"
  116.  
  117. subs = Subscriber.find(:all)
  118.  
  119. subs.each do |sub|
  120. puts "Subscriber: #{sub.id}"
  121. end
Add Comment
Please, Sign In to add comment