Va Lanie
-
Upload
brandon-hale -
Category
Documents
-
view
217 -
download
0
Transcript of Va Lanie
8/9/2019 Va Lanie
http://slidepdf.com/reader/full/va-lanie 1/156
Everything You Wanted to Kn
About Writing Async,Concurrent HTTP Apps i
Java
8/9/2019 Va Lanie
http://slidepdf.com/reader/full/va-lanie 5/156
About your spea"er
# $ b a r u
c hg i t h u b %c o & ' $ b a r u c
h
in"d%in'$baruch
tac"over(ow%co&'users')*+*-'$ba
8/9/2019 Va Lanie
http://slidepdf.com/reader/full/va-lanie 11/156
CONCURRENT DOWNLOADS
Y U NO SUPPORT THEM?!
8/9/2019 Va Lanie
http://slidepdf.com/reader/full/va-lanie 12/156
0e1uire&ents
2 parae 3e 4ownoads2 Parae 3e parts
2 interrupt'pause'resu&e
2 Progress events
2 Chec"su&s caching
8/9/2019 Va Lanie
http://slidepdf.com/reader/full/va-lanie 13/156.irst Association 5or 6concurrent dow
8/9/2019 Va Lanie
http://slidepdf.com/reader/full/va-lanie 15/156
8uc"y day! 4ownoad &anager written i
8/9/2019 Va Lanie
http://slidepdf.com/reader/full/va-lanie 17/156
8et:s oo" i5 we can use
;%<o traceabe icense+%<o website or docs
-%<o traceabe sources)% =t:s an app, not a ib
8/9/2019 Va Lanie
http://slidepdf.com/reader/full/va-lanie 19/156
WHY WON’T YOU JUST
USE URLCONNECTION?
8/9/2019 Va Lanie
http://slidepdf.com/reader/full/va-lanie 20/156
Java%net%urconnectio
;% Me&ory waste5u >bu?ering+% Mini&a AP=
-% oc"ing strea&s
8/9/2019 Va Lanie
http://slidepdf.com/reader/full/va-lanie 22/156
What we:re oo"ing 5o
;% Async'nonBboc"ing+% Event cabac"s
8/9/2019 Va Lanie
http://slidepdf.com/reader/full/va-lanie 23/156
What is =T going to ta"
;% 0eactor+% nio
8/9/2019 Va Lanie
http://slidepdf.com/reader/full/va-lanie 24/156
Weco&e to the react
tt 5 i ht i ht
8/9/2019 Va Lanie
http://slidepdf.com/reader/full/va-lanie 25/156
2 pattern 5or ightweight concu
2 Event driven
2 Threads reuse
2 ses nonBboc"ing =o
8/9/2019 Va Lanie
http://slidepdf.com/reader/full/va-lanie 26/156
Drigina pattern
http!''www%dre%vanderbit%edu'sch&idt'P4.'reactorBsie&ens% d5
Fuess the author by t
8/9/2019 Va Lanie
http://slidepdf.com/reader/full/va-lanie 27/156
Fuess the author by tdiagra&
http!''gee%cs%oswego%edu'
8/9/2019 Va Lanie
http://slidepdf.com/reader/full/va-lanie 31/156
Java version B 4ispatch
while (!Thread.interrupted()) {
selector.select();
Set selected = selector.selectedKeys();
Iterator it = selected.iterator();
while (it.hasNext())
SelectionKey k = (SelectionKey)(it.ne
((Runnable)(k.attachment())).run();
selected.clear();
}
8/9/2019 Va Lanie
http://slidepdf.com/reader/full/va-lanie 32/156
Handing reactor events is co&
2 <eed to &aintain state2 u?ering 2 asse&bing chun"
2 Coordinating async events
HTTP NIO FRAMWORKS
8/9/2019 Va Lanie
http://slidepdf.com/reader/full/va-lanie 33/156
HTTP NIO FRAMWORKS
DO THE HEAVY LIFTING
8/9/2019 Va Lanie
http://slidepdf.com/reader/full/va-lanie 34/156
<io ibraries
2 Most o5 the& are servers2 <etty, griIIy, etc%
2 Apache Mina
2 Apache HTTP co&ponentsasynccient
<ing http cient
8/9/2019 Va Lanie
http://slidepdf.com/reader/full/va-lanie 42/156
Here it is9
try >AsyncHttpCient asyncHttpCient new AsyncHttpCient>@@
8/9/2019 Va Lanie
http://slidepdf.com/reader/full/va-lanie 43/156
y > sy c pC e asy c pC e e sy c pC e >@@ 8istenabe.utureL0esponse 5uture asyncHttpCient%prepare "http://oss.jfrog.org/api/system/ping"@%eecute> new AsyncCo&petionHanderL0esponse>@ #Dverride
public 0esponse onCo&peted>0esponse response@ Gyste&%out %printn>response%get0esponseody>@@N return responseN O
#Dverride
public void onThrowabe>Throwabe t@ t%printGtac"Trace>@N O O@N 0esponse response 5uture%get>@NO
8/9/2019 Va Lanie
http://slidepdf.com/reader/full/va-lanie 45/156
HAC Concepts
2 0e1uest producer2 0esponse consu&er
try (CloseableHttpAsyncClient asyncHttpClient = HttpAsyncClients.createDefauasyncHttpClient.start();Future<HttpResponse> future = asyncHttpClient execute(
8/9/2019 Va Lanie
http://slidepdf.com/reader/full/va-lanie 46/156
Future<HttpResponse> future = asyncHttpClient.execute(HttpAsyncMethods.createet("http://oss.jfrog.org/api/system/pingnew AsyncByteConsumer<HttpResponse>() {
"#$errideprotected void onResponseRecei$ed(final HttpResponse respons
%yste&.out.println(response.'et%tatusine().'etReasonhr*
"#$errideprotected void onByteReceived(final Char+u,,er bu,! final -#
"#$errideprotected void releaseResources() { *
"#$errideprotected HttpResponse buildResult(final HttpContext context
return (HttpResponse) context.'etAttribute("http.respons*
*! null);HttpResponse response = future.get();
*
WAIT A SECOND
8/9/2019 Va Lanie
http://slidepdf.com/reader/full/va-lanie 47/156
WAIT A SECOND…
THEY ARE ALMOST THE SAME!
Choosing between ning and http asyn
8/9/2019 Va Lanie
http://slidepdf.com/reader/full/va-lanie 48/156
Choosing between ning and http asyn
8/9/2019 Va Lanie
http://slidepdf.com/reader/full/va-lanie 50/156
pulic interface " ttp#rovider$ ow nload" andler {
void onResponseRecei$ed(int statusCode! M ap< %trin'! ist< %trin'
oolean on+ytesRecei$ed(+yte+u,,er bu,);
void onFailed(/hro0 able error);
void onCanceled();
void onCo& pleted();*
Head to head
8/9/2019 Va Lanie
http://slidepdf.com/reader/full/va-lanie 51/156
Head to head
Feature/Library Ning client Http sync !l
aturity Food Qery new >eary
#ownloadcancelation
Easy With bugs
$rogress hoo%s Events not
granuar enough
Just use on+yteR
#ocumentation A bit sparse Mini&a
&erver'sidecounterpart
<one, cient ony org%apache%httshttpcoreBnio
Per5or&ance/
8/9/2019 Va Lanie
http://slidepdf.com/reader/full/va-lanie 52/156
Per5or&ance/
G&a 3e Mediu& 3e 8arge 3e*
;**
+**-**
)**
**
R**
S**
**
U**
http!''bogs atassian co&'+*;-'*S'htt
8/9/2019 Va Lanie
http://slidepdf.com/reader/full/va-lanie 55/156
Con5used/
Just read so&e stac"over(ow
8/9/2019 Va Lanie
http://slidepdf.com/reader/full/va-lanie 56/156
>and i&prove your rep as you go@
And that odiscoverin
8/9/2019 Va Lanie
http://slidepdf.com/reader/full/va-lanie 57/156
discoverinrange healost on re
HTTP WAR STORIES
Vuestion
8/9/2019 Va Lanie
http://slidepdf.com/reader/full/va-lanie 58/156
Vuestion
What shoudcontentBeng
when usingco&pression
8/9/2019 Va Lanie
http://slidepdf.com/reader/full/va-lanie 61/156
https!''github co&'http+'http+ spec'issues')R
Vuestion9
8/9/2019 Va Lanie
http://slidepdf.com/reader/full/va-lanie 62/156
Vuestion9
Why when redirected to C4<the chun"s start 5ro& Iero/
8/9/2019 Va Lanie
http://slidepdf.com/reader/full/va-lanie 63/156
HttpAsyncClient+uilder builder = HttpAsyncClients.custo& ();11 add redirect strate'y that copies 2ran'e2 headers! i, existuilder.setRedirect%trategy(ne0 $ efaultRedirect%trategy() {
" #$erride
public Http3riRe4uest 'etRedirect(HttpRe4uest re4uest! HttpResponse resHttpContext context)
Http3riRe4uest redirectRe4uest = super.'etRedirect(re4uest! response! co11 copy 2Ran'e2 headers! i, existHeader56 ran'eHeaders = re4uest.'etHeaders(HttpH eaders.RA78);
i, (ran'eHeaders 9= null) {,or (Header header : ran'eH eaders) {
redirectRe&uest.add" eader(header);*
*return redirectRe4uest;
**);
Vuestion9
8/9/2019 Va Lanie
http://slidepdf.com/reader/full/va-lanie 64/156
Vuestion9
How &any si&utaneouconnections shoud =open/
8/9/2019 Va Lanie
http://slidepdf.com/reader/full/va-lanie 70/156
public static Gtringencoder>Gtring urGtr@ 08Encoder%encode>urGtr, "()*"@N %%%O
4ecoded 08s cannot bereBencoded to the sa&e 5or&
8/9/2019 Va Lanie
http://slidepdf.com/reader/full/va-lanie 71/156
re encoded to the sa&e 5or&
http:11exa& ple.co& 14uery= ab= = c
Cannot be decoded bac a,ter it 0 as e
http:11exa& ple.co& 14uery= a ?@b= =
8/9/2019 Va Lanie
http://slidepdf.com/reader/full/va-lanie 75/156
How do =cose asoc"etcorrecty/
How hard can it be to cose a so
8/9/2019 Va Lanie
http://slidepdf.com/reader/full/va-lanie 78/156
custo&ers
<ever boc" in soc"ecose>@
8/9/2019 Va Lanie
http://slidepdf.com/reader/full/va-lanie 79/156
cose>@
•
The other side epectyou to cean up nicey
•
=t wi give up on ti&e • You wi wait >5orever@
I ALWAYS CLOSECONNECTIONS
8/9/2019 Va Lanie
http://slidepdf.com/reader/full/va-lanie 80/156
CONNECTIONS
AND WHEN I DO IT I USE
TRY"WITH"RESOURCES
0e&e&ber/
8/9/2019 Va Lanie
http://slidepdf.com/reader/full/va-lanie 82/156
2 Write to separate 3es, co&b3nish
8/9/2019 Va Lanie
http://slidepdf.com/reader/full/va-lanie 83/156
3nish
2 Write to sa&e 3e, see"ing tright position
WHY WON’T YOU JUST US
8/9/2019 Va Lanie
http://slidepdf.com/reader/full/va-lanie 86/156
• =&pe&ents %eeable+yteChan
a$a.nio.channels.'ileChannelB w rite( a$a.nio.+yte+u,,er src! lon' positio
downoad progress trac
8/9/2019 Va Lanie
http://slidepdf.com/reader/full/va-lanie 87/156
• $ersistent.ieProgress=n5o – Gave the tota siIe, sha;, nu&ber o5 pa
– Gtate o5 each part >o?set, siIe, co&pe
.ieProgress=n5o .iePartProgress=n5o
.ie 8oc"ing
8/9/2019 Va Lanie
http://slidepdf.com/reader/full/va-lanie 91/156
Fileoc loc = ,ileChannel.tryoc(); 11 onshared: (! on'.M ADEA38!false)
if(loc = = null) { throw new #$erlappin'Fileoc8xception(); * return loc;
*
Ecusive
8/9/2019 Va Lanie
http://slidepdf.com/reader/full/va-lanie 94/156
2 Prevent sa&e QM threads writing t
3e when we started cosing it2 Cosing se1uence!
2 0eease 3e oc"s
2 Cose channes2 0ena&e a 3e to itXs 3na na&e >re&o
2 Erase progress in5o
8/9/2019 Va Lanie
http://slidepdf.com/reader/full/va-lanie 97/156
2 Mosty standardiIing FoogeXs spd
2 Header co&pression2 &utipeing
2 PrioritiIation
2 Gerver push2 Dn the way cear so&e stu?
2 E%g% co&pressed content ength
Ease the oad
8/9/2019 Va Lanie
http://slidepdf.com/reader/full/va-lanie 101/156
A Vuic" Touo5 Logos
The 8ogica
Go what eacty is ogic/ Who
8/9/2019 Va Lanie
http://slidepdf.com/reader/full/va-lanie 102/156
=n5or&ay, ogic is about saying things that ma%
sense. You can thin" o5 it in that way i5 you i"e%6=tXs pretty sunny today, so you shoud wear sunscree
.or&ay, ogic is the art o5 arguing 2 no3ght or debate, but by using the in5or&a
aready "now to draw new and concusions%6=5 itXs sunny today, you shoud wear sunscree
=ndeed it is sunny today% There5ore, you shoud wear sunscreen%7
8/9/2019 Va Lanie
http://slidepdf.com/reader/full/va-lanie 104/156
;% This piece o5 5resh 5ruit is 5uIIy%
+% =t aso has seeds%-% =5 a 5ruit is 5uIIy, itXs either a "iwi or a pea
)% Peaches have a pitN they donXt have seed
% Go the 5ruit canXt be a peach%
R% Go the 5ruit &ust be a "iwi%
Which o5 the above sentences is a conc
How about a &ore co&pe argu&
8/9/2019 Va Lanie
http://slidepdf.com/reader/full/va-lanie 105/156
;% This piece o5 5resh 5ruit is 5uIIy%
+% =t aso has seeds%-% =5 a 5ruit is 5uIIy, itXs either a "iwi or a pea
)% Peaches have a pitN they donXt have seed
% Go the 5ruit canXt be a peach%
R% Go the 5ruit &ust be a "iwi%
Food ogic ets us cobbe together otsdi?erent pieces o5 in5or&ation, and te
the& whatXs probably or defnitely tru
ut what counts as 6good og
8/9/2019 Va Lanie
http://slidepdf.com/reader/full/va-lanie 106/156
That argu&ent was good >&ade sense@, because the con5oowed 5ro& the pre&ises% WeX see what this &eans in
Why donXt we oo" at a bad argu&ent/
ut what counts as 6good og
8/9/2019 Va Lanie
http://slidepdf.com/reader/full/va-lanie 107/156
Go&e peope have 5aen o? ci?s and i
There5ore, i5 = $u&p o? this ci?, = wi de3nite
8/9/2019 Va Lanie
http://slidepdf.com/reader/full/va-lanie 109/156
8etXs oo" at two "inds o5 argu&ents%
-. 4eductive reasoning! A interns can breathe 3re% Go Phiip
breathe 3re%
=s there a piece o5 the puIIe &issing/
8/9/2019 Va Lanie
http://slidepdf.com/reader/full/va-lanie 114/156
ut even better is the superpower to spot weak
<et ti&e you watch TQ or go onine >with your pper&ission, o5 course@, try to "eep trac" o5 how &di?erent argu&ents are being pitched to you by &uch in5o is given to you/ How &uch is e5t out/
8/9/2019 Va Lanie
http://slidepdf.com/reader/full/va-lanie 118/156
)o utumn is a poe& by Engish 0o&antic John Keats >-; Dctober ;SU 2 +- .ebruary ;
8/9/2019 Va Lanie
http://slidepdf.com/reader/full/va-lanie 119/156
To Autu&n is the 3na wor" in a gro5 poe&s "nown as KeatsXs ;;U o
8/9/2019 Va Lanie
http://slidepdf.com/reader/full/va-lanie 121/156
The i&agery is richy achievedthrough the personi3cation o5 Au
8/9/2019 Va Lanie
http://slidepdf.com/reader/full/va-lanie 122/156
$oem
Geason o5 &ists and &eow 5ruit5u
8/9/2019 Va Lanie
http://slidepdf.com/reader/full/va-lanie 124/156
!lose bosom'friend of the maturingsun2
!onspiring with him how to load and
8/9/2019 Va Lanie
http://slidepdf.com/reader/full/va-lanie 126/156
To bend with appes the &ossXd cottageBtre
8/9/2019 Va Lanie
http://slidepdf.com/reader/full/va-lanie 127/156
nd +ll all fruit with ripeness to the
core2
8/9/2019 Va Lanie
http://slidepdf.com/reader/full/va-lanie 129/156
With a sweet "erneN to set budding
8/9/2019 Va Lanie
http://slidepdf.com/reader/full/va-lanie 130/156
And sti &ore, ater (owers 5or the
8/9/2019 Va Lanie
http://slidepdf.com/reader/full/va-lanie 131/156
nti they thin" war& days wi never cea
8/9/2019 Va Lanie
http://slidepdf.com/reader/full/va-lanie 134/156
Go&eti&es whoever see"s abroad 3nd
8/9/2019 Va Lanie
http://slidepdf.com/reader/full/va-lanie 135/156
Thee sitting careess on a granary
8/9/2019 Va Lanie
http://slidepdf.com/reader/full/va-lanie 144/156
Where are the songs o5 Gpring/ Ay, where are
8/9/2019 Va Lanie
http://slidepdf.com/reader/full/va-lanie 145/156
Thin" not o5 the&, thou hast thy &usic t
8/9/2019 Va Lanie
http://slidepdf.com/reader/full/va-lanie 146/156
Whie barrZd couds boo& the so5tBdying
8/9/2019 Va Lanie
http://slidepdf.com/reader/full/va-lanie 147/156
And touch the stubbeBpains with rosy
8/9/2019 Va Lanie
http://slidepdf.com/reader/full/va-lanie 151/156
And 5uBgrown a&bs oud beat 5ro& hiy b
8/9/2019 Va Lanie
http://slidepdf.com/reader/full/va-lanie 152/156
edgeBcric"ets singN and now with treb
8/9/2019 Va Lanie
http://slidepdf.com/reader/full/va-lanie 154/156
And gathering swaows twitter in th