c12 JSE XML JAXB JDK11 9 ReactiveStreams
c12 JSE XML JAXB JDK11 9 ReactiveStreams
c12 JSE XML JAXB JDK11 9 ReactiveStreams
presentation
Java Programming – Software App Development
Cristian Toma
1 2 3
XML + XSD +
XPath / XSLT
Java XML &
JSON
Programming
Exchange
Ideas
1
XML, XSD, X-Path / XSLT
http://www.w3schools.com | http://www.w3schools.com/xml/default.ASP
1. XML Concepts
XML Concepts:
http://www.w3schools.com | http://www.w3schools.com/xml/default.ASP
1. XML Concepts
XML Concepts:
http://www.w3schools.com | http://www.w3schools.com/xml/default.ASP
1. XML Concepts
XML Concepts – What about JSON?:
http://www.w3schools.com | http://www.w3schools.com/xml/default.ASP
1. XML Concepts
XML Concepts:
http://www.w3schools.com | http://www.w3schools.com/xml/default.ASP
1. XML Concepts
XML Concepts:
In XML, it is illegal to omit the closing tag. All elements must have a closing tag:
<p>This is a paragraph.</p>
<br />
XML tags are case sensitive. The tag <Letter> is different from the tag <letter>.
Opening and closing tags must be written with the same case:
<Message>This is incorrect</message>
<message>This is correct</message>
Note: "Opening and closing tags" are often referred to as "Start and end tags"
http://www.w3schools.com | http://www.w3schools.com/xml/default.ASP
1. XML Concepts
XML Concepts – Synthax:
XML Elements Must be Properly Nested
In XML, all elements must be properly nested within each other:
In the example above, "Properly nested" simply means that since the <i> element is
opened inside the <b> element, it must be closed inside the <b> element.
<root>
<child>
<subchild>.....</subchild>
</child>
</root>
http://www.w3schools.com | http://www.w3schools.com/xml/default.ASP
1. XML Concepts
XML Concepts – Synthax:
<note date=12/11/2007>
<to>Tove</to>
<from>Jani</from>
</note>
<note date="12/11/2007">
<to>Tove</to>
<from>Jani</from>
</note>
The error in the first document is that the date attribute in the note element is not quoted.
http://www.w3schools.com | http://www.w3schools.com/xml/default.ASP
1. XML Concepts
XML Concepts – Synthax:
Entity References
Some characters have a special meaning in XML.
If you place a character like "<" inside an XML element, it will generate an error because the parser
interprets it as the start of a new element.
To avoid this error, replace the "<" character with an entity reference:
<message>if salary < 1000 then</message>
Comments in XML
The syntax for writing comments in XML is similar to that of
HTML.
<!-- This is a comment -->
In the example above, <bookstore> and <book> have element contents, because they
contain other elements. <book> also has an attribute (category="CHILDREN"). <title>,
<author>, <year>, and <price> have text content because they contain text.
http://www.w3schools.com | http://www.w3schools.com/xml/default.ASP
1. XML Concepts
XML Concepts - Namespace:
Name Conflicts
In XML, element names are defined by the developer. This often results in a conflict when
trying to mix XML documents from different XML applications.
This XML carries HTML table information:
<table>
<tr>
<td>Apples</td>
<td>Bananas</td>
</tr>
</table>
http://www.w3schools.com | http://www.w3schools.com/xml/default.ASP
1. XML Concepts
XML Path: is a language for finding information in an XML document.
But wait…
There’s More!
3. Java 9+ and 11 Reactive Streams
Java 9 Reactive Streams
Reactive Streams is about asynchronous processing of stream, so there should be
a Publisher and a Subscriber. The Publisher publishes the stream of data and the Subscriber
consumes the data.
Sometimes it is necessary to transform the data between Publisher and Subscriber. Processor is the
entity sitting between the end publisher and subscriber to transform the data received from publisher
so that subscriber can understand it. It is possible to have a chain of processors. Processor works both
as Subscriber and a Publisher.
Copyright: https://www.journaldev.com/20723/java-9-reactive-streams
3. Java 9+ and 11 Reactive Streams
The new Flow API in Java 9 follows
the Reactive Streams
Specification and is aligned with the
paradigms of the Reactive Manifesto:
Responsive, Resilient, Elastic,
Message Driven.
Copyright:
https://www.hascode.com/2018/01/reactive-
streams-java-9-flow-api-rxjava-and-reactor-
examples/
3. Java 9+ and 11 Reactive Streams
Java 9 Flow API Classes and Interfaces
public final class Flow {
private Flow() {} // uninstantiable
@FunctionalInterface
public static interface Publisher<T> {
public void subscribe(Subscriber<? super T> subscriber);
}
public static interface Subscriber<T> {
public void onSubscribe(Subscription subscription);
public void onNext(T item);
public void onError(Throwable throwable);
public void onComplete();
}
public static interface Subscription {
public void request(long n);
public void cancel();
}
public static interface Processor<T,R> extends Subscriber<T>,
Publisher<R> {}
}
Copyright: https://dzone.com/articles/reactive-streams-in-java-9
3. Java 9+ and 11 Reactive Streams
Java 9 Flow API Classes and Interfaces
Java 9 Flow API implements the Reactive Streams Specification. Flow API is a combination
of Iterator and Observer pattern. Iterator works on pull model where application pulls items from the
source, whereas Observer works on push model and reacts when item is pushed from source to application.
Flow API classes and interfaces:
java.util.concurrent.Flow: This is the main class of Flow API. This class encapsulates all the important
interfaces of the Flow API. This is a final class and it can’t be extend.
java.util.concurrent.Flow.Publisher: This is a functional interface and every publisher has to implement it’s
subscribe method to add the given subscriber to receive messages.
java.util.concurrent.Flow.Subscriber: Every subscriber has to implement this interface. The methods in the
subscriber are invoked in strict sequential order. There are four methods in this interface:
onSubscribe: This is the first method to get invoked when subscriber is subscribed to receive messages
by publisher. Usually we invoke subscription.request to start receiving items from processor.
onNext: This method gets invoked when an item is received from publisher, this is where we
implement our business logic to process the stream and then request for more data from publisher.
onError: This method is invoked when an irrecoverable error occurs, we can do cleanup taks in this
method, such as closing database connection.
onComplete: This is like finally method and gets invoked when no other items are being produced by
publisher and publisher is closed. We can use it to send notification of successful processing of stream.
Copyright: https://www.journaldev.com/20723/java-9-reactive-streams
3. Java 9+ and 11 Reactive Streams
Java 9 Flow API Classes and Interfaces
Copyright: https://www.journaldev.com/20723/java-9-reactive-streams
3. Java 9+ and 11 Reactive Streams
Reactive Streams - Publishers, Subscribers, and Subscriptions
Instead of having a client and server style of data handling, where a client requests data from a server and
the server responds, possibly asynchronously, to the client with the requested data, we instead use a
publish-subscribe mechanism: A subscriber informs a publisher that it is willing to accept a given number
of items (requests a given number of items), and if items are available, the publisher pushes the maximum
receivable number of items to the subscriber. It is important to note that this is a two-way communication,
where the subscriber informs the publisher how many items it is willing to handle and the publisher
pushes that number of items to the subscriber.
The process of restricting the number of items that a subscriber is willing to accept (as judged by the
subscriber itself) is called backpressure and is essential in prohibiting the overloading of the subscriber
(pushing more items that the subscriber can handle). This scheme is illustrated in the figure below.
When publisher is producing messages in much faster rate than it’s being consumed by subscriber, back
pressure gets built. Java 9 Flow API doesn’t provide for the moment any mechanism to signal about back
pressure or to deal with it. But we can devise our own strategy to deal with it, such as fine tuning the
subscriber or reducing the message producing rate.
Copyright: https://dzone.com/articles/reactive-streams-in-java-9
3. Java 9+ and 11 Reactive Streams
Reactive Streams - Publishers,
Subscribers, and Subscriptions
Copyright: https://dzone.com/articles/reactive-streams-in-java-9
3. Java 9+ and 11 Reactive Streams
Reactive Streams - Publishers, Subscribers, and Subscriptions
It is important to note that there are two theoretical approaches to streaming data to a subscriber:
(1) the subscription holds the items or
(2) the publisher holds the items.
In the first case, the publisher pushes items to the subscription when they become available; when, at a later time, the subscriber
requests n items, the subscription provides n or fewer items it has previously been given by the publisher. This may be used when the
publisher manages queued items, such as incoming HTTP requests.
In the second case, the subscriber forwards requests to the publisher, which pushes n or fewer items to the subscription, which in turn
pushes those same items to the subscriber. This scenario may be more suitable for instances were items are generated as needed, such as
with a prime number generator.
For example, suppose a subscriber requests 5 items and 7 are currently available in the publisher. The outstanding demand for the subscriber
is 5 so 5 of the 7 items are pushed to the subscriber. The remaining 2 items are maintained by the publisher, awaiting the subscriber to
request more items. If the subscriber then requests 10 more items, the 2 remaining items are pushed to the subscriber, resulting in an
outstanding demand of 8. If 5 more items become available in the publisher, these 5 items are pushed to the subscriber, leaving an
outstanding demand of 3. The outstanding demand will remain at 3 unless the subscriber requests n more items, in which case the
outstanding demand will increase to 3 + n, or more i items are pushed to the subscriber, in which case the outstanding demand will decrease
to 3 - i (to a minimum of 0).
If an entity is both a publisher and a subscriber, it is called a processor. A processor commonly acts as an intermediary between another
publisher and subscriber (either of which may be another processor), performing some transformation on the stream of data. For example, a
processor can be created that filters out items that match some criteria before passing them onto its subscriber. A visual representation of a
processor is illustrated in the figure below.
Copyright: https://dzone.com/articles/reactive-streams-in-java-9
What’s
Thanks!Your Message?
Java SE
End of Lecture 12 – XML | JSON Programming