Slide 1

Slide 1 text

JRebel  Under  the  Covers   How  is  it  even  possible?  

Slide 2

Slide 2 text

Simon  Maple  -­‐  @sjmaple   @sjmaple Simon Maple

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

Make a change Compile Build Deploy Observe the result Take some coffee Avg: 3 min

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

state external serializable temporary derivative

Slide 8

Slide 8 text

How can I reload a single class preserving the state?

Slide 9

Slide 9 text

ClassLoader API

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

How to reload web application? (and preserve state)

Slide 12

Slide 12 text

Classes Libraries OldClassLoader NewClassLoader Sevlet New Classes New Libraries Sevlet Session Session init() App State App State Serialize/deserialize

Slide 13

Slide 13 text

OldClassLoader NewClassLoader Sevlet New Classes New Libraries Sevlet Session Session App State App State

Slide 14

Slide 14 text

Class loaders are good for isolation, but suck at reloading the code … (and perfect for memory leaks)

Slide 15

Slide 15 text

No content

Slide 16

Slide 16 text

Leaking ClassLoaders

Slide 17

Slide 17 text

Hello, HotSwap! Hotswapping methods since 2002

Slide 18

Slide 18 text

M. Dmitriev. Safe class and data evolution in large and long-lived java[tm] applications. Technical report, Mountain View, CA, 2001.

Slide 19

Slide 19 text

The  Heap  &  Garbage  CollecBon  

Slide 20

Slide 20 text

Hello, JRebel! Improving HotSwap since 2007

Slide 21

Slide 21 text

Demo Time!

Slide 22

Slide 22 text

HotSwap   JRebel   Changing  method  bodies   Adding/removing  methods   Adding/removing  constructors   Adding/removing  fields   Adding/removing  annotaBons   Changing  superclass   Adding/removing  interfaces  

Slide 23

Slide 23 text

How does it work???

Slide 24

Slide 24 text

The engine

Slide 25

Slide 25 text

ClassA +field1 +field2 +field3 +method1(n:int) +method2(s:String) +method3(z:double) ClassA +field1 +field2 +field3 +proxy methods ClassA0 +method1(n:int) +method2(s:String) +method3(z:double) Transformer

Slide 26

Slide 26 text

public class C extends X { int y = 5; int method1(int x) { return x + y; } void method2(String s) { System.out.println(s); } }

Slide 27

Slide 27 text

public class C extends X { int y = 5; int method1(int x) { Object[] o = new Object[1]; o[0] = x; return Runtime.redirect(this, o, "C", "method1", "(I)I"); } void method2(String s) { Object[] o = new Object[1]; o[0] = s; return Runtime.redirect( this, o, "C", "method2", "(Ljava/lang/String;)V"); } }

Slide 28

Slide 28 text

public abstract class C0 { public static int method1(C c, int x) { int tmp1 = Runtime.getFieldValue(c, "C", "y", "I"); return x + tmp1; } public static void method2(C c, String s) { PrintStream tmp1 = Runtime.getFieldValue( null, "java/lang/System", "out", "Ljava/io/PrintStream;"); Object[] o = new Object[1]; o[0] = s; Runtime.redirect( tmp1, o, "java/io/PrintStream;", "println", "(Ljava/lang/String;)V"); } }

Slide 29

Slide 29 text

public class C extends X { int y = 5; int method1(int x) { return x + y; } //... } public class C extends X { int y = 5; int z() { return 10; } int method1(int x) { return x + y + z(); } //... }

Slide 30

Slide 30 text

public class C1 { public static int z(C c) { return 10; } public static int method1(C c, int x) { int tmp1 = Runtime.getFieldValue(c, "C", "y", "I"); int tmp2 = Runtime.redirect(c, null, "C", "z", "(V)I"); return x + tmp1 + tmp2; } //... }

Slide 31

Slide 31 text

JRebel Reloading

Slide 32

Slide 32 text

No content

Slide 33

Slide 33 text

No content

Slide 34

Slide 34 text

static { } Adding new static fields Removing static fields Changing values of static fields Factories etc

Slide 35

Slide 35 text

package a; public class A { int say() {return 1;} } package a; class Test { public static void main(String args[]) { a.A a = new b.B(); System.out.println(a.say()); } } package b; public class B extends a.A { int say() {return 2;} } An Exercise

Slide 36

Slide 36 text

“package” visibility package -> public -> package

Slide 37

Slide 37 text

concurrency synchronized volatile Object.wait() Object.notify()

Slide 38

Slide 38 text

serialization Serializable Externalizable transient

Slide 39

Slide 39 text

reflection Class, Method, Field, Constructor, Proxy

Slide 40

Slide 40 text

No content

Slide 41

Slide 41 text

The Ecosystem

Slide 42

Slide 42 text

Javassist: binary patching for fun and profit

Slide 43

Slide 43 text

import java.lang.instrument.ClassFileTransformer; import java.lang.instrument.Instrumentation; public class Agent { public static void premain(String args, Instrumentation inst) throws Exception { inst.addTransformer(new ClassFileTransformer { }); } } java  –javaagent:agent.jar  …   META-INF/MANIFEST.MF Premain-Class: Agent

Slide 44

Slide 44 text

new ClassFileTransformer() { public byte[] transform(ClassLoader loader, String className, Class>classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer){ ClassPool cp = ClassPool.getDefault(); CtClass ct = cp.makeClass(new ByteArrayInputStream(classfileBuffer)); transformClass(ct, cp); return ct.toBytecode(); } }

Slide 45

Slide 45 text

new ClassFileTransformer() { public byte[] transform(ClassLoader loader, String className, Class>classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer){ ClassPool cp = ClassPool.getDefault(); CtClass ct = cp.makeClass(new ByteArrayInputStream(classfileBuffer)); transformClass(ct, cp); return ct.toBytecode(); } }

Slide 46

Slide 46 text

JRebel Plugins

Slide 47

Slide 47 text

@Path(“/”) public String foo() { return “Foo”; } @Path(“/foobar”) public String foo() { return “FooBar”; } JRebel   core   JRebel   Spring   plugin  

Slide 48

Slide 48 text

Remote?

Slide 49

Slide 49 text

THE IMPACT

Slide 50

Slide 50 text

http://zeroturnaround.com/company/case-studies/ “Team velocity was increased by 40.6%, and according to t-test it was a statistically significant difference.” “Netty and Spring together with JRebel is real pleasure to work with. The value of time saved is over 50% of my development time.”

Slide 51

Slide 51 text

Questions? @sjmaple

Slide 52

Slide 52 text

No content

Slide 53

Slide 53 text

No content

Slide 54

Slide 54 text

Credits https://www.flickr.com/photos/smemon/4556099850/ https://www.flickr.com/photos/gforsythe/10173857405 https://www.flickr.com/photos/dkshots/6967173295 https://www.flickr.com/photos/reckless_sniper/4545673070 https://www.flickr.com/photos/omcoc/8350510425 https://www.flickr.com/photos/bufivla/3619927485 https://www.flickr.com/photos/39747297@N05/5229733647 http://leadershipelements.files.wordpress.com/2014/01/square-peg-round-hole.jpg