Learning Reactive Programming with Java 8 1st Edition Nickolay Tsvetinov pdf download
Learning Reactive Programming with Java 8 1st Edition Nickolay Tsvetinov pdf download
https://ebookname.com/product/learning-reactive-programming-with-
java-8-1st-edition-nickolay-tsvetinov/
https://ebookname.com/product/pro-java-8-programming-3rd-edition-
terrill-brett-spell/
https://ebookname.com/product/functional-reactive-
programming-1st-edition-stephen-blackheath/
https://ebookname.com/product/practical-database-programming-
with-java-1st-edition-ying-bai/
https://ebookname.com/product/charge-and-energy-transfer-
dynamics-in-molecular-systems-third-edition-dr-volkhard-may/
Grim Reaper End of Days 1st Edition Steve Alten
https://ebookname.com/product/grim-reaper-end-of-days-1st-
edition-steve-alten/
https://ebookname.com/product/dk-eyewitness-top-10-travel-guide-
las-vegas-1st-edition-connie-emerson/
https://ebookname.com/product/inderbir-singh-s-textbook-of-human-
histology-with-colour-atlas-and-practical-guide-7th-revised-
edition-neelam-vasudeva/
https://ebookname.com/product/shadow-banking-scope-origins-and-
theories-1st-edition-nesvetailova/
https://ebookname.com/product/construction-project-scheduling-
and-control-3rd-edition-saleh-a-mubarak/
Education in Political Science Discovering a Neglected
Field 1st Edition Anja P. Jakobi
https://ebookname.com/product/education-in-political-science-
discovering-a-neglected-field-1st-edition-anja-p-jakobi/
Table of Contents
Learning Reactive Programming with Java 8
Credits
About the Author
About the Reviewers
www.PacktPub.com
Support files, eBooks, discount offers, and more
Why subscribe?
Free access for Packt account holders
Preface
What this book covers
What you need for this book
Who this book is for
Conventions
Reader feedback
Customer support
Downloading the example code
Errata
Piracy
Questions
1. An Introduction to Reactive Programming
What is reactive programming?
Why should we be reactive?
Introducing RxJava
Downloading and setting up RxJava
Comparing the iterator pattern and the RxJava Observable
Implementing the reactive sum
Summary
2. Using the Functional Constructions of Java 8
Lambdas in Java 8
Introducing the new syntax and semantics
Functional interfaces in Java 8 and RxJava
Implementing the reactive sum example with lambdas
Pure functions and higher order functions
Pure functions
Higher order functions
RxJava and functional programming
Summary
3. Creating and Connecting Observables, Observers, and Subjects
The Observable.from method
The Observable.just method
Other Observable factory methods
The Observable.create method
Subscribing and unsubscribing
Hot and cold Observable instances
The ConnectableObservable class
The Subject instances
Summary
4. Transforming, Filtering, and Accumulating Your Data
Observable transformations
Transformations with the various flatMap operators
Grouping items
Additional useful transformation operators
Filtering data
Accumulating data
Summary
5. Combinators, Conditionals, and Error Handling
Combining the Observable instances
The zip operator
The combineLatest operator
The merge operator
The concat operator
The conditional operators
The amb operator
The takeUntil(), takeWhile(), skipUntil(), and skipWhile()
conditional operators
The defaultIfEmpty( ) operator
Handling errors
The return and resume operators
The retrying technique
An HTTP client example
Summary
6. Using Concurrency and Parallelism with Schedulers
RxJava's schedulers
Debugging Observables and their schedulers
The interval Observable and its default scheduler
Types of schedulers
The Schedulers.immediate scheduler
The Schedulers.trampoline scheduler
The Schedulers.newThread scheduler
The Schedulers.computation scheduler
The Schedulers.io scheduler
The Schedulers.from(Executor) method
Combining Observables and schedulers
The Observable<T> subscribeOn(Scheduler) method
The Observable<T> observeOn(Scheduler) operator
Parallelism
Buffering, throttling, and debouncing
Throttling
Debouncing
The buffer and window operators
The backpressure operators
Summary
7. Testing Your RxJava Application
Testing using simple subscription
The BlockingObservable class
The aggregate operators and the BlockingObservable class
Testing with the aggregate operators and the BlockingObservable
class
Using the TestSubscriber class for in-depth testing
Testing asynchronous Observable instances with the help of the
TestScheduler class
Summary
8. Resource Management and Extending RxJava
Resource management
Introducing the Observable.using method
Caching data with Observable.cache
Creating custom operators with lift
Composing multiple operators with the Observable.compose
operator
Summary
Index
Learning Reactive
Programming with Java 8
Learning Reactive
Programming with Java 8
Copyright © 2015 Packt Publishing
Every effort has been made in the preparation of this book to ensure
the accuracy of the information presented. However, the information
contained in this book is sold without warranty, either express or
implied. Neither the author, nor Packt Publishing, and its dealers and
distributors will be held liable for any damages caused or alleged to
be caused directly or indirectly by this book.
Livery Place
35 Livery Street
ISBN 978-1-78528-872-2
www.packtpub.com
Credits
Author
Nickolay Tsvetinov
Reviewers
Samuel Gruetter
Dávid Karnok
Timo Tuominen
Shixiong Zhu
Commissioning Editor
Veena Pagare
Acquisition Editor
Larrisa Pinto
Adrian Raposo
Technical Editor
Abhishek R. Kotian
Copy Editors
Brandt D'mello
Neha Vyas
Project Coordinator
Sanchita Mandal
Proofreader
Safis Editing
Indexer
Mariammal Chettiyar
Production Coordinator
Conidon Miranda
Cover Work
Conidon Miranda
About the Author
Nickolay Tsvetinov is a professional all-round web developer at
TransportAPI—Britain's first comprehensive open platform for
transport solutions. During his career as a software developer, he
experienced both good and bad and played with most of the popular
programming languages—from C and Java to Ruby and JavaScript.
For the last 3-4 years, he's been creating and maintaining single-
page applications (SPA) and the backend API architectures that
serve them. He is a fan of open source software, Rails, Vim, Sinatra,
Ember.js, Node.js, and Nintendo. He was an unsuccessful musician
and poet, but he is a successful husband and father. His area of
interest and expertise includes the declarative/functional and
reactive programming that resulted in the creation of ProAct.js
(http://proactjs.com), which is a library that augments the JavaScript
language and turns it into a reactive language.
He has been working with Java and related core technologies since
2005 to bring Java's benefits to manufacturing and logistic
companies.
Did you know that Packt offers eBook versions of every book
published, with PDF and ePub files available? You can upgrade to
the eBook version at www.PacktPub.com and as a print book
customer, you are entitled to a discount on the eBook copy. Get in
touch with us at <service@packtpub.com> for more details.
https://www2.packtpub.com/books/subscription/packtlib
Why subscribe?
Fully searchable across every book published by Packt
Copy and paste, print, and bookmark content
On demand and accessible via a web browser
Free access for Packt account holders
If you have an account with Packt at www.PacktPub.com, you can
use this to access PacktLib today and view 9 entirely free books.
Simply use your login credentials for immediate access.
Preface
Reactive programming has been around for decades. There has
been a few implementations of reactive programming from the time
Smalltalk was a young language. However, it has only become
popular recently and it is now becoming a trend. Why now you ask?
Because it is good for writing fast, real-time applications and current
technologies and the Web demand this.
I got involved in it back in 2008, when the team I was part of was
developing a multimedia book creator called Sophie 2. It had to be
fast and responsive so we created a framework called Prolib, which
provided objects with properties which could depend on each other
(in other words, we implemented bindings for Swing and much more
—transformations, filtering, and so on). It felt natural to wire the
model data to the GUI like this.
Of course, this was far away from the functional-like approach that
comes with RX. In 2010, Microsoft released RX and, after that,
Netflix ported it to Java—RxJava. However, Netflix released RxJava
to the open source community and the project became a huge
success. Many other languages have their port of RX and many
alternatives to it. Now, you can code using reactive programming on
your Java backend and wire it to your RxJava's frontend.
Observable
.just('R', 'x', 'J', 'a', 'v', 'a')
.subscribe(
System.out::print,
System.err::println,
System.out::println
);
New terms and important words are shown in bold. Words that you
see on the screen, for example, in menus or dialog boxes, appear in
the text like this: "Interfaces of this type are called functional
interfaces."
Note
Warnings or important notes appear in a box like this.
Tip
Tips and tricks appear like this.
Reader feedback
Feedback from our readers is always welcome. Let us know what
you think about this book—what you liked or disliked. Reader
feedback is important for us as it helps us develop titles that you will
really get the most out of.
If there is a topic that you have expertise in and you are interested in
either writing or contributing to a book, see our author guide at
www.packtpub.com/authors.
Customer support
Now that you are the proud owner of a Packt book, we have a
number of things to help you to get the most from your purchase.
Errata
Although we have taken every care to ensure the accuracy of our
content, mistakes do happen. If you find a mistake in one of our
books—maybe a mistake in the text or the code—we would be
grateful if you could report this to us. By doing so, you can save
other readers from frustration and help us improve subsequent
versions of this book. If you find any errata, please report them by
visiting http://www.packtpub.com/submit-errata, selecting your book,
clicking on the Errata Submission Form link, and entering the
details of your errata. Once your errata are verified, your submission
will be accepted and the errata will be uploaded to our website or
added to any list of existing errata under the Errata section of that
title.
Piracy
Another Random Document on
Scribd Without Any Related Topics
In the seventh chapter of Elsie Venner, Oliver Wendell Holmes makes
the Reverend Mr. Honeywood lay aside an old sermon on Human
Nature, and write one on The Obligations of an infinite Creator to a
finite Creature. A. J. F. Behrends grounded our Lord's representative
relation not in his human nature but in his divine nature. “He is our
representative not because he was in the loins of Adam, but because
we, Adam included, were in his loins. Personal created existence is
grounded in the Logos, so that God must deal with him as well as
with every individual sinner, and sin and guilt and punishment must
smite the Logos as well as the sinner, and that, whether the sinner is
saved or not. This is not, as is often charged, a denial of grace or of
freedom in grace, for it is no denial of freedom or grace to show that
they are eternally rational and conformable to eternal law. In the
ideal sphere, necessity and freedom, law and grace, coalesce.” J. C.
C. Clarke, Man and his Divine Father, 387—“Vicarious atonement
does not consist in any single act.... No one act embraces it all, and
no one definition can compass it.” In this sense we may adopt the
words of Forsyth: “In the atonement the Holy Father dealt with a
world's sin on (not in) a world-soul.”
[pg 757]
If Christ had been born into the world by ordinary
generation, he too would have had depravity, guilt,
penalty. But he was not so born. In the womb of the
Virgin, the human nature which he took was purged
from its depravity. But this purging away of depravity
did not take away guilt, or penalty. There was still
left the just exposure to the penalty of violated law.
Although Christ's nature was purified, his obligation
to suffer yet remained. He might have declined to
join himself to humanity, and then he need not have
suffered. He might have sundered his connection
with the race, and then he need not have suffered.
But once born of the Virgin, once possessed of the
human nature that was under the curse, he was
bound to suffer. The whole mass and weight of God's
displeasure against the race fell on him, when once
he became a member of the race.
Because Christ is essential humanity, the universal man, the life of
the race, he is the central brain to which and through which all ideas
must pass. He is the central heart to which and through which all
pains must be communicated. You cannot telephone to your friend
across the town without first ringing up the central office. You
cannot injure your neighbor without first injuring Christ. Each one of
us can say of him: “Against thee, thee only, have I sinned” (Ps.
51:4). Because of his central and all-inclusive humanity, he must
bear in his own person all the burdens of humanity, and must be
“the Lamb of God, that” taketh, and so “taketh away, the sin of the
world” (John 1:29). Simms Reeves, the great English tenor, said that
the passion-music was too much for him; he was found completely
overcome after singing the prophet's words in Lam. 1:12—“Is it
nothing to you, all ye that pass by? Behold, and see if there be any
sorrow like unto my sorrow, which is brought upon me, Wherewith
Jehovah hath afflicted me in the day of his fierce anger.”
Father Damien gave his life in ministry to the lepers' colony of the
Hawaiian Islands. Though free from the disease when he entered,
he was at last himself stricken with the leprosy, and then wrote: “I
must now stay with my own people.” Once a leper, there was no
release. When Christ once joined himself to humanity, all the
exposures and liabilities of humanity fell upon him. Through himself
personally without sin, he was made sin for us. Christ inherited guilt
and penalty. Heb. 2:14, 15—“Since then the children are sharers in
flesh and blood, he also himself in like manner partook of the same;
that through death he might bring to naught him that had the power
of death, that is, the devil; and might deliver all them who through
fear of death were all their life-time subject to bondage.”
Only God can forgive sin, because only God can feel it in its true
heinousness and rate it at its true worth. Christ could forgive sin
because he added to the divine feeling with regard to sin the
anguish of a pure humanity on account of it. Shelley, Julian and
Maddolo: “Me, whose heart a stranger's tear might wear, As water-
drops the sandy fountain-stone; Me, who am as a nerve o'er which
do creep The Else unfelt oppressions of the earth.” S. W. Culver:
“We cannot be saved, as we are taught geometry, by lecture and
diagram. No person ever yet saved another from drowning by
standing coolly by and telling him the importance of rising to the
surface and the necessity of respiration. No, he must plunge into the
destructive element, and take upon himself the very condition of the
drowning man, and by the exertion of his own strength, by the vigor
of his own life, save him from the impending death. When your child
is encompassed by the flames that consume your dwelling, you will
not save him by calling to him from without. You must make your
way through the devouring flame, till you come personally into the
very conditions of his peril and danger, and, thence returning, bear
him forth to freedom and safety.”
1. That of Isaac Watts (see Bib. Sac., 1875:421). This holds that the
humanity of Christ, both in body and soul, preëxisted before the
incarnation, and was manifested to the patriarchs. We reply that
Christ's human nature is declared to be derived from the Virgin.
5. That the humanity of Christ was not a new creation, but was
derived from Adam, through Mary his mother; so that Christ, so far
as his humanity was concerned, was in Adam just as we were, and
had the same race-responsibility with ourselves. As Adam's
descendant, he was responsible for Adam's sin, like every other
member of the race; the chief difference being, that while we inherit
from Adam both guilt and depravity, he whom the Holy Spirit
purified, inherited not the depravity, but only the guilt. Christ took to
himself, not sin (depravity), but the consequences of sin. In him
there was abolition of sin, without abolition of obligation to suffer for
sin; while in the believer, there is abolition of obligation to suffer,
without abolition of sin itself.
The fault of all the analogies just mentioned is that they are purely
commercial. A transference of pecuniary obligation is easier to
understand than a transference of criminal liability. I cannot justly
bear another's penalty, unless I can in some way share his guilt. The
theory we advocate shows how such a sharing of our guilt on the
part of Christ was possible. All believers in substitution hold that
Christ bore our guilt: “My soul looks back to see The burdens thou
didst bear When hanging on the accursed tree, And hopes her guilt
was there.” But we claim that, by virtue of Christ's union with
humanity, that guilt was not only an imputed, but also an imparted,
guilt.
This suffering in and with the sins of men, which Dr. Bushnell
emphasized so strongly, though it is not, as he thought, the principal
element, is notwithstanding an indispensable element in the
atonement of Christ. Suffering in and with the sinner is one way,
though not the only way, in which Christ is enabled to bear the
wrath of God which constitutes the real penalty of sin.
For a good exposition of 2 Cor. 5:21, Gal. 3:13, and Rom. 3:25, 26,
see Denney, Studies in Theology, 109-124.
Melanchthon: “Christ was made sin for us, not only in respect to
punishment, but primarily by being chargeable with guilt also (culpæ
et reatus)”—quoted by Thomasius, Christi Person und Werk, 3:95,
102, 103, 107; also 1:307, 314 sq. Thomasius says that “Christ bore
the guilt of the race by imputation; but as in the case of the
imputation of Adam's sin to us, imputation of our sins to Christ
presupposes a real relationship. Christ appropriated our sin. He sank
himself into our guilt.” Dorner, Glaubenslehre, 2:442 (Syst. Doct.,
3:350, 351), agrees with Thomasius, that “Christ entered into our
natural mortality, which for us is a penal condition, and into the state
of collective guilt, so far as it is an evil, a burden to be borne; not
that he had personal guilt, but rather that he entered into our guilt-
laden common life, not as a stranger, but as one actually belonging
to it—put under its law, according to the will of the Father and of his
own love.”
When, and how, did Christ take this guilt and this penalty upon him?
With regard to penalty, we have no difficulty in answering that, as
his whole life of suffering was propitiatory, so penalty rested upon
him from the very beginning of his life. This penalty was inherited,
and was the consequence of Christ's taking human nature (Gal. 4:4,
5—“born of a woman, born under the law”). But penalty and guilt
are correlates; if Christ inherited penalty, it must have been because
he inherited guilt. This subjection to the common guilt of the race
was intimated in Jesus' circumcision (Luke 2:21); in his ritual
purification (Luke 2:22—“their purification”—i. e., the purification of
Mary and the babe; see Lange, Life of Christ; Commentaries of
Alford, Webster and Wilkinson; and An. Par. Bible); in his legal
redemption (Luke 2:23, 24; cf. Ex. 13:2, 13); and in his baptism
(Mat. 3:15—“thus it becometh us to fulfill all righteousness”). The
baptized person went [pg 762]down into the water, as one laden
with sin and guilt, in order that this sin and guilt might be buried
forever, and that he might rise from the typical grave to a new and
holy life. (Ebrard: “Baptism = death.”) So Christ's submission to
John's baptism of repentance was not only a consecration to death,
but also a recognition and confession of his implication in that guilt
of the race for which death was the appointed and inevitable penalty
(cf. Mat. 10:38; Luke 12:50; Mat. 26:39); and, as his baptism was a
prefiguration of his death, we may learn from his baptism something
with regard to the meaning of his death. See further, under The
Symbolism of Baptism.
As one who had had guilt, Christ was “justified in the spirit” (1 Tim.
3:16); and this justification appears to have taken place after he
“was manifested in the flesh” (1 Tim. 3:16), and when “he was
raised for our justification” (Rom. 4:25). Compare Rom. 1:4
—“declared to be the Son of God with power, according to the spirit
of holiness, by the resurrection from the dead”; 6:7-10—“he that
hath died is justified from sin. But if we died with Christ, we believe
that we shall also live with him; knowing that Christ being raised
from the dead dieth no more; death no more hath dominion over
him. For the death that he died, he died unto sin once; but the life
that he liveth, he liveth unto God”—here all Christians are conceived
of as ideally justified in the justification of Christ, when Christ died
for our sins and rose again. 8:3—“God, sending his own Son in the
likeness of sinful flesh and for sin, condemned sin in the flesh”—here
Meyer says: “The sending does not precede the condemnation; but
the condemnation is effected in and with the sending.” John 16:10
—“of righteousness, because I go to the Father”; 19:30—“It is
finished.” On 1 Tim. 3:16, see the Commentary of Bengel.
[pg 763]
“If you could imagine a finger endowed with free will and trying to
sunder its connection with the body by tying a string around itself,
you would have a picture of man trying to sunder his connection
with Christ. What is the result of such an attempt? Why, pain, decay;
possible, nay, incipient death, to the finger. By what law? By the law
of the organism, which is so constituted as to maintain itself against
its own disruption by the revolt of the members. The pain and death
of the finger is the reaction of the whole against the treason of the
part. The finger suffers pain. But are there no results of pain to the
body? Does not the body feel pain also? How plain it is that no such
pain can be confined to the single part! The heart feels, aye, the
whole organism feels, because all the parts are members one of
another. It not only suffers, but that suffering tends to remedy the
evil and to remove its cause. The body summons its forces, pours
new tides of life into the dying member, strives to rid the finger of
the ligature that binds it. So through all the course of history, Christ,
the natural life of the race, has been afflicted in the affliction of
humanity and has suffered for human sin. This suffering has been an
atoning suffering, since it has been due to righteousness. If God had
not been holy, if God had not made all nature express the holiness of
his being, if God had not made pain and loss the necessary
consequences of sin, then Christ would not have suffered. But since
these things are sin's penalty and Christ is the life of the sinful race,
it must needs be that Christ should suffer. There is nothing arbitrary
in laying upon him the iniquities of us all. Original grace, like original
sin, is only the ethical interpretation of biological facts.” See also
Ames, on Biological Aspects of the Atonement, in Methodist Review,
Nov. 1905:943-953.
[pg 764]
(a) It rests upon correct philosophical principles with
regard to the nature of will, law, sin, penalty,
righteousness.
Acts 17:3—“it behooved the Christ to suffer, and to rise again from
the dead”—lit.: “it was necessary for the Christ to suffer”; Luke
24:26—“Behooved it not the Christ to suffer these things, and to
enter into his glory?”—lit.: “Was it not necessary that the Christ
should suffer these things?” It is not enough to say that Christ must
suffer in order that the prophecies might be fulfilled. Why was it
prophesied that he should suffer? Why did God purpose that he
should suffer? The ultimate necessity is a necessity in the nature of
God.
Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.
ebookname.com