Real-world polyglot programming on the JVM - Ben Summers (ONEIS)

579
-1

Published on

Presented at JAX London 2013

Java is not the only language! The JVM is a wonderful common runtime for a wide variety of languages. By using the right language for each task, you can deliver higher quality applications in less time.

Published in: Technology, News & Politics
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
579
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
10
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Real-world polyglot programming on the JVM - Ben Summers (ONEIS)

  1. 1. Ben Summers | ONEIS Real-world polyglot programming on the JVM
  2. 2. Real-world polyglot programming on the JVM Be n Su m me r s Te c hnic a l d i re ctor, ON EIS @b e nsu mm ers
  3. 3. This is nothing new.
  4. 4. Actual language usage • Primary programming language • Con!guration + build system • Presentation • Miscellaneous scripting
  5. 5. Polyglot programming is a formalisation.
  6. 6. A polyglot application Ruby 40% Java 20% JavaScript 20% HTML, CSS, etc 18% C++ 1% Python 1%
  7. 7. Table of contents • How I became a polyglot programmer • The case for polyglot programming • Languages, JVM and polyglot apps • Impedance mismatch • Glue code • Language boundaries • Getting started
  8. 8. An accidental polyglot flickr: darynbarry
  9. 9. ONEIS architecture JavaScript Ruby Java Java Jetty Internet JVM Worker Worker Worker Worker JVM JVM JVM JVM Sandboxed JavaScript plugins
  10. 10. The case for polyglot flickr: dm-set
  11. 11. Languages are not equal • Expressiveness • Performance • Readability • Style • Static • Dynamic • Functional • WTF
  12. 12. The case for polyglot • “Real artists ship” • You’re doing it already • Formalisation and extension
  13. 13. Ola Bini’s pyramid http://olabini.com/blog/2008/06/fractal-programming/
  14. 14. Java JVM Rub y San dbo Java xed Scr plug ipt ins Ola Bini’s pyramid Java Ja vaS c ript
  15. 15. Why the JVM? • Great runtime • Portability • Common underlying VM for lots of languages • Interoperability • Java library for everything
  16. 16. Side note: Don’t forget the CLR • Good stu" from Microsoft • Great runtime • DLR • APIs • Di"erent culture • Trap?
  17. 17. Side note: Don’t forget IPC • Go old school • Separate processes • Common protocol over sockets and pipes • Thrift, Google Protocol Bu"ers, Avro • or text over HTTP: XML, JSON, etc
  18. 18. Languages and the JVM flickr: munksynz
  19. 19. What’s the best language? • No such thing as the “best” language • Use the best language for solving your problems • Polyglot is about avoiding compromises
  20. 20. Language choice • Don’t use two closely related languages • Don’t forget the standard library • And some have special features eg. concurrency libraries
  21. 21. Pick one from each column Static Dynamic Scripting / DSLs Java Ruby JavaScript Scala Groovy Ruby Clojure Groovy Jython
  22. 22. Aims • Work in harmony • Choose most appropriate language for each task • Choose carefully as choices are hard to undo
  23. 23. Memory usage • Multiple runtimes • Multiple standard libraries • Cloud is memory constrained
  24. 24. Impedance mismatch flickr: h080
  25. 25. Impedance mismatch • Type systems • Your languages won’t agree on the simplest things: • Strings, Numbers, Dates, Arrays, null
  26. 26. Strings • Java: UTF-16 (ish) strings • JavaScript: implementation dependent • Browsers: UCS-16 usually • JVM: Whatever Java does + optimisations • Ruby • bytes (1.8), or bytes+encoding (1.9) • JVM originated languages: Java Strings
  27. 27. Numbers • Java: primitives, java.lang.Number, etc • JavaScript: doubles only. Plus odd Number object. • Ruby: • ints (always Fixnum) with silent promotion to Bignum • doubles, Rational
  28. 28. null • Java: null • Ruby: nil • JVM languages: null • JavaScript: null and undefined
  29. 29. Javascript’s null and undefined var o = {item: null}; if(o.item === undefined) { console.log("0"); } if(o.noSuchItem === undefined) { console.log("1"); } OUTPUT 1
  30. 30. Javascript’s null and undefined var o = {item: null}; if(o.item === undefined) { console.log("0"); } if(o.noSuchItem === undefined) { console.log("1"); } var undefined = 42; if(o.noSuchItem === undefined) { console.log("2"); } OUTPUT 1
  31. 31. JavaScript Top Tip Use JSHint in your tests (but not JSLint)
  32. 32. Strings, numbers, dates... • Impedance mismatch means you have to think about the boundaries • Use Groovy?
  33. 33. Glue code flickr: samcatchesides
  34. 34. Glue code • Work around impedance mismatch • Java: The new assembly language • Rule enforcement & security • Cheat until your pro!ler says it’s slow • Serialise/deserialise to common format, eg JSON
  35. 35. undefined is not null JAVA public class Host extends ScriptableObject { public String jsFunction_f(String x) { System.out.println( "x = " + x ); } }
  36. 36. undefined is not null JAVA public class Host extends ScriptableObject { public String jsFunction_f(String x) { System.out.println( "x = " + x ); } } JAVASCRIPT var h = new Host(); h.f("Hello"); h.f(2); h.f(null); h.f(undefined);
  37. 37. undefined is not null JAVA public class Host extends ScriptableObject { public String VALUES OF X jsFunction_f(String x) { System.out.println( "x = " + x"Hello" ); "2" } null } "undefined" JAVASCRIPT var h = new Host(); h.f("Hello"); h.f(2); h.f(null); h.f(undefined);
  38. 38. null and undefined are 0 JAVA public void jsFunction_each( Integer desc, boolean hasDesc, Integer qual, boolean hasQual) JAVASCRIPT Store.prototype.each = function(desc, qual) { return this.$store.each( desc, (desc !== null && desc !== undefined), qual, (qual !== null && qual !== undefined) ); };
  39. 39. Conversions public static Date tryConvertJsDate(Object jsObject) { if(jsObject == null) { return null;} if(nativeDateClass == null) { try { nativeDateClass = Class.forName("org.mozilla.javascript.NativeDate"); } catch(ClassNotFoundException e) { /* ... */ } } if(nativeDateClass.isInstance(jsObject)) { Object d = Context.jsToJava(jsObject, Date.class); if(d != null && d instanceof Date) { return (Date)d; } } } return null;
  40. 40. Calling Java is easy • Don’t let JavaScript put you o" • Calling Java APIs from any other language is easy
  41. 41. Create a JPEG with Java import import import import java.awt.image.BufferedImage; java.awt.Graphics2D; javax.imageio.ImageIO; java.io.File; public class MakeImage { public BufferedImage makeImage(int w, int h) { BufferedImage image = new BufferedImage(w, h, TYPE_INT_RGB); Graphics2D graphics = image.createGraphics(); graphics.drawString("Hello world", w/2, h/2); graphics.dispose(); return image; } } public static void main(String args[]) throws Exception { BufferedImage img = (new MakeImage()).makeImage(200, 400); ImageIO.write(img, "jpeg", new File("test.jpeg")); }
  42. 42. Create a JPEG with JRuby require 'java' class MakeImage java_import java.awt.image.BufferedImage; java_import java.awt.Graphics2D; def make_image(w, h) image = BufferedImage.new(w, h, BufferedImage::TYPE_INT_RGB) graphics = image.create_graphics graphics.draw_string("Hello world", w/2, h/2) graphics.dispose image end end java_import javax.imageio.ImageIO; java_import java.io.File; img = MakeImage.new.make_image(200, 400) ImageIO.write(img, "jpeg", File.new("test.jpeg"))
  43. 43. The artwork
  44. 44. Divided by a common language • JRuby: Transparent bridging • JavaScript: Host objects + bridging • Groovy: Java is subset of Groovy • Scala: Write Java with Scala syntax
  45. 45. JSR-223 • Java Scripting API • Common API for scripting languages on the JVM • Scripting “engines” available for many languages, variable quality • Su"ers from impedance mismatch itself • Functions may return unexpected types
  46. 46. Language boundaries flickr: dinomite
  47. 47. Ola Bini’s pyramid http://olabini.com/blog/2008/06/fractal-programming/
  48. 48. Boundaries • Which language for each module? • Clear boundaries, not a free-for-all. • Well de!ned (and documented) interfaces
  49. 49. Java to Ruby public interface Framework { void startApplication(); void startBackgroundTasks(); void stopApplication(); } Response handleFromJava( HttpServletRequest request, Application app, byte[] body, boolean isRequestSSL);
  50. 50. Ruby implementation class Framework def start_application # ... end def handle_from_java( servlet_request, application, body, is_ssl) # magic goes here end end
  51. 51. JavaScript to Ruby (via Java) public interface AppWorkUnit { public int id(); public public public public } Integer actionableById(); void setActionableById(Integer id); String jsGetDataRaw(); void jsSetDataRaw(String data); public boolean save(); public void destroy();
  52. 52. Security • Trust models • Sandboxing interpreters vs sandboxed interpreters
  53. 53. Building the team flickr: AaronLMGoodwin
  54. 54. Hiring polyglots
  55. 55. Hiring polyglots developers
  56. 56. Hiring polyglots developers • Find people who can write code, not people who can write Java. • Test to see if programming is instinctive, not syntax and trivia. • Look for people who continuously learn, not those who have learnt.
  57. 57. Finding a polyglot job • Smaller companies, or smaller teams within large companies • Early stage projects, not maintenance or enhancements • Talk to really good recruiters • Networking!
  58. 58. Getting started flickr: holyhoses
  59. 59. The Enterprise • It’s just Java the JVM • Implement a servlet • Use tools like Warbler • TorqueBox • New job outside the Enterprise
  60. 60. Starting points • Use JRuby to try out an API • Use Ruby testing libraries to test your Java • Use Groovy to a make business rules DSL • Write a layer of a small project in a dynamic language
  61. 61. Polyglot programming: Not new, but very e!ective.
  62. 62. Questions? Be n Su m me r s Te c hnic a l d i re ctor, ON EIS @b e nsu mm ers http://bens.me.u k onei s .co.uk / jo b s
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×