-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
441 lines (428 loc) · 13.6 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<title>reveal.js</title>
<link rel="stylesheet" href="css/reveal.css">
<link rel="stylesheet" href="css/theme/black.css">
<!-- Theme used for syntax highlighting of code -->
<link rel="stylesheet" href="lib/css/zenburn.css">
<!-- Printing and PDF exports -->
<script>
var link = document.createElement( 'link' );
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = window.location.search.match( /print-pdf/gi ) ? 'css/print/pdf.css' : 'css/print/paper.css';
document.getElementsByTagName( 'head' )[0].appendChild( link );
</script>
<style>
h1, h2 {
color: #B68585 !important;
}
</style>
</head>
<body>
<div class="reveal">
<div class="slides">
<section data-background="#dddddd">
<p>Programação assíncrona com CompletableFuture</p>
<a href="#" class="navigate-down">
<img data-src="img/auto-components.jpg">
</a>
<p>
<small>Emerson Venâncio @ Dígitro Tecnologia</small>
<br/>
<small>Filipi da Silva Fuchter @ Dígitro Tecnologia</small>
</p>
</section>
<section data-background="#1c4587">
<div class="sl-block" data-block-type="image">
<img style="border: 0px; background-color: transparent" height="600px" data-src="img/mao_mundo.png"/>
<img style="float: right; border: 0px; background-color: transparent; top: 300px;" height="100px" data-src="img/digitro-logo.png"/>
</div>
</section>
<section data-background="#dddddd">
<h2>Programação assíncrona</h2>
</section>
<section>
<h2>Por Que?</h2>
<p>Programação assíncrona deve ser usada para otimizar o uso de recursos do hardware</p>
<div class="content">
<blockquote style="font-size: 2rem;">
<ul>
<li>Adicionar mais threads não traz performance em todos os casos</li>
<li>Quando o número de threads excede o número de processadores o efeito é inverso</li>
</ul>
</blockquote>
</div>
</section>
<section data-background="#1c4587">
<div class="sl-block" data-block-type="image">
<img style="border: 0px; background-color: transparent" height="600px" data-src="img/officeBoy.jpg"/>
</div>
</section>
<section data-background="#1c4587">
<div class="sl-block" data-block-type="image">
<img style="border: 0px; background-color: transparent" height="600px" data-src="img/fila.jpg"/>
</div>
</section>
<section data-background="#1c4587">
<div class="sl-block" data-block-type="image">
<img style="border: 0px; background-color: transparent" height="600px" data-src="img/diagrama.jpg"/>
</div>
</section>
<section>
<p>Sistema OfficeBoy</p>
<pre>
<code class="hljs" data-trim>
public class OfficeBoy {
@PersistenceUnit
EntityManager manager;
public void pagaBoletos(List ids) {
for (Long idBoleto : ids) {
Boleto boleto = manager.getReference(Boleto.class, idBoleto);
Float valor = boleto.getValor();
boolean pago = BancoWSApi.pagaBoleto(boleto, valor);
boleto.setPago(pago);
manager.merge(boleto);
}
}
}
</code>
</pre>
</section>
<section data-background="#1c4587">
<div class="sl-block" data-block-type="image">
<img style="border: 0px; background-color: transparent" height="600px" data-src="img/diagrama boleto2.jpg"/>
</div>
</section>
<section data-background="#1c4587">
<div class="sl-block" data-block-type="image">
<img style="border: 0px; background-color: transparent" height="600px" data-src="img/memoria.jpg"/>
</div>
</section>
<section>
<h2>Quando?</h2>
<p></p>
<div class="content">
<blockquote style="font-size: 2rem;">
<ul>
<li>Servlets</li>
<li>Clientes HTTP</li>
<li>I/O</li>
<li>Grande volume de dados</li>
</ul>
</blockquote>
</div>
</section>
<section>
<h2>Evitando lock</h2>
<p>Adicionando e consulmindo sem lock</p>
<pre>
<code class="hljs" data-trim>
<!--
List<String> tmp = new ArrayList<>();
private List<String> getWorkList() {
List<String> work = tmp;
tmp = new ArrayList<>();
return work;
}-->
</code>
</pre>
</section>
<section>
<h2>Evitando lock</h2>
<p>Use objetos imutáveis para salvar estado</p>
<pre>
<code class="hljs" data-trim>
<!--
List<String> work = Collections.unmodifiableList(new ArrayList<>());
public synchronized void addToWorkList(String o) {
List<String> tmp = new ArrayList<>(work);
tmp.add(o);
work = Collections.unmodifiableList(tmp);
} -->
</code>
</pre>
</section>
<section data-background="#dddddd">
<h2>Completable Future</h2>
<p>Representa um objeto Futuro que pode ser explicitamente completado,
e que pode ser utilizado para agregar funções dependentes que serão disparadas quando isto ocorrer.
</p>
</section>
<section>
<h2>Interfaces implementadas</h2>
<ul class="small-li">
<li>Future
<ul>
<li>Não notifica quando o resultado está disponível.</li>
<li>Métodos: get() ou isDone()</li>
</ul>
</li>
<hr>
<li>CompletionStage
<ul>
<li>Realiza uma ação quando uma outra ação for concretizada.</li>
</ul>
</li>
</ul>
</section>
<section>
<h2>Métodos</h2>
<ul class="small-li">
<li>38 métodos total</li>
<li>Características dos métodos:
<ul>
<li>somethingAsync(..., Executor)</li>
<li>somethingAsync(...)</li>
<ul>
<li>Utiliza ForkJoinPool</li>
</ul>
<li>something(...)</li>
</ul>
</li>
</ul>
</section>
<section>
<h2>Métodos</h2>
<ul class="small-li">
<li>Argumentos e retornos:
<ul>
<li>Apply</li>
<li>Accept</li>
<li>Run</li>
</ul>
</li>
</ul>
</section>
<section>
<h2>Métodos</h2>
<ul class="small-li">
<li>Forma de execução do método
<ul>
<li>single input
<ul><li>thenApply, thenAccept, thenRun</li></ul>
</li>
<li>binary or
<ul><li>applyToEither, acceptEither, runAfterEither</li></ul>
</li>
<li>binary and
<ul><li>thenCombine, thenAcceptBoth, runAfterBoth</li></ul>
</li>
</ul>
</li>
</ul>
</section>
<section>
<h2>Métodos:</h2>
<ul class="small-li">
<li>thenCompose</li>
<li>whenComplete</li>
<li>exceptionally</li>
<li>handle</li>
</ul>
</section>
<section>
<h2>Métodos</h2>
<ul class="small-li">
<li>Diferenças:
<ul>
<li>thenCompose: retorna outro stage que retornará o resultado</li>
<li>thenApply: retorna resultado em sí</li>
</ul>
</li>
</ul>
</section>
<section>
<h2>Comportamento das Threads</h2>
<ul class="small-li">
<li>ForkJoinPool</li>
<li>Executors</li>
<div class="sl-block" data-block-type="image">
<img style="border: 0px; background-color: transparent" height="300px" data-src="img/threads_01.png"/>
</div>
</ul>
<p>Threads demais podem levar a cenários de erro!</p>
</section>
<section>
<h2>Comportamento das Threads</h2>
<div class="sl-block" data-block-type="image">
<img style="border: 0px; background-color: transparent" height="450px" data-src="img/threads_02.png"/>
</div>
</section>
<section>
<h2>Comportamento das Threads</h2>
<div class="sl-block" data-block-type="image">
<img style="border: 0px; background-color: transparent" height="450px" data-src="img/threads_03.png"/>
</div>
</section>
<section>
<h2>Comportamento das Threads</h2>
<div class="sl-block" data-block-type="image">
<img style="border: 0px; background-color: transparent" height="300px" data-src="img/threads_04.png"/>
</div>
</section>
<section>
<h2>Tratamento de Exceção</h2>
<ul class="small-li">
<li>whenComplete</li>
<li>exceptionally</li>
<li>handle</li>
</ul>
</section>
<section>
<h2>Exemplo</h2>
<ul class="small-li">
<li>Exemplo utilizando os principais métodos:</li>
<li><a href="examples/TestCompletableFuture.java" target="_blank">TestCompletableFuture</a></li>
</ul>
</section>
<section>
<h2>Performance</h2>
<ul class="small-li">
<li>Asynchronous API with CompletableFuture</li>
<li><a href="https://youtu.be/lyJBtg6Dgwc" target="_blank">JavaOne Oracle - Sergey Kuksenko</a></li>
<hr>
<li>Evite transições entre uma Thread para outra. Isto custa!</li>
<li>Evite excesso de threads!</li>
<li>Evite bloquear as Threads!
<ul>
<li>get()</li>
<li>join()</li>
</ul>
</li>
</ul>
</section>
<section data-background="#dddddd">
<h2>Casos de Uso</h2>
<!--<img data-src="img/app-archtiecture.svg">-->
</section>
<section>
<p>Workflow para enriquecimento de dados</p>
<img data-src="img/fluxos_pipeline.png">
</section>
<section>
<p>Workflow para enriquecimento de dados</p>
<pre>
<code class="hljs" data-trim>
var templateStep = new QueryBCStep(stepConfig);
templateStep.nextStep(new LinkBCStep(stepConfig))
.nextStep(new TagResultStep(stepConfig, UIsToTag.getTagRequestDTO(nomeUI)));
var stepInicial = new LoadUIDTOStep(stepConfig);
stepInicial.nextStep(new ForkOperacoesStep(stepConfig, request.getOperacoes(), templateStep))
.nextStep(new JoinStep(stepConfig))
.nextStep(new ClassifyResultStep(stepConfig))
.nextStep(new PersistTemporaryStep(stepConfig))
.nextStep(callback);
stepInicial.execute(stepContext, request.getUIID());
</code>
</pre>
</section>
<section>
<p>Workflow para enriquecimento de dados</p>
<pre>
<code class="hljs" data-trim>
<!--
List<CompletableFuture<List<UIDTO>>> promises = this.executaOperacoes(stepContext, uidto);
proceed(stepContext, promises);
-->
</code>
</pre>
</section>
<section>
<p>Workflow para enriquecimento de dados</p>
<pre>
<code class="hljs" data-trim>
<!--
protected CompletableFuture<List<UIDTO>> criarPromiseComTodosValores(List<CompletableFuture<List<UIDTO>>> promises) {
try {
CompletableFuture allOf = CompletableFuture.allOf(promises.toArray(new CompletableFuture[promises.size()]));
CompletableFuture<List<UIDTO>> allPromises = allOf;
return allPromises.thenApply((uis) -> {
List<UIDTO> valores = new ArrayList<UIDTO>();
for (CompletableFuture<List<UIDTO>> promise : promises) {
try {
List<UIDTO> lista = promise.get();
if (lista != null) {
valores.addAll(lista);
}
} catch (Exception e) {
tratarErro(e);
}
}
return valores;
});
} catch (Exception e) {
tratarErro(e);
return CompletableFuture.completedFuture(Collections.emptyList());
}
}
-->
</code>
</pre>
</section>
<section>
<p>Workflow para enriquecimento de dados</p>
<pre>
<code class="hljs" data-trim>
<!--
CompletableFuture<List<UIDTO>> uisFuture = this.criarPromiseComTodosValores(promises);
uisFuture.whenComplete((uis, erro) -> {
proceed(stepContext, uis);
});
-->
</code>
</pre>
</section>
<section data-background="#245628">
<h2>Benefícios</h2>
<div class="content">
<ul class="small-li">
<li>Componentes com escopo bem definido e reutilizáveis (arquitetura)</li>
<li>Legibilidade do fluxo de negócio (manutenção)</li>
<li>Melhor aproveitamento dos recursos de hardware (performance)</li>
</ul>
</div>
<img data-src="img/agnostic.png">
</section>
<section data-background="#B63C3C">
<h2>Riscos</h2>
<div class="content">
<ul class="small-li">
<li>Hábitos de programação síncrona</li>
<li>Callback-hell</li>
<li>Estados inesperados por modificação concorrente</li>
<li>Deadlocks</li>
</ul>
</div>
</section>
<section>
<img data-src="img/doubts.jpg">
<p>
<small>Emerson Venâncio @ Dígitro Tecnologia</small>
<br/>
<small>Filipi da Silva Fuchter @ Dígitro Tecnologia</small>
</p>
<a href="https://github.com/filipi87/completablefuture-revealjs" target="_blank">https://github.com/filipi87/completablefuture-revealjs</a>
</section>
</div>
</div>
<script src="lib/js/head.min.js"></script>
<script src="js/reveal.js"></script>
<script>
// More info about config & dependencies:
// - https://github.com/hakimel/reveal.js#configuration
// - https://github.com/hakimel/reveal.js#dependencies
Reveal.initialize({
dependencies: [
{ src: 'plugin/markdown/marked.js' },
{ src: 'plugin/markdown/markdown.js' },
{ src: 'plugin/notes/notes.js', async: true },
{ src: 'plugin/highlight/highlight.js', async: true, callback: function() { hljs.initHighlightingOnLoad(); } }
]
});
</script>
</body>
</html>