new taming ajax with gwt - software summit · 2008. 1. 3. · scott blum — taming ajax with gwt...
TRANSCRIPT
Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Google, Inc.
Scott Blum — Taming AJAX with GWT Page 1
Taming AJAX with GWTScott Blum
Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Google, Inc.
Scott Blum — Taming AJAX with GWT Page 2
OverviewWhy AJAX?Why GWT?How GWT WorksAdd GWT to your AppAdvanced TechniquesSummaryQ & A
Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Google, Inc.
Scott Blum — Taming AJAX with GWT Page 3
Demo – A Production AppAJAX Texas Hold’em
http://www.gpokr.com/
Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Google, Inc.
Scott Blum — Taming AJAX with GWT Page 4
AJAX: Easy on UsersNo installation
Every application is just a URL away
Secure… the mantra we teach our parentsinstalling things = mostly unsafesurfing the web = mostly safe
Web simplicityAJAX pages have the familiar web look and feelWeb simplicity – back, forward, links, URLs
Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Google, Inc.
Scott Blum — Taming AJAX with GWT Page 5
AJAX: Easy on ServersState maintained on client
Only send the data, not the presentationSend only deltas instead of entire UI stateClean separation of concernsAllows servers to be effectively stateless
Leverage user’s CPU and RAMDo everything you can on the clientMuch faster for the user, too!
Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Google, Inc.
Scott Blum — Taming AJAX with GWT Page 6
Traditional App Cycle
Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Google, Inc.
Scott Blum — Taming AJAX with GWT Page 7
AJAX App Cycle
Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Google, Inc.
Scott Blum — Taming AJAX with GWT Page 8
Summary and Q&AUse AJAXThe End
Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Google, Inc.
Scott Blum — Taming AJAX with GWT Page 9
OverviewWhy AJAX?Why GWT?How GWT WorksAdd GWT to your AppAdvanced TechniquesSummaryQ & A
Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Google, Inc.
Scott Blum — Taming AJAX with GWT Page 10
AJAX: Hard on DevelopersJavaScript is the “J” in AJAXNo strong type system
typos + expandos = bug-o-sImagine this gem on line 5912 of your script
x.compnent = document.getElementById("x");// a "spelling bug" that will bite much later
Poor IDE supportDebuggers… for some browsers…
Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Google, Inc.
Scott Blum — Taming AJAX with GWT Page 11
AJAX: Hard on DevelopersIf you support IE6 and FF you’re doing well!But then there’s
IE7Older MozillaeSafariOperaKonquerorYou name it
Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Google, Inc.
Scott Blum — Taming AJAX with GWT Page 12
AJAX: Hard on DevelopersNo installation… sort of
In a sense, it’s also always-reinstall (better be small)Modularizing JavaScript is really tricky
Secure… sort ofHard enough just to make AJAX work at all!Lots of JS = lots of attack surface
Has web simplicity… sort ofHistory, bookmarks and even hyperlinks misbehaveBadly coded AJAX is worse than traditional HTML
Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Google, Inc.
Scott Blum — Taming AJAX with GWT Page 13
AJAX: Hard on DevelopersLots of technologies to worry about (you need regexps to list them all on one page)
HTTPS?, [DX]?HTML (3.2|4.0), CSS[1-3]DOM Level[0-3](Java|ECMA|J|VB)Script(X|VR?|Math)MLSVG, Canvas, FlashJSON, SOAP, XML-RPC
Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Google, Inc.
Scott Blum — Taming AJAX with GWT Page 14
AJAX: Hard on DevelopersLots of JS libraries to choose from, but…
You often wind up paying for the whole library when you only need 5% of itInline comments and well-named identifiers add to code sizeThe compiler can’t tell you if your using the library wrongUpgrading to new versions?
Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Google, Inc.
Scott Blum — Taming AJAX with GWT Page 15
Solution: Use GWTWrite your AJAX code in JavaCompile it with a real Java compilerDebug it in a real Java IDEUnit test it with JUnitJavadoc it without script size worriesReuse it on a class or package basisShare it as a JAR
Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Google, Inc.
Scott Blum — Taming AJAX with GWT Page 16
Solution: Use GWTJava has a lot of advantages over JavaScript
Static type checkingGreat IDE support• Code completion• Quick correction• Lint, style, and other warnings• Refactoring
Established code patternsScales to large projects and large teams
Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Google, Inc.
Scott Blum — Taming AJAX with GWT Page 17
Solution: Use GWT
(+50 more slides about howJava whoops JavaScript)
Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Google, Inc.
Scott Blum — Taming AJAX with GWT Page 18
OverviewWhy AJAX?Why GWT?How GWT WorksAdd GWT to your AppAdvanced TechniquesSummaryQ & A
Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Google, Inc.
Scott Blum — Taming AJAX with GWT Page 19
How GWT WorksBuild your app in JavaDOM-based Widget frameworkDebug in a real Java IDECompile to JavaScriptDeploy compiled output as static contentBuilt-in RPC based on Servlets
Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Google, Inc.
Scott Blum — Taming AJAX with GWT Page 20
Code Sample – A Simple App<html><head><meta name='gwt:module'content='com.google.gwt.sample.hello.Hello'>
<title>Hello</title></head><body><script language="javascript" src="gwt.js"></script></body></html>
Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Google, Inc.
Scott Blum — Taming AJAX with GWT Page 21
Code Sample – A Simple Apppublic class Hello implements EntryPoint {
public void onModuleLoad() {Button b = new Button("Click me",new ClickListener() {public void onClick(Widget sender) {Window.alert("Hello, AJAX");
}});RootPanel.get().add(b);
}
}
Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Google, Inc.
Scott Blum — Taming AJAX with GWT Page 22
Demo – A Simple AppHello, AJAX
Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Google, Inc.
Scott Blum — Taming AJAX with GWT Page 23
How GWT WorksGWT
ToolsClient Server
Java to JavaScriptCompiler
Hosted Mode
Widget Framework
JRE Emulation
Browser Abstraction
SerializationFramework
Deferred Binding
Java Servlet
Other Servers
Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Google, Inc.
Scott Blum — Taming AJAX with GWT Page 24
How GWT Works
Client
Widget Framework
JRE Emulation
Browser Abstraction
Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Google, Inc.
Scott Blum — Taming AJAX with GWT Page 25
Client Frameworkgwt-user.jarOpen Source: Apache 2.0Browser abstraction layerFull set of UI Widgets, styled with CSS
Real HTML elementsEvent drivenLike Swing or SWT
Panels for managing layout
Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Google, Inc.
Scott Blum — Taming AJAX with GWT Page 26
Demo – Widgets and HistoryMail and KitchenSink
Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Google, Inc.
Scott Blum — Taming AJAX with GWT Page 27
Client FrameworkGrowing subset of the JRE
Much of java.lang, java.utilNo reflection (by design!)What makes sense on a browser• No threads, file i/o, etc.
RPC, XML, JSON, i18n, other acronyms
Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Google, Inc.
Scott Blum — Taming AJAX with GWT Page 28
How GWT Works
Server
Java Servlet
Serialization Framework
Other Servers
Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Google, Inc.
Scott Blum — Taming AJAX with GWT Page 29
GWT’s Built-in RPCgwt-servlet.jarOpen Source: Apache 2.0Servlets on the server side Explicit server calls
This is a good thing ☺
Declare a remote interfaceSend POJOs, primitives, collections
Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Google, Inc.
Scott Blum — Taming AJAX with GWT Page 30
Code Sample – RPC Interfaces// Synchronous interface (declared)public interface SpellingService extends RemoteService {String[] suggest(String wordToCheck);
}
// Asynchronous interface (derived)public interface SpellingServiceAsync {void suggest(String wordToCheck, AsyncCallback callback);
}
Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Google, Inc.
Scott Blum — Taming AJAX with GWT Page 31
Code Sample – RPC CallSpellServiceAsync spell = GWT.create(SpellService.class);
spell.suggest("compnent", new AsyncCallback() {
void onSuccess(Object result) {String[] alts = (String[])result;if (alts.length > 0)showSuggestions(alts);
}
void onFailure(Throwable e) {reportProblem(e);
}
});
Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Google, Inc.
Scott Blum — Taming AJAX with GWT Page 32
Code Sample – RPC Servlet// Servlet classpublic class SpellingServiceImpl
extends RemoteServiceServletimplements SpellingService {
public String[] suggest(String maybeMisspelledWord) {List list = new ArrayList();// populate list; run any server-side code you likereturn (String[]) list.toArray(new String[list.size()]);
}
}
Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Google, Inc.
Scott Blum — Taming AJAX with GWT Page 33
Demo – RPCDynaTable
Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Google, Inc.
Scott Blum — Taming AJAX with GWT Page 34
Network Usage
0
10000
20000
30000
40000
50000
60000
70000
80000
90000
Page 1 Page 2 Page 3 Page 4 Page 5 Page 6 Page 7
Byt
es T
rans
ferr
ed
Traditional HTML GWT First Run GWT Other Runs
Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Google, Inc.
Scott Blum — Taming AJAX with GWT Page 35
GWT’s Built-in RPCAlways asynchronousGenerated classes perform serializationUnpublished super-efficient wire formatLibraries support custom RPC
Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Google, Inc.
Scott Blum — Taming AJAX with GWT Page 36
How GWT Works
Tools
Java to JavaScriptCompiler
Hosted Mode
Deferred Binding
Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Google, Inc.
Scott Blum — Taming AJAX with GWT Page 37
GWT Toolsgwt-dev-(windows|linux|mac).jarFree; Currently Closed-SourceCompilerHosted Mode supportCommon Infrastructure
Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Google, Inc.
Scott Blum — Taming AJAX with GWT Page 38
GWT Tools – CompilerCompile Java into JavaScriptFull Java 1.4 language supportDeploy compiled scripts on any web serverOptimize like heck
Monolithic compileType TighteningAggressive Code Pruning
Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Google, Inc.
Scott Blum — Taming AJAX with GWT Page 39
Hello AJAX – Javapublic class Hello implements EntryPoint {
public void onModuleLoad() {Button b = new Button("Click me",new ClickListener() {public void onClick(Widget sender) {Window.alert("Hello, AJAX");
}});RootPanel.get().add(b);
}
}
Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Google, Inc.
Scott Blum — Taming AJAX with GWT Page 40
Hello AJAX – Generated Script// from class Hellofunction _$onModuleLoad(_this$static){var _b = _$Button(new _Button(), 'Click me',_$Hello$1(new _Hello$1(), _this$static));
_$add(_get(), _b);}
// from anonymous ClickListener classfunction _onClick(_sender){_alert('Hello, AJAX');
}
Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Google, Inc.
Scott Blum — Taming AJAX with GWT Page 41
Hello AJAX – Obfuscatedfunction ne(oe){var pe=qe(new re(),'Click me',se(new te(),oe));ue(ve(),pe);}
function xe(ye){ze('Hello, AJAX');}
Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Google, Inc.
Scott Blum — Taming AJAX with GWT Page 42
GWT Tools – Pay for Use
0
10000
20000
30000
40000
50000
60000
Empty
Wind
ow.al
ert("h
ello")
Vertica
lPanel
Button
Check
BoxTree
(3 ite
ms)
Labe
lTex
tBox
Grid (4
x 4)
HTML
Imag
e
TabPan
el (3
tabs)
ListB
ox (3
items)
Radio
Button
(3)
Hyperl
inkHori
zonta
lPanel
FlowPan
elAbs
oluteP
anel
Scrip
t Siz
e (b
ytes
)
Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Google, Inc.
Scott Blum — Taming AJAX with GWT Page 43
GWT Tools – Hosted ModeRun under a real 1.4 or 5.0 JVMRun inside the browser with full DOM accessFull debugging and IDE supportPlatform support:
Windows with IELinux with MozillaMac OS X with Safari may already be available
Embedded Tomcat serves files from your classpath for easy setup
Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Google, Inc.
Scott Blum — Taming AJAX with GWT Page 44
GWT Tools – Deferred BindingReplace classes at compile time
DOM dom = (DOM)GWT.create(DOM.class);
dom.doSomethingPlatformSpecific();
At runtime, dom actually refers to the most appropriate subclass
DOMInterface
InternetExplorerClass
MozillaClass
Application code
Deferred binding compiles in the correct subclass based on
selection rules.SafariClass
Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Google, Inc.
Scott Blum — Taming AJAX with GWT Page 45
GWT Tools – Deferred Binding
Single JavaCode Base
Download exactly what you need in a single
can't-go-wrong chunk
Cache it on the client until the sun explodes
…
FireFox 1.0.x
Your Code
en_US
1D04ADDA.cache.html
Safari 2.0.x
Your Code
fr_FR…
7EFE4D24.cache.html
IE 6
Your Code
en_UK…
15F361BB.cache.html
Opera 9
Your Code
fr_CA…
D415D917.cache.html
Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Google, Inc.
Scott Blum — Taming AJAX with GWT Page 46
GWT Tools – Deferred BindingWhy do at runtime what you can do at compile time?Because of type tightening, polymorphic calls can be inlinedGenerators provide an optimized alternative to runtime reflection
Our own RPC is built on it
Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Google, Inc.
Scott Blum — Taming AJAX with GWT Page 47
OverviewWhy AJAX?Why GWT?How GWT WorksAdd GWT to your AppAdvanced TechniquesSummaryQ & A
Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Google, Inc.
Scott Blum — Taming AJAX with GWT Page 48
Add GWT to Your AppAttach a <meta> tag to your HTMLInclude the gwt.js scriptAdd an id to elements you’re interested inDeploy compiler output as static contentWorks with any HTML-generation approach
Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Google, Inc.
Scott Blum — Taming AJAX with GWT Page 49
Demo – Adding GWTA Simple JSP Example
Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Google, Inc.
Scott Blum — Taming AJAX with GWT Page 50
OverviewWhy AJAX?Why GWT?How GWT WorksAdd GWT to your AppAdvanced TechniquesSummaryQ & A
Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Google, Inc.
Scott Blum — Taming AJAX with GWT Page 51
Advanced TechniquesEffective RPCHanging RPCJavaScript native interface (JSNI)
Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Google, Inc.
Scott Blum — Taming AJAX with GWT Page 52
Effective RPCCalls are Asynchronous only
Think event-driven
Limit the number of invocationsThink “chunky”, not “chatty”Ideally, no more than 1 call per user action
Limit to 2 outstanding calls at onceBrowser-imposed limit
Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Google, Inc.
Scott Blum — Taming AJAX with GWT Page 53
Hanging RPCImagine a chat room appNaïve solution:
Clients poll on timerMost calls return emptySlow perceived responseWasted processing/bandwidth
Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Google, Inc.
Scott Blum — Taming AJAX with GWT Page 54
Hanging RPCa.k.a. “server push” or “comet”
Not really possible with HTTP; this fakes it
Basic algorithmClient calls to check for eventsServer blocks until an event occursClient calls again to wait for next event
Pro: Less processing/bandwidth than pollingCon: Each client constantly uses a TCP connection and server thread; not as easy to throttle clients
Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Google, Inc.
Scott Blum — Taming AJAX with GWT Page 55
More Info on RPCRon Bodkin’s AJAX Performance and Monitoring
More depth on these topicsJetty continuations and hanging RPC
Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Google, Inc.
Scott Blum — Taming AJAX with GWT Page 56
JavaScript Native InterfaceHand-write JavaScript into your Java class
Like inline assembly in C
Foundation of GWT’s frameworkInteract with browser directlyCall an external script
Wrap existing JavaScript with type-safe Java signatures
Constructs JS supports better than JavaFunction pointer tableString-key map
Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Google, Inc.
Scott Blum — Taming AJAX with GWT Page 57
Code sample – JSNI
native void alert(String msg) /*-{alert(msg);
}-*/;
Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Google, Inc.
Scott Blum — Taming AJAX with GWT Page 58
JSNI FeaturesImplement a Java method in JavaScript Call Java methods from JavaScriptRead and write Java fields from JavaScriptMarshal values between Java/JavaScript
Super efficient in production
Throw exceptions across Java/JavaScript boundariesOur entire client framework is based on it
If you don’t like ours, you can write your own ☺
Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Google, Inc.
Scott Blum — Taming AJAX with GWT Page 59
Code Sample – JSNInative JavaScriptObject lazyInitMap() /*-{var map = [email protected]::stringMap;if (map == null) {map = [email protected]::stringMap = {};
}return map;
}-*/;
native String getValue(String value) /*-{var map = [email protected]::lazyInitMap()();var result = map[key];return (result == null) ? null : result;
}-*/;
native void putValue(String key, String value) /*-{var map = [email protected]::lazyInitMap()();map[key] = value;
}-*/;
Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Google, Inc.
Scott Blum — Taming AJAX with GWT Page 60
OverviewWhy AJAX?Why GWT?How GWT WorksAdd GWT to your AppAdvanced TechniquesSummaryQ & A
Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Google, Inc.
Scott Blum — Taming AJAX with GWT Page 61
Growing GWT CommunityCommunity and Support
5000+ members on the developer forumBooks, articles, blogsMeta-sites (e.g. http://gwtpowered.org)645,000 results for “google web toolkit”
Libraries and Applications25 projects on Google Code Project HostingGWT Widgets on SourceForgeJetBrains' JET markup framework for GWT
ToolsJetBrains IntelliJ IDEA support for GWT built into Version 6.0Googlipse, an open source Eclipse plug-in for GWTVistaFei for GWT – Visual DesignerInstantiations GWT Designer – Visual Designer; round-trip to Source!
Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Google, Inc.
Scott Blum — Taming AJAX with GWT Page 62
Instantiations GWT Designer
Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Google, Inc.
Scott Blum — Taming AJAX with GWT Page 63
Production Appshttp://www.dimdim.comWeb-based conferencing
http://www.indigopuzzles.comHigh-quality online puzzles
http://www.notetwonote.comStudent self-testing
Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Google, Inc.
Scott Blum — Taming AJAX with GWT Page 64
ConclusionAJAX is hard: you tame it… or vice-versa
You need leverage to take full advantage of AJAXPhD in browser quirks is no longer a prereqWe will share our best work and ideas with you, and we hope you will return the favorMuch more to come… see you online!
http://code.google.com/webtoolkit/
Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Google, Inc.
Scott Blum — Taming AJAX with GWT Page 65
Q & AAsk me anything! ☺[email protected]