Skip to content

Instantly share code, notes, and snippets.

@rosenfeld
Created April 4, 2012 20:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rosenfeld/2305431 to your computer and use it in GitHub Desktop.
Save rosenfeld/2305431 to your computer and use it in GitHub Desktop.
Demonstrate how to use Drb to integrate to Java through Ruby and JRuby
# ruby client.rb SheetName > output.xls
require 'drb/drb'
SERVER_URI="druby://localhost:8787"
DRb.start_service
service = DRbObject.new_with_uri(SERVER_URI)
puts service.get_excel_output('sheet_name' => ARGV.first || 'Test')
# run with JRuby
require 'java'
require_relative 'lib/poi-3.7.jar'
import java.io.ByteArrayOutputStream
import org.apache.poi.hssf.usermodel.HSSFWorkbook
class ExcelExporter
def initialize(data)
@wb = HSSFWorkbook.new
@sheet = @wb.create_sheet(data['sheet_name'])
end
def out
os = ByteArrayOutputStream.new
@wb.write os
String.from_java_bytes(os.to_byte_array)
end
end
require 'drb/drb'
require_relative 'excel_exporter'
URI="druby://localhost:8787"
class Service
def get_excel_output(data)
ExcelExporter.new(data).out
end
end
DRb.start_service URI, Service.new
puts "Listening on #{URI}"
# Wait for the drb server thread to finish before exiting.
DRb.thread.join
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment