Standing On The Shoulders of Giants With JRuby
Standing On The Shoulders of Giants With JRuby
Standing On The Shoulders of Giants With JRuby
mndag 19 mars 12
Theo @iconara
mndag 19 mars 12
Chief Architect at
mndag 19 mars 12
mndag 19 mars 12
ruby
mndag 19 mars 12
TL;DR
Most of you are Java developers Writing Java is tedious Theres truckloads of great Java libraries The JVM is awesome Ruby is awesome JRuby FTW
mndag 19 mars 12
just
mndag 19 mars 12
mndag 19 mars 12
mndag 19 mars 12
mndag 19 mars 12
mndag 19 mars 12
JRuby Java
stuff = TreeMap.new stuff['windmill'] = 'rocket' stuff['pirate'] = 'bees' stuff.each do |something| # redacted end
mndag 19 mars 12
JRuby Java
require 'java' require 'rabbitmq-client.jar' import 'com.rabbitmq.client.ConnectionFactory' factory = ConnectionFactory.new() factory.setUri('amqp://localhost:5672/') connection = factory.newConnection()
mndag 19 mars 12
JRuby Java
require 'java' require 'rabbitmq-client.jar' import 'com.rabbitmq.client.ConnectionFactory' factory = ConnectionFactory.new factory.uri = 'amqp://localhost:5672/' connection = factory.new_connection
mndag 19 mars 12
JRuby Java
class Worker < Thread def run puts 'Hard work, no play' end end # or class Worker include Runnable def run puts 'Hard work, no play' end end
mndag 19 mars 12
JRuby Java
pool = Executors.new_fixed_thread_pool(3) memes = LinkedBlockingQueue.new 10.times do pool.submit do open('http://api.autome.me/text').read.each_line do |meme| memes << meme end end end pool.shutdown pool.await_termination(3, TimeUnit::DAYS) memes.each { |m| puts(m) }
mndag 19 mars 12
mndag 19 mars 12
Made up fact
84% of Java devs dont write tests because its way too much extra code to type.
mndag 19 mars 12
Testing
public class Person { public final String firstName; public final String lastName; public Person(String firstName, String lastName) { this.firstName = firstName; this.lastName = lastName; } public String getFullName() { return String.format("%s %s", firstName, lastName); } // omg Im already bored }
mndag 19 mars 12
Testing
class TestPerson < Test::Unit::TestCase def setup @person = Person.new('James', 'Gosling') end def test_full_name assert_equal('James Gosling', @person.full_name) end end # this is TestUnit, its part of the stdlib
mndag 19 mars 12
Testing
describe Person do describe '#full_name' do before do @person = Person.new('James', 'Gosling') end it 'is the first name and the last name with a space in-between' do @person.full_name.should == 'James Gosling' end end end # this is RSpec, read more at rspec.info
mndag 19 mars 12
Made up fact
Ant was designed by the same people who came up with the QWERTY layout.
mndag 19 mars 12
Automation
<?xml version="1.0"?> <project name="MyProject" default="dist" basedir="."> <description> simple example build file </description> <!-- set global properties for this build --> <property name="src" location="src"/> <property name="build" location="build"/> <property name="dist" location="dist"/> <target name="init"> <!-- Create the time stamp --> <tstamp/> <!-- Create the build directory structure used by compile --> <mkdir dir="${build}"/> </target> <target name="compile" depends="init" description="compile the source "> <!-- Compile the java code from ${src} into ${build} --> <javac srcdir="${src}" destdir="${build}"/> </target> <target name="dist" depends="compile" description="generate the distribution"> <!-- Create the distribution directory --> <mkdir dir="${dist}/lib"/> <!-- And dont get me started on Maven, $%&@*! --> <!-- Put everything in ${build} into the MyProject-${DSTAMP}.jar file --> mndag 19 mars 12
Automation
require 'ant' src_dir = 'src' build_dir = 'build' dist_dir = 'dist' timestamp = Time.now task :init do mkdir_p build_dir end task :compile => :init do ant.javac :srcdir => src_dir, :destdir => build_dir end task :dist => :compile do mkdir_p "#{dist_dir}/lib" ant.jar :jarfile => "#{dist_dir}/lib/MyProject-#{timestamp}.jar", :basedir => build_dir end task :clean do rm_rf build_dir rm_rf dist_dir mndag 19 mars 12
Made up fact
The average number of lines in a Java web application is somewhere around 100 000
mndag 19 mars 12
mndag 19 mars 12
mndag 19 mars 12
Make a console
$ hbase shell HBase Shell; enter 'help<RETURN>' for list of supported commands. Type "exit<RETURN>" to leave the HBase Shell Version 0.90.4-cdh3u2, r, Thu Oct 13 20:32:26 PDT 2011 hbase(main):001:0> 0 row(s) in 1.2200 hbase(main):002:0> 0 row(s) in 0.0560 hbase(main):003:0> 0 row(s) in 0.0370 create 'test', 'cf' seconds put 'test', 'row1', 'cf:a', 'value1' seconds put 'test', 'row2', 'cf:b', 'value2' seconds
mndag 19 mars 12
Monitoring
require 'jmx' client = JMX.connect(:port => 7199) storage_service = client['org.apache.cassandra.db:type=StorageService'] storage_service.keyspaces.each do |keyspace| puts keyspace end memory_mbean = client['java.lang:type=Memory'] puts memory_mbean.heap_memory_usage.used memory_mbean.gc
mndag 19 mars 12
Configuration
conf = configuration do base_keys :api_key, :date dimension dimension dimension dimension :path :section :country :section, :country
metric :pageviews metric :reach, :user_id, :type => :unique metric :clicks, :click?, :type => :predicate end counters = conf.build!
mndag 19 mars 12
mndag 19 mars 12
mndag 19 mars 12
mndag 19 mars 12
mndag 19 mars 12
mndag 19 mars 12
mndag 19 mars 12
mndag 19 mars 12
mndag 19 mars 12
vs.
columns = {'name' => 'Dan', 'age' => 33} column_family.update(row_key, columns, :consistency_level => :one)
mndag 19 mars 12
mndag 19 mars 12
mndag 19 mars 12
mndag 19 mars 12
mndag 19 mars 12