SlideShare a Scribd company logo
Java NIO.2
Network socket
Network socket is an endpoint of a connection across a computer network.
Main types of Internet socket:
● Datagram sockets, which use User Datagram Protocol (UDP).
● Stream sockets, which use Transmission Control Protocol (TCP) or Stream
Control Transmission Protocol (SCTP).
● Raw sockets, typically available in routers and other network equipment.
Java IO vs NIO
IO NIO
Stream-oriented Buffer-oriented
Blocking I/O Non-blocking I/O
Selectors
Channels
Java.IO - Stream-oriented
Data Source Program
Data
Destination Program
001001001111001001001001011
001001001111001001001001011
Java.IO - Blocking I/O
Socket Thread
read data, block until ready
read data, block until ready
write data, block until ready
write data, block until ready
Java.IO — Work with data
// read from socket
Scanner sc = new Scanner(socket.getInputStream());
String string = sc.nextLine();
System.out.println("Received " + string);
// write to socket
PrintWriter pw = new PrintWriter(socket.getOutputStream());
pw.println("Hello");
Java.NIO — Buffer-oriented & Non-blocking I/O
Channel Buffer Thread
read data into buffer
fill data into buffer
check data in buffer
goto top
Java.NIO — Work with data
ByteBuffer readBuffer = ByteBuffer.allocate(1024);
// prepare to read
readBuffer.clear();
SocketChannel channel = getChannel();
channel.read(readBuffer);
readBuffer.flip();
// reading the buffer
// ...............…
ByteBuffer writeBuffer = ByteBuffer.allocate(1024);
// prepare to put data
writeBuffer.clear();
// putting the data
// ...............
// prepare to write
writeBuffer.flip();
channel.write(writeBuffer);
Java.NIO — Selector & Channels
Channel is a lightweight entity effeciently
transporting data between sockets as a tube.
Selector is a manager allowing a single thread to
monitor multiple input channels.
Java.IO - Classic IO server design
Server
Socket
Thread
Connection Thread
Connection Thread
Connection Thread
Connection Thread
Java.NIO — NIO server design
Thread
Selector
Channel Channel Channel Channel
Java.NIO — NIO server design
ServerSocketChannel channel = ServerSocketChannel.open();
channel.bind(new InetSocketAddress("localhost", 2222));
channel.configureBlocking(false);
Selector selector = Selector.open();
SelectionKey key = channel.register(selector, SelectionKey.OP_ACCEPT);
while (true) {
if (selector.select() == 0) {
Thread.sleep(1);
continue;
}
Set<SelectionKey> selectedKeys = selector.selectedKeys();
Iterator<SelectionKey> keyIterator = selectedKeys.iterator();
while (keyIterator.hasNext()) {
SelectionKey selectionKey = keyIterator.next();
if (selectionKey.isAcceptable()) {
// a connection was accepted by a ServerSocketChannel.
} else if (selectionKey.isConnectable()) {
// a connection was established with a remote.
} else if (selectionKey.isReadable()) {
// a channel is ready for reading
} else if (selectionKey.isWritable()) {
// a channel is ready for writing
}
keyIterator.remove();
}
}
Java NIO.1 vs NIO.2
NIO.1 NIO.2
Selector AsynchronousChannelGroup
ServerSocketChannel AsynchronousServerSocketChannel
SocketChannel AsynchronousSocketChannel
SelectionKey CompletionHandler
Java.NIO.2 — Reading
ByteBuffer readBuffer = ByteBuffer.allocate(1024);
// prepare to read
readBuffer.clear();
AsynchronousSocketChannel channel = getChannel2();
channel.read(readBuffer, null, new CompletionHandler<Integer, Object>() {
@Override
public void completed(Integer result, Object attachment) {
// buffer is ready for read
}
@Override
public void failed(Throwable exc, Object attachment) {
exc.printStackTrace();
}
});
Java.NIO.2 — Writing
ByteBuffer writeBuffer = ByteBuffer.allocate(1024);
// prepare to put data
writeBuffer.clear();
// putting data
// ...............
// prepare to write
writeBuffer.flip();
AsynchronousSocketChannel channel = getChannel2();
channel.write(writeBuffer, null, new CompletionHandler<Integer, Object>() {
@Override
public void completed(Integer result, Object attachment) {
// writing has been completed
}
@Override
public void failed(Throwable exc, Object attachment) {
exc.printStackTrace();
}
});
Java.NIO — NIO.1 server design
ServerSocketChannel channel = ServerSocketChannel.open();
channel.bind(new InetSocketAddress("localhost", 2222));
channel.configureBlocking(false);
Selector selector = Selector.open();
SelectionKey key = channel.register(selector, SelectionKey.OP_ACCEPT);
while (true) {
if (selector.select() == 0) {
Thread.sleep(1);
continue;
}
Set<SelectionKey> selectedKeys = selector.selectedKeys();
Iterator<SelectionKey> keyIterator = selectedKeys.iterator();
while (keyIterator.hasNext()) {
SelectionKey selectionKey = keyIterator.next();
if (selectionKey.isAcceptable()) {
// a connection was accepted by a ServerSocketChannel.
} else if (selectionKey.isConnectable()) {
// a connection was established with a remote.
} else if (selectionKey.isReadable()) {
// a channel is ready for reading
} else if (selectionKey.isWritable()) {
// a channel is ready for writing
}
keyIterator.remove();
}
}
Java.NIO — NIO.2 server design
AsynchronousChannelGroup group =
AsynchronousChannelGroup.withThreadPool(newFixedThreadPool(10));
AsynchronousServerSocketChannel channel = open(group);
channel.bind(new InetSocketAddress("localhost", 2222));
channel.accept(null, toHandler((client, attach) -> {
ByteBuffer readBuffer = ByteBuffer.allocate(1024);
ByteBuffer writeBuffer = ByteBuffer.allocate(1024);
client.read(readBuffer, null, toHandler((result, attachment) -> {
// buffer is ready for read
}));
client.write(writeBuffer, null, toHandler((result, attachment) -> {
// writing has been completed
}));
}));
public class NetworkServer {
AsynchronousServerSocketChannel channel;
CompletionHandler<AsynchronousSocketChannel, Void> handler;
public NetworkServer(SocketAddress address) throws IOException {
AsynchronousChannelGroup group = withFixedThreadPool(10, Thread::new);
handler = toHandler((channel, attach) -> processAccept(channel));
channel = AsynchronousServerSocketChannel.open(group);
channel.bind(address);
channel.accept(null, toHandler((clientChannel, attach) -> processAccept(clientChannel)));
}
private void processAccept(AsynchronousSocketChannel clientChannel) {
NetworkClient networkClient = new NetworkClient(clientChannel, message ->
out.println("Server: received: " + message));
networkClient.write("Hello! I'm NIO.2 server!n");
channel.accept(null, handler);
}
public void stop() throws IOException {
channel.close();
}
}
Java NIO.2 — Basic implementation of server
public class NetworkClient {
AtomicBoolean isWriting = new AtomicBoolean();
Deque<String> toWrite = new ConcurrentLinkedDeque<>();
Consumer<String> readFunction;
CompletionHandler<Integer, ByteBuffer> readHandler = toHandler((byteCount, buffer) -> finishRead(byteCount));
CompletionHandler<Integer, ByteBuffer> writeHandler = toHandler((byteCount, buffer) -> finishWrite());
ByteBuffer rb = allocateDirect(1024);
ByteBuffer wb = allocateDirect(1024);
AsynchronousSocketChannel channel;
public NetworkClient(AsynchronousSocketChannel channel, Consumer<String> readFunction) {
this.channel = channel;
this.readFunction = readFunction;
readNext();
}
private void finishRead(Integer byteCount) {
if(byteCount.equals(-1)) return;
readFunction.accept(NUtils.read(rb));
readNext();
}
private void finishWrite() {
if (isWriting.compareAndSet(true, false)) writeNext();
}
public void write(String message) {
toWrite.add(message);
if(isWriting.compareAndSet(false, true)) writeNext();
}
private void writeNext() {
if(toWrite.isEmpty()) return;
NUtils.write(wb, toWrite);
channel.write(wb, wb, writeHandler);
}
private void readNext() {
channel.read(rb, rb, readHandler);
}
}
Java NIO.2 — Basic implementation of server
new Thread(run(() -> {
final NetworkServer server = new NetworkServer(new InetSocketAddress(3333));
ConcurrentUtils.wait(counter);
server.stop();
})).start();
ThreadUtils.sleep(1000);
for (int i = 0, length = CLIENT_COUNT; i < length; i++) {
new Thread(run(() -> {
Socket socket = new Socket();
socket.connect(new InetSocketAddress(3333));
writeLine(socket, "Hello! I'm client " + currentThread().getName());
System.out.println("Client: received: " + readLine(socket));
synchronized (counter) {
if (counter.decrementAndGet() == 0) {
ConcurrentUtils.notifyAll(counter);
} else {
ConcurrentUtils.wait(counter);
}
}
socket.close();
})).start();
}
Java NIO.2 — Test
Java NIO.2 — Test
Server: received: Hello! I'm client Thread-16
Server: received: Hello! I'm client Thread-19
Server: received: Hello! I'm client Thread-11
Server: received: Hello! I'm client Thread-20
Server: received: Hello! I'm client Thread-15
Server: received: Hello! I'm client Thread-18
Server: received: Hello! I'm client Thread-12
Server: received: Hello! I'm client Thread-14
Server: received: Hello! I'm client Thread-13
Server: received: Hello! I'm client Thread-17
Client: received: Hello! I'm NIO.2 server!
Client: received: Hello! I'm NIO.2 server!
Client: received: Hello! I'm NIO.2 server!
Client: received: Hello! I'm NIO.2 server!
Client: received: Hello! I'm NIO.2 server!
Client: received: Hello! I'm NIO.2 server!
Client: received: Hello! I'm NIO.2 server!
Client: received: Hello! I'm NIO.2 server!
Client: received: Hello! I'm NIO.2 server!
Client: received: Hello! I'm NIO.2 server!
All thanks
Repository with my code examples:
https://bitbucket.org/JavaSabr/publictest

More Related Content

What's hot

Spring Boot and REST API
Spring Boot and REST APISpring Boot and REST API
Spring Boot and REST API
07.pallav
 
Spring Security
Spring SecuritySpring Security
Spring Security
Knoldus Inc.
 
Samba server configuration
Samba server configurationSamba server configuration
Samba server configuration
Rohit Phulsunge
 
Secure coding in C#
Secure coding in C#Secure coding in C#
Secure coding in C#
Siddharth Bezalwar
 
Java Course 8: I/O, Files and Streams
Java Course 8: I/O, Files and StreamsJava Course 8: I/O, Files and Streams
Java Course 8: I/O, Files and Streams
Anton Keks
 
Basics of Java Concurrency
Basics of Java ConcurrencyBasics of Java Concurrency
Basics of Java Concurrency
kshanth2101
 
Introduction au reseau informatique
 Introduction au reseau informatique Introduction au reseau informatique
Introduction au reseau informatique
Lycée Technique de BIKOK
 
Java Socket Programming
Java Socket ProgrammingJava Socket Programming
Java Socket Programming
Vipin Yadav
 
The linux networking architecture
The linux networking architectureThe linux networking architecture
The linux networking architecturehugo lu
 
Rapport Sockets en Java
Rapport Sockets en JavaRapport Sockets en Java
Rapport Sockets en Java
Soukaina Boujadi
 
Formation1 sockets
Formation1 socketsFormation1 sockets
Formation1 sockets
Mariem SOMRANI
 
Applets
AppletsApplets
Automating Network Infrastructure : Ansible
Automating Network Infrastructure : AnsibleAutomating Network Infrastructure : Ansible
Automating Network Infrastructure : Ansible
Bangladesh Network Operators Group
 
Loom Virtual Threads in the JDK 19
Loom Virtual Threads in the JDK 19Loom Virtual Threads in the JDK 19
Loom Virtual Threads in the JDK 19
José Paumard
 
Java IO
Java IOJava IO
Java IO
UTSAB NEUPANE
 
Xke spring boot
Xke spring bootXke spring boot
Xke spring boot
sourabh aggarwal
 
Java Threads and Concurrency
Java Threads and ConcurrencyJava Threads and Concurrency
Java Threads and Concurrency
Sunil OS
 

What's hot (20)

Spring Boot and REST API
Spring Boot and REST APISpring Boot and REST API
Spring Boot and REST API
 
Java I/O
Java I/OJava I/O
Java I/O
 
Nmap tutorial
Nmap tutorialNmap tutorial
Nmap tutorial
 
Spring Security
Spring SecuritySpring Security
Spring Security
 
Samba server configuration
Samba server configurationSamba server configuration
Samba server configuration
 
Secure coding in C#
Secure coding in C#Secure coding in C#
Secure coding in C#
 
Java Course 8: I/O, Files and Streams
Java Course 8: I/O, Files and StreamsJava Course 8: I/O, Files and Streams
Java Course 8: I/O, Files and Streams
 
Basics of Java Concurrency
Basics of Java ConcurrencyBasics of Java Concurrency
Basics of Java Concurrency
 
Introduction au reseau informatique
 Introduction au reseau informatique Introduction au reseau informatique
Introduction au reseau informatique
 
Apache ppt
Apache pptApache ppt
Apache ppt
 
Java Socket Programming
Java Socket ProgrammingJava Socket Programming
Java Socket Programming
 
The linux networking architecture
The linux networking architectureThe linux networking architecture
The linux networking architecture
 
Rapport Sockets en Java
Rapport Sockets en JavaRapport Sockets en Java
Rapport Sockets en Java
 
Formation1 sockets
Formation1 socketsFormation1 sockets
Formation1 sockets
 
Applets
AppletsApplets
Applets
 
Automating Network Infrastructure : Ansible
Automating Network Infrastructure : AnsibleAutomating Network Infrastructure : Ansible
Automating Network Infrastructure : Ansible
 
Loom Virtual Threads in the JDK 19
Loom Virtual Threads in the JDK 19Loom Virtual Threads in the JDK 19
Loom Virtual Threads in the JDK 19
 
Java IO
Java IOJava IO
Java IO
 
Xke spring boot
Xke spring bootXke spring boot
Xke spring boot
 
Java Threads and Concurrency
Java Threads and ConcurrencyJava Threads and Concurrency
Java Threads and Concurrency
 

Viewers also liked

JAVA NIO
JAVA NIOJAVA NIO
JAVA NIO
오석 한
 
Netty
NettyNetty
Netty
Jaejin Yun
 
Java 7 - short intro to NIO.2
Java 7 - short intro to NIO.2Java 7 - short intro to NIO.2
Java 7 - short intro to NIO.2
Martijn Verburg
 
IBM InterConnect: Java vs JavaScript for Enterprise WebApps
IBM InterConnect: Java vs JavaScript for Enterprise WebAppsIBM InterConnect: Java vs JavaScript for Enterprise WebApps
IBM InterConnect: Java vs JavaScript for Enterprise WebApps
Chris Bailey
 
Event loop
Event loopEvent loop
Event loop
codepitbull
 
java.io - fluxos (streams) e arquivos
java.io - fluxos (streams) e arquivosjava.io - fluxos (streams) e arquivos
java.io - fluxos (streams) e arquivos
Marcello Thiry
 
Java: Manipulação de Arquivos
Java:  Manipulação  de ArquivosJava:  Manipulação  de Arquivos
Java: Manipulação de Arquivos
Arthur Emanuel
 
Ficheiros em JAVA
Ficheiros em JAVAFicheiros em JAVA
Ficheiros em JAVA
Pedro De Almeida
 
IO In Java
IO In JavaIO In Java
IO In Javaparag
 
Apostila 8 sistema de arquivos
Apostila 8   sistema de arquivosApostila 8   sistema de arquivos
Apostila 8 sistema de arquivos
Paulo Fonseca
 

Viewers also liked (11)

JAVA NIO
JAVA NIOJAVA NIO
JAVA NIO
 
Netty
NettyNetty
Netty
 
Java 7 - short intro to NIO.2
Java 7 - short intro to NIO.2Java 7 - short intro to NIO.2
Java 7 - short intro to NIO.2
 
Nio2
Nio2Nio2
Nio2
 
IBM InterConnect: Java vs JavaScript for Enterprise WebApps
IBM InterConnect: Java vs JavaScript for Enterprise WebAppsIBM InterConnect: Java vs JavaScript for Enterprise WebApps
IBM InterConnect: Java vs JavaScript for Enterprise WebApps
 
Event loop
Event loopEvent loop
Event loop
 
java.io - fluxos (streams) e arquivos
java.io - fluxos (streams) e arquivosjava.io - fluxos (streams) e arquivos
java.io - fluxos (streams) e arquivos
 
Java: Manipulação de Arquivos
Java:  Manipulação  de ArquivosJava:  Manipulação  de Arquivos
Java: Manipulação de Arquivos
 
Ficheiros em JAVA
Ficheiros em JAVAFicheiros em JAVA
Ficheiros em JAVA
 
IO In Java
IO In JavaIO In Java
IO In Java
 
Apostila 8 sistema de arquivos
Apostila 8   sistema de arquivosApostila 8   sistema de arquivos
Apostila 8 sistema de arquivos
 

Similar to Java NIO.2

Stackless Python In Eve
Stackless Python In EveStackless Python In Eve
Stackless Python In Eve
guest91855c
 
Stackless Python In Eve
Stackless Python In EveStackless Python In Eve
Stackless Python In Evel xf
 
Embedded networks
Embedded networksEmbedded networks
Embedded networks
Gorkem Taral
 
Multithreading in Java
Multithreading in JavaMultithreading in Java
Multithreading in Java
Appsterdam Milan
 
Socket & Server Socket
Socket & Server SocketSocket & Server Socket
Socket & Server Socket
Hemant Chetwani
 
GDG Jakarta Meetup - Streaming Analytics With Apache Beam
GDG Jakarta Meetup - Streaming Analytics With Apache BeamGDG Jakarta Meetup - Streaming Analytics With Apache Beam
GDG Jakarta Meetup - Streaming Analytics With Apache Beam
Imre Nagi
 
Server side JavaScript: going all the way
Server side JavaScript: going all the wayServer side JavaScript: going all the way
Server side JavaScript: going all the way
Oleg Podsechin
 
Java Programming - 08 java threading
Java Programming - 08 java threadingJava Programming - 08 java threading
Java Programming - 08 java threading
Danairat Thanabodithammachari
 
forwarder.java.txt java forwarder class waits for an in.docx
forwarder.java.txt java forwarder class waits for an in.docxforwarder.java.txt java forwarder class waits for an in.docx
forwarder.java.txt java forwarder class waits for an in.docx
budbarber38650
 
Socket Programming - nitish nagar
Socket Programming - nitish nagarSocket Programming - nitish nagar
Socket Programming - nitish nagar
Nitish Nagar
 
Java Programming - 07 java networking
Java Programming - 07 java networkingJava Programming - 07 java networking
Java Programming - 07 java networking
Danairat Thanabodithammachari
 
DCCN 2016 - Tutorial 2 - 4G for SmartGrid ecosystem
DCCN 2016 - Tutorial 2 - 4G for SmartGrid ecosystemDCCN 2016 - Tutorial 2 - 4G for SmartGrid ecosystem
DCCN 2016 - Tutorial 2 - 4G for SmartGrid ecosystem
rudndccn
 
rtnetlink
rtnetlinkrtnetlink
rtnetlink
Taku Fukushima
 
Network programming in Java
Network programming in JavaNetwork programming in Java
Network programming in Java
Tushar B Kute
 
Tornado Web Server Internals
Tornado Web Server InternalsTornado Web Server Internals
Tornado Web Server InternalsPraveen Gollakota
 

Similar to Java NIO.2 (20)

Stackless Python In Eve
Stackless Python In EveStackless Python In Eve
Stackless Python In Eve
 
Stackless Python In Eve
Stackless Python In EveStackless Python In Eve
Stackless Python In Eve
 
Embedded networks
Embedded networksEmbedded networks
Embedded networks
 
Sockets
SocketsSockets
Sockets
 
Multithreading in Java
Multithreading in JavaMultithreading in Java
Multithreading in Java
 
Java Concurrency
Java ConcurrencyJava Concurrency
Java Concurrency
 
Socket & Server Socket
Socket & Server SocketSocket & Server Socket
Socket & Server Socket
 
GDG Jakarta Meetup - Streaming Analytics With Apache Beam
GDG Jakarta Meetup - Streaming Analytics With Apache BeamGDG Jakarta Meetup - Streaming Analytics With Apache Beam
GDG Jakarta Meetup - Streaming Analytics With Apache Beam
 
Server side JavaScript: going all the way
Server side JavaScript: going all the wayServer side JavaScript: going all the way
Server side JavaScript: going all the way
 
Lecture10
Lecture10Lecture10
Lecture10
 
Java sockets
Java socketsJava sockets
Java sockets
 
Java Programming - 08 java threading
Java Programming - 08 java threadingJava Programming - 08 java threading
Java Programming - 08 java threading
 
forwarder.java.txt java forwarder class waits for an in.docx
forwarder.java.txt java forwarder class waits for an in.docxforwarder.java.txt java forwarder class waits for an in.docx
forwarder.java.txt java forwarder class waits for an in.docx
 
Socket Programming - nitish nagar
Socket Programming - nitish nagarSocket Programming - nitish nagar
Socket Programming - nitish nagar
 
分散式系統
分散式系統分散式系統
分散式系統
 
Java Programming - 07 java networking
Java Programming - 07 java networkingJava Programming - 07 java networking
Java Programming - 07 java networking
 
DCCN 2016 - Tutorial 2 - 4G for SmartGrid ecosystem
DCCN 2016 - Tutorial 2 - 4G for SmartGrid ecosystemDCCN 2016 - Tutorial 2 - 4G for SmartGrid ecosystem
DCCN 2016 - Tutorial 2 - 4G for SmartGrid ecosystem
 
rtnetlink
rtnetlinkrtnetlink
rtnetlink
 
Network programming in Java
Network programming in JavaNetwork programming in Java
Network programming in Java
 
Tornado Web Server Internals
Tornado Web Server InternalsTornado Web Server Internals
Tornado Web Server Internals
 

More from *instinctools

ERP Customization for TV Services & Media Company
ERP Customization for TV Services & Media CompanyERP Customization for TV Services & Media Company
ERP Customization for TV Services & Media Company
*instinctools
 
Integration Of Data Visualization Tools In Odoo: Pros And Cons.pdf
Integration Of Data Visualization Tools In Odoo: Pros And Cons.pdfIntegration Of Data Visualization Tools In Odoo: Pros And Cons.pdf
Integration Of Data Visualization Tools In Odoo: Pros And Cons.pdf
*instinctools
 
Examples of custom intuitive dashboards in Odoo.pdf
Examples of custom intuitive dashboards in Odoo.pdfExamples of custom intuitive dashboards in Odoo.pdf
Examples of custom intuitive dashboards in Odoo.pdf
*instinctools
 
CRM FOR MARKETING COMPANY
CRM FOR MARKETING COMPANYCRM FOR MARKETING COMPANY
CRM FOR MARKETING COMPANY
*instinctools
 
BI Technologies and ECM-System For A Multi-Industry Corporation
BI Technologies and ECM-System For A Multi-Industry CorporationBI Technologies and ECM-System For A Multi-Industry Corporation
BI Technologies and ECM-System For A Multi-Industry Corporation
*instinctools
 
How to protect sensitive data
How to protect sensitive dataHow to protect sensitive data
How to protect sensitive data
*instinctools
 
Video streaming trends & technologies
Video streaming trends & technologiesVideo streaming trends & technologies
Video streaming trends & technologies
*instinctools
 
Happy Programmer's day | 2021 | *instinctools in numbers
Happy Programmer's day | 2021 | *instinctools in numbersHappy Programmer's day | 2021 | *instinctools in numbers
Happy Programmer's day | 2021 | *instinctools in numbers
*instinctools
 
Data Integration: Huntflow and PowerBI | Case Study | Software Development Co...
Data Integration: Huntflow and PowerBI | Case Study | Software Development Co...Data Integration: Huntflow and PowerBI | Case Study | Software Development Co...
Data Integration: Huntflow and PowerBI | Case Study | Software Development Co...
*instinctools
 
Top software development trends of 2021
Top software development trends of 2021Top software development trends of 2021
Top software development trends of 2021
*instinctools
 
6 hidden costs of cloud migration
6 hidden costs of cloud migration6 hidden costs of cloud migration
6 hidden costs of cloud migration
*instinctools
 
Learning management system
Learning management systemLearning management system
Learning management system
*instinctools
 
P2P trading platform - Blockchain solution for electricity provider
P2P trading platform - Blockchain solution for electricity providerP2P trading platform - Blockchain solution for electricity provider
P2P trading platform - Blockchain solution for electricity provider
*instinctools
 
Java Memory Model
Java Memory ModelJava Memory Model
Java Memory Model
*instinctools
 
Business Analysis in IT
Business Analysis in ITBusiness Analysis in IT
Business Analysis in IT
*instinctools
 
Electron. Build cross platform desktop apps with web technologies!
Electron. Build cross platform desktop apps with web technologies!Electron. Build cross platform desktop apps with web technologies!
Electron. Build cross platform desktop apps with web technologies!
*instinctools
 
Videostream compression in iOS
Videostream compression in iOSVideostream compression in iOS
Videostream compression in iOS
*instinctools
 
Apple Watch (Part 2)
Apple Watch (Part 2)Apple Watch (Part 2)
Apple Watch (Part 2)
*instinctools
 
Apple Watch (Part 1)
Apple Watch (Part 1)Apple Watch (Part 1)
Apple Watch (Part 1)
*instinctools
 
Viper architecture
Viper architectureViper architecture
Viper architecture
*instinctools
 

More from *instinctools (20)

ERP Customization for TV Services & Media Company
ERP Customization for TV Services & Media CompanyERP Customization for TV Services & Media Company
ERP Customization for TV Services & Media Company
 
Integration Of Data Visualization Tools In Odoo: Pros And Cons.pdf
Integration Of Data Visualization Tools In Odoo: Pros And Cons.pdfIntegration Of Data Visualization Tools In Odoo: Pros And Cons.pdf
Integration Of Data Visualization Tools In Odoo: Pros And Cons.pdf
 
Examples of custom intuitive dashboards in Odoo.pdf
Examples of custom intuitive dashboards in Odoo.pdfExamples of custom intuitive dashboards in Odoo.pdf
Examples of custom intuitive dashboards in Odoo.pdf
 
CRM FOR MARKETING COMPANY
CRM FOR MARKETING COMPANYCRM FOR MARKETING COMPANY
CRM FOR MARKETING COMPANY
 
BI Technologies and ECM-System For A Multi-Industry Corporation
BI Technologies and ECM-System For A Multi-Industry CorporationBI Technologies and ECM-System For A Multi-Industry Corporation
BI Technologies and ECM-System For A Multi-Industry Corporation
 
How to protect sensitive data
How to protect sensitive dataHow to protect sensitive data
How to protect sensitive data
 
Video streaming trends & technologies
Video streaming trends & technologiesVideo streaming trends & technologies
Video streaming trends & technologies
 
Happy Programmer's day | 2021 | *instinctools in numbers
Happy Programmer's day | 2021 | *instinctools in numbersHappy Programmer's day | 2021 | *instinctools in numbers
Happy Programmer's day | 2021 | *instinctools in numbers
 
Data Integration: Huntflow and PowerBI | Case Study | Software Development Co...
Data Integration: Huntflow and PowerBI | Case Study | Software Development Co...Data Integration: Huntflow and PowerBI | Case Study | Software Development Co...
Data Integration: Huntflow and PowerBI | Case Study | Software Development Co...
 
Top software development trends of 2021
Top software development trends of 2021Top software development trends of 2021
Top software development trends of 2021
 
6 hidden costs of cloud migration
6 hidden costs of cloud migration6 hidden costs of cloud migration
6 hidden costs of cloud migration
 
Learning management system
Learning management systemLearning management system
Learning management system
 
P2P trading platform - Blockchain solution for electricity provider
P2P trading platform - Blockchain solution for electricity providerP2P trading platform - Blockchain solution for electricity provider
P2P trading platform - Blockchain solution for electricity provider
 
Java Memory Model
Java Memory ModelJava Memory Model
Java Memory Model
 
Business Analysis in IT
Business Analysis in ITBusiness Analysis in IT
Business Analysis in IT
 
Electron. Build cross platform desktop apps with web technologies!
Electron. Build cross platform desktop apps with web technologies!Electron. Build cross platform desktop apps with web technologies!
Electron. Build cross platform desktop apps with web technologies!
 
Videostream compression in iOS
Videostream compression in iOSVideostream compression in iOS
Videostream compression in iOS
 
Apple Watch (Part 2)
Apple Watch (Part 2)Apple Watch (Part 2)
Apple Watch (Part 2)
 
Apple Watch (Part 1)
Apple Watch (Part 1)Apple Watch (Part 1)
Apple Watch (Part 1)
 
Viper architecture
Viper architectureViper architecture
Viper architecture
 

Recently uploaded

GOING AOT WITH GRAALVM FOR SPRING BOOT (SPRING IO)
GOING AOT WITH GRAALVM FOR  SPRING BOOT (SPRING IO)GOING AOT WITH GRAALVM FOR  SPRING BOOT (SPRING IO)
GOING AOT WITH GRAALVM FOR SPRING BOOT (SPRING IO)
Alina Yurenko
 
Pro Unity Game Development with C-sharp Book
Pro Unity Game Development with C-sharp BookPro Unity Game Development with C-sharp Book
Pro Unity Game Development with C-sharp Book
abdulrafaychaudhry
 
Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus
 
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Globus
 
A Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of PassageA Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of Passage
Philip Schwarz
 
Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"
Donna Lenk
 
Text-Summarization-of-Breaking-News-Using-Fine-tuning-BART-Model.pptx
Text-Summarization-of-Breaking-News-Using-Fine-tuning-BART-Model.pptxText-Summarization-of-Breaking-News-Using-Fine-tuning-BART-Model.pptx
Text-Summarization-of-Breaking-News-Using-Fine-tuning-BART-Model.pptx
ShamsuddeenMuhammadA
 
Quarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden ExtensionsQuarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden Extensions
Max Andersen
 
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing SuiteAI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
Google
 
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus
 
Automated software refactoring with OpenRewrite and Generative AI.pptx.pdf
Automated software refactoring with OpenRewrite and Generative AI.pptx.pdfAutomated software refactoring with OpenRewrite and Generative AI.pptx.pdf
Automated software refactoring with OpenRewrite and Generative AI.pptx.pdf
timtebeek1
 
Nidhi Software Price. Fact , Costs, Tips
Nidhi Software Price. Fact , Costs, TipsNidhi Software Price. Fact , Costs, Tips
Nidhi Software Price. Fact , Costs, Tips
vrstrong314
 
Enterprise Software Development with No Code Solutions.pptx
Enterprise Software Development with No Code Solutions.pptxEnterprise Software Development with No Code Solutions.pptx
Enterprise Software Development with No Code Solutions.pptx
QuickwayInfoSystems3
 
Utilocate provides Smarter, Better, Faster, Safer Locate Ticket Management
Utilocate provides Smarter, Better, Faster, Safer Locate Ticket ManagementUtilocate provides Smarter, Better, Faster, Safer Locate Ticket Management
Utilocate provides Smarter, Better, Faster, Safer Locate Ticket Management
Utilocate
 
Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024
Globus
 
A Study of Variable-Role-based Feature Enrichment in Neural Models of Code
A Study of Variable-Role-based Feature Enrichment in Neural Models of CodeA Study of Variable-Role-based Feature Enrichment in Neural Models of Code
A Study of Variable-Role-based Feature Enrichment in Neural Models of Code
Aftab Hussain
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
Safe Software
 
AI Genie Review: World’s First Open AI WordPress Website Creator
AI Genie Review: World’s First Open AI WordPress Website CreatorAI Genie Review: World’s First Open AI WordPress Website Creator
AI Genie Review: World’s First Open AI WordPress Website Creator
Google
 
Enhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdfEnhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdf
Globus
 
2024 eCommerceDays Toulouse - Sylius 2.0.pdf
2024 eCommerceDays Toulouse - Sylius 2.0.pdf2024 eCommerceDays Toulouse - Sylius 2.0.pdf
2024 eCommerceDays Toulouse - Sylius 2.0.pdf
Łukasz Chruściel
 

Recently uploaded (20)

GOING AOT WITH GRAALVM FOR SPRING BOOT (SPRING IO)
GOING AOT WITH GRAALVM FOR  SPRING BOOT (SPRING IO)GOING AOT WITH GRAALVM FOR  SPRING BOOT (SPRING IO)
GOING AOT WITH GRAALVM FOR SPRING BOOT (SPRING IO)
 
Pro Unity Game Development with C-sharp Book
Pro Unity Game Development with C-sharp BookPro Unity Game Development with C-sharp Book
Pro Unity Game Development with C-sharp Book
 
Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024
 
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
 
A Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of PassageA Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of Passage
 
Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"
 
Text-Summarization-of-Breaking-News-Using-Fine-tuning-BART-Model.pptx
Text-Summarization-of-Breaking-News-Using-Fine-tuning-BART-Model.pptxText-Summarization-of-Breaking-News-Using-Fine-tuning-BART-Model.pptx
Text-Summarization-of-Breaking-News-Using-Fine-tuning-BART-Model.pptx
 
Quarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden ExtensionsQuarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden Extensions
 
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing SuiteAI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
 
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024
 
Automated software refactoring with OpenRewrite and Generative AI.pptx.pdf
Automated software refactoring with OpenRewrite and Generative AI.pptx.pdfAutomated software refactoring with OpenRewrite and Generative AI.pptx.pdf
Automated software refactoring with OpenRewrite and Generative AI.pptx.pdf
 
Nidhi Software Price. Fact , Costs, Tips
Nidhi Software Price. Fact , Costs, TipsNidhi Software Price. Fact , Costs, Tips
Nidhi Software Price. Fact , Costs, Tips
 
Enterprise Software Development with No Code Solutions.pptx
Enterprise Software Development with No Code Solutions.pptxEnterprise Software Development with No Code Solutions.pptx
Enterprise Software Development with No Code Solutions.pptx
 
Utilocate provides Smarter, Better, Faster, Safer Locate Ticket Management
Utilocate provides Smarter, Better, Faster, Safer Locate Ticket ManagementUtilocate provides Smarter, Better, Faster, Safer Locate Ticket Management
Utilocate provides Smarter, Better, Faster, Safer Locate Ticket Management
 
Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024
 
A Study of Variable-Role-based Feature Enrichment in Neural Models of Code
A Study of Variable-Role-based Feature Enrichment in Neural Models of CodeA Study of Variable-Role-based Feature Enrichment in Neural Models of Code
A Study of Variable-Role-based Feature Enrichment in Neural Models of Code
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
 
AI Genie Review: World’s First Open AI WordPress Website Creator
AI Genie Review: World’s First Open AI WordPress Website CreatorAI Genie Review: World’s First Open AI WordPress Website Creator
AI Genie Review: World’s First Open AI WordPress Website Creator
 
Enhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdfEnhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdf
 
2024 eCommerceDays Toulouse - Sylius 2.0.pdf
2024 eCommerceDays Toulouse - Sylius 2.0.pdf2024 eCommerceDays Toulouse - Sylius 2.0.pdf
2024 eCommerceDays Toulouse - Sylius 2.0.pdf
 

Java NIO.2

  • 2. Network socket Network socket is an endpoint of a connection across a computer network. Main types of Internet socket: ● Datagram sockets, which use User Datagram Protocol (UDP). ● Stream sockets, which use Transmission Control Protocol (TCP) or Stream Control Transmission Protocol (SCTP). ● Raw sockets, typically available in routers and other network equipment.
  • 3. Java IO vs NIO IO NIO Stream-oriented Buffer-oriented Blocking I/O Non-blocking I/O Selectors Channels
  • 4. Java.IO - Stream-oriented Data Source Program Data Destination Program 001001001111001001001001011 001001001111001001001001011
  • 5. Java.IO - Blocking I/O Socket Thread read data, block until ready read data, block until ready write data, block until ready write data, block until ready
  • 6. Java.IO — Work with data // read from socket Scanner sc = new Scanner(socket.getInputStream()); String string = sc.nextLine(); System.out.println("Received " + string); // write to socket PrintWriter pw = new PrintWriter(socket.getOutputStream()); pw.println("Hello");
  • 7. Java.NIO — Buffer-oriented & Non-blocking I/O Channel Buffer Thread read data into buffer fill data into buffer check data in buffer goto top
  • 8. Java.NIO — Work with data ByteBuffer readBuffer = ByteBuffer.allocate(1024); // prepare to read readBuffer.clear(); SocketChannel channel = getChannel(); channel.read(readBuffer); readBuffer.flip(); // reading the buffer // ...............… ByteBuffer writeBuffer = ByteBuffer.allocate(1024); // prepare to put data writeBuffer.clear(); // putting the data // ............... // prepare to write writeBuffer.flip(); channel.write(writeBuffer);
  • 9. Java.NIO — Selector & Channels Channel is a lightweight entity effeciently transporting data between sockets as a tube. Selector is a manager allowing a single thread to monitor multiple input channels.
  • 10. Java.IO - Classic IO server design Server Socket Thread Connection Thread Connection Thread Connection Thread Connection Thread
  • 11. Java.NIO — NIO server design Thread Selector Channel Channel Channel Channel
  • 12. Java.NIO — NIO server design ServerSocketChannel channel = ServerSocketChannel.open(); channel.bind(new InetSocketAddress("localhost", 2222)); channel.configureBlocking(false); Selector selector = Selector.open(); SelectionKey key = channel.register(selector, SelectionKey.OP_ACCEPT); while (true) { if (selector.select() == 0) { Thread.sleep(1); continue; } Set<SelectionKey> selectedKeys = selector.selectedKeys(); Iterator<SelectionKey> keyIterator = selectedKeys.iterator(); while (keyIterator.hasNext()) { SelectionKey selectionKey = keyIterator.next(); if (selectionKey.isAcceptable()) { // a connection was accepted by a ServerSocketChannel. } else if (selectionKey.isConnectable()) { // a connection was established with a remote. } else if (selectionKey.isReadable()) { // a channel is ready for reading } else if (selectionKey.isWritable()) { // a channel is ready for writing } keyIterator.remove(); } }
  • 13. Java NIO.1 vs NIO.2 NIO.1 NIO.2 Selector AsynchronousChannelGroup ServerSocketChannel AsynchronousServerSocketChannel SocketChannel AsynchronousSocketChannel SelectionKey CompletionHandler
  • 14. Java.NIO.2 — Reading ByteBuffer readBuffer = ByteBuffer.allocate(1024); // prepare to read readBuffer.clear(); AsynchronousSocketChannel channel = getChannel2(); channel.read(readBuffer, null, new CompletionHandler<Integer, Object>() { @Override public void completed(Integer result, Object attachment) { // buffer is ready for read } @Override public void failed(Throwable exc, Object attachment) { exc.printStackTrace(); } });
  • 15. Java.NIO.2 — Writing ByteBuffer writeBuffer = ByteBuffer.allocate(1024); // prepare to put data writeBuffer.clear(); // putting data // ............... // prepare to write writeBuffer.flip(); AsynchronousSocketChannel channel = getChannel2(); channel.write(writeBuffer, null, new CompletionHandler<Integer, Object>() { @Override public void completed(Integer result, Object attachment) { // writing has been completed } @Override public void failed(Throwable exc, Object attachment) { exc.printStackTrace(); } });
  • 16. Java.NIO — NIO.1 server design ServerSocketChannel channel = ServerSocketChannel.open(); channel.bind(new InetSocketAddress("localhost", 2222)); channel.configureBlocking(false); Selector selector = Selector.open(); SelectionKey key = channel.register(selector, SelectionKey.OP_ACCEPT); while (true) { if (selector.select() == 0) { Thread.sleep(1); continue; } Set<SelectionKey> selectedKeys = selector.selectedKeys(); Iterator<SelectionKey> keyIterator = selectedKeys.iterator(); while (keyIterator.hasNext()) { SelectionKey selectionKey = keyIterator.next(); if (selectionKey.isAcceptable()) { // a connection was accepted by a ServerSocketChannel. } else if (selectionKey.isConnectable()) { // a connection was established with a remote. } else if (selectionKey.isReadable()) { // a channel is ready for reading } else if (selectionKey.isWritable()) { // a channel is ready for writing } keyIterator.remove(); } }
  • 17. Java.NIO — NIO.2 server design AsynchronousChannelGroup group = AsynchronousChannelGroup.withThreadPool(newFixedThreadPool(10)); AsynchronousServerSocketChannel channel = open(group); channel.bind(new InetSocketAddress("localhost", 2222)); channel.accept(null, toHandler((client, attach) -> { ByteBuffer readBuffer = ByteBuffer.allocate(1024); ByteBuffer writeBuffer = ByteBuffer.allocate(1024); client.read(readBuffer, null, toHandler((result, attachment) -> { // buffer is ready for read })); client.write(writeBuffer, null, toHandler((result, attachment) -> { // writing has been completed })); }));
  • 18. public class NetworkServer { AsynchronousServerSocketChannel channel; CompletionHandler<AsynchronousSocketChannel, Void> handler; public NetworkServer(SocketAddress address) throws IOException { AsynchronousChannelGroup group = withFixedThreadPool(10, Thread::new); handler = toHandler((channel, attach) -> processAccept(channel)); channel = AsynchronousServerSocketChannel.open(group); channel.bind(address); channel.accept(null, toHandler((clientChannel, attach) -> processAccept(clientChannel))); } private void processAccept(AsynchronousSocketChannel clientChannel) { NetworkClient networkClient = new NetworkClient(clientChannel, message -> out.println("Server: received: " + message)); networkClient.write("Hello! I'm NIO.2 server!n"); channel.accept(null, handler); } public void stop() throws IOException { channel.close(); } } Java NIO.2 — Basic implementation of server
  • 19. public class NetworkClient { AtomicBoolean isWriting = new AtomicBoolean(); Deque<String> toWrite = new ConcurrentLinkedDeque<>(); Consumer<String> readFunction; CompletionHandler<Integer, ByteBuffer> readHandler = toHandler((byteCount, buffer) -> finishRead(byteCount)); CompletionHandler<Integer, ByteBuffer> writeHandler = toHandler((byteCount, buffer) -> finishWrite()); ByteBuffer rb = allocateDirect(1024); ByteBuffer wb = allocateDirect(1024); AsynchronousSocketChannel channel; public NetworkClient(AsynchronousSocketChannel channel, Consumer<String> readFunction) { this.channel = channel; this.readFunction = readFunction; readNext(); } private void finishRead(Integer byteCount) { if(byteCount.equals(-1)) return; readFunction.accept(NUtils.read(rb)); readNext(); } private void finishWrite() { if (isWriting.compareAndSet(true, false)) writeNext(); } public void write(String message) { toWrite.add(message); if(isWriting.compareAndSet(false, true)) writeNext(); } private void writeNext() { if(toWrite.isEmpty()) return; NUtils.write(wb, toWrite); channel.write(wb, wb, writeHandler); } private void readNext() { channel.read(rb, rb, readHandler); } } Java NIO.2 — Basic implementation of server
  • 20. new Thread(run(() -> { final NetworkServer server = new NetworkServer(new InetSocketAddress(3333)); ConcurrentUtils.wait(counter); server.stop(); })).start(); ThreadUtils.sleep(1000); for (int i = 0, length = CLIENT_COUNT; i < length; i++) { new Thread(run(() -> { Socket socket = new Socket(); socket.connect(new InetSocketAddress(3333)); writeLine(socket, "Hello! I'm client " + currentThread().getName()); System.out.println("Client: received: " + readLine(socket)); synchronized (counter) { if (counter.decrementAndGet() == 0) { ConcurrentUtils.notifyAll(counter); } else { ConcurrentUtils.wait(counter); } } socket.close(); })).start(); } Java NIO.2 — Test
  • 21. Java NIO.2 — Test Server: received: Hello! I'm client Thread-16 Server: received: Hello! I'm client Thread-19 Server: received: Hello! I'm client Thread-11 Server: received: Hello! I'm client Thread-20 Server: received: Hello! I'm client Thread-15 Server: received: Hello! I'm client Thread-18 Server: received: Hello! I'm client Thread-12 Server: received: Hello! I'm client Thread-14 Server: received: Hello! I'm client Thread-13 Server: received: Hello! I'm client Thread-17 Client: received: Hello! I'm NIO.2 server! Client: received: Hello! I'm NIO.2 server! Client: received: Hello! I'm NIO.2 server! Client: received: Hello! I'm NIO.2 server! Client: received: Hello! I'm NIO.2 server! Client: received: Hello! I'm NIO.2 server! Client: received: Hello! I'm NIO.2 server! Client: received: Hello! I'm NIO.2 server! Client: received: Hello! I'm NIO.2 server! Client: received: Hello! I'm NIO.2 server!
  • 22. All thanks Repository with my code examples: https://bitbucket.org/JavaSabr/publictest