Created
April 4, 2012 20:40
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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