SlideShare a Scribd company logo
MILANO 1863
POLITECNICO
ØMQ - ZeroMQ
AN INTRODUCTION
Carlo Bernaschina – carlo.bernaschina@polimi.it
“We took a normal TCP socket, injected it with a mix of radioactive
isotopes stolen from a secret Soviet atomic research project,
bombarded it with 1950-era cosmic rays, and put it into the hands
of a drug-addled comic book author with a badly-disguised fetish
for bulging muscles clad in spandex. Yes, ZeroMQ sockets are the
world-saving superheroes of the networking world.”
History
HOW IT BEGAN
The original Ø meant:
• Zero Broker
• Zero Latency (as close as possible)
With time it also started meaning:
• Zero Administration
• Zero Cost
• Zero Waste
The Real History
WHAT Ø STANDS FOR
Let’s build a simple lock-stepped
Client Server communication.
Lock-Stepped Communication:
Each end of the communication sends a message in turn
REQ - REP
A BASIC EXAMPLE
Context ctx = ZMQ.context(1);
Socket socket = ctx.socket(ZMQ.REP);
socket.bind("tcp://*:5555");
while (true) {
byte[] req = socket.recv();
socket.send(req);
}
REQ - REP
A BASIC EXAMPLE (2)
Context ctx = ZMQ.context(1);
Socket socket = ctx.socket(ZMQ.REQ);
socket.connect("tcp://localhost:5555");
socket.send("hello".getBytes());
byte[] rep = socket.recv();
REQ - REP
A BASIC EXAMPLE (3)
In ZeroMQ all the IO operations are done in the
background by a thread pool.
A Context groups a set of sockets which share the
same pool.
ZMQ.Context ctx = ZMQ.context(1);
The parameter of the ZMQ.context Factory is the
number of threads active in the thread-pool.
(1 is a good default for most of the cases)
Context
HOW IS IO HANDLED?
In ZeroMQ Sockets are logical endpoints of a
communication system. They abstract different
physical connections and protocols.
Socket socket = ctx.socket(ZMQ.REP);
They can be of different types:
Socket
HOW TO SEND AND RECEIVE DATA?
• PUB
• SUB
• REQ
• REP
• ROUTER
• DEALER
• PUSH
• PULL
• PAIR
ZeroMQ socket types are agnostic w.r.t. the endpoint
responsible of initiating the communication, any socket type can
be a TCP Server or Client.
One socket should bind to one (or more) address.
socket.bind("tcp://*:5555");
The other should connect to it (or them).
socket.connect("tcp://localhost:5555");
General role of thumb the endpoint which is “static” should bind
the “dynamic” ones should connect.
Binding vs Connecting
WHO IS THE SERVER?
Each ZeroMQ socket type pair has its own rules
related to sending and receiving data. Different type
pairs allow developers to achieve different result.
The API to send and receive, though, are independent
from the socket type.
byte[] data = socket.recv();
socket.send(data);
Sending vs Receiving
HOW DO THEY COMMUNICATE?
ZeroMQ is a message based system even though it
can use stream based transport protocol like TCP.
The message on the wire follows a simple format:
• Length
• Content
Packet Format
HOW ARE THE DATA OF THE WIRE?
ZeroMQ packets can contain more than one Frame
each one following the [length | content] format.
Here is the default message envelope.
Frames & Envelops
CAN WE SEND DIFFERENT PARTS IN THE SAME MESSAGE?
A ZMsg is convenient Java Class which hides away the
complexity of decoding and the message envelope.
ZMsg msg = ZMsg.recvMsg(socket);
msg.send(socket);
Frames are managed as a stack and they can be
pushed/popped in/from the message.
Byte[] frame= msg.pop();
msg.push(frame);
ZMsg
IS THERE SOME HELP IN MANAGING COMPLEXITY?
ZeroMQ sockets identify each connected endpoint with
a unique identifier called Identity.
The message envelope can be extended to contain
one (or more) Identities.
They are stored before the “empty delimiter frame”.
Identifies
HOW CAN WE IDENTIFY THE ORIGIN OF A PACKET?
We will focus on Request Response pairs.
The Valid Combinations are the following:
• REQ to REP
• DEALER to REP
• REQ to ROUTER
• DEALER to ROUTER
• DEALER to DEALER
• ROUTER to ROUTER
Valid Socket Combinations
HOW CAN WE PUT BUILDING BLOCKS TOGETHER?
REQ Sockets are synchronous.
After connection/binding they can just send a
message, trying to receive in this state will produce an
exception.
Once one (and only one) message has been sent, they
can just receive a message, trying to send in this state
will produce an exception.
Once one (and only one) message has been received,
the sockets go back to its initial state.
REQ
REQUEST AND WAIT
REP Sockets are synchronous.
After connection/binding they can just receive a message,
trying to send in this state will produce an exception.
The Identity of the source endpoint is stored into an
internal state and not exposed to user code.
Once one (and only one) message has been received, the
sockets can just send a message, which will be sent to the
stored Identity.
Once one (and only one) message has been sent, the
sockets go back to its initial state.
REP
WAIT AND REPLY
DEALER Sockets are asynchronous.
After connection/binding they can send as many
messages as necessary.
Any number of messages can also be received, no
identity though is attached to them.
DEALER
REQUEST AND FORGET
ROUTER Sockets are asynchronous.
After connection/binding they can receive many messages.
Each message is enriched with and extra frame containing the
Identity of the source endpoint.
These sockets can send messages just to other endpoints from
which they received messages.
The messages which are going to be sent MUST contain an
initial frame with the Identity of the target endpoint.
ROUTER
ENREACH THE MESSAGE
DEALER – ROUTER Socket Pairs can be used to
orchestrate a classic Client - Server communication
channels with one (or more) clients and one servers.
Once the DEALER sends a message it can start waiting for
answers. The single server architecture guarantees the
origin of the message.
DEALER - ROUTER
ASYNCHRONOUS CLIENT SERVER COMMUNICATION
Once the ROUTER receives a
message it can store the Identity and
use it to forward the message back to
the correct endpoint when the answer
is ready. Meanwhile it can go on
waiting for other requests.
Download Source Code
https://github.com/zeromq/jeromq
Download the JAR file
http://central.maven.org/maven2/org/zeromq/jeromq
JeroMQ
A PURE JAVA IMPLEMENTATION
Let’s open our IDEs
DEMO TIME
PRACTICE NEVER HURTS
• http://zguide.zeromq.org
• https://github.com/zeromq/jeromq
Reference

More Related Content

Similar to ØMQ - An Introduction

ZeroMQ: Super Sockets - by J2 Labs
ZeroMQ: Super Sockets - by J2 LabsZeroMQ: Super Sockets - by J2 Labs
ZeroMQ: Super Sockets - by J2 LabsJames Dennis
 
Socket Programming
Socket ProgrammingSocket Programming
Socket Programming
CEC Landran
 
ZeroMQ - Sockets on steroids!
ZeroMQ - Sockets on steroids!ZeroMQ - Sockets on steroids!
ZeroMQ - Sockets on steroids!
Pedro Januário
 
Zmq in context of openstack
Zmq in context of openstackZmq in context of openstack
Zmq in context of openstack
Yatin Kumbhare
 
ZeroMQ
ZeroMQZeroMQ
Lecture 07
Lecture 07Lecture 07
Lecture 07
Anwal Mirza
 
Lindsay distributed geventzmq
Lindsay distributed geventzmqLindsay distributed geventzmq
Lindsay distributed geventzmqRobin Xiao
 
Of the variedtypes of IPC, sockets arout and awaythe foremostcommon..pdf
Of the variedtypes of IPC, sockets arout and awaythe foremostcommon..pdfOf the variedtypes of IPC, sockets arout and awaythe foremostcommon..pdf
Of the variedtypes of IPC, sockets arout and awaythe foremostcommon..pdf
anuradhasilks
 
Tossim intro
Tossim introTossim intro
Tossim intro
AmolThotam1
 
Tcp sockets
Tcp socketsTcp sockets
Tcp sockets
babak danyal
 
Mac protocol for wmn
Mac protocol for wmnMac protocol for wmn
Mac protocol for wmn
mmjalbiaty
 
Message queuing telemetry transport (mqtt) launch
Message queuing telemetry transport (mqtt) launchMessage queuing telemetry transport (mqtt) launch
Message queuing telemetry transport (mqtt) launch
Hamdamboy
 
Message queuing telemetry transport (mqtt) launch
Message queuing telemetry transport (mqtt) launchMessage queuing telemetry transport (mqtt) launch
Message queuing telemetry transport (mqtt) launch
Hamdamboy (함담보이)
 
Data link layer
Data link layer Data link layer
Data link layer
Mukesh Chinta
 
datalinklayermukesh
datalinklayermukeshdatalinklayermukesh
datalinklayermukesh
TamiratDejene1
 
The Onion Routing (TOR)
The Onion Routing (TOR)The Onion Routing (TOR)
The Onion Routing (TOR)
Amrit Khandelwal
 
CN_UNIT4.ppt ytutuim jykhjl fjghkhj gjjj
CN_UNIT4.ppt ytutuim jykhjl fjghkhj gjjjCN_UNIT4.ppt ytutuim jykhjl fjghkhj gjjj
CN_UNIT4.ppt ytutuim jykhjl fjghkhj gjjj
PRADEEPERUKULLA2
 
Cisco switching and spanning tree protocol (stp) basics
Cisco switching and spanning tree protocol (stp) basicsCisco switching and spanning tree protocol (stp) basics
Cisco switching and spanning tree protocol (stp) basics
IT Tech
 

Similar to ØMQ - An Introduction (20)

ZeroMQ: Super Sockets - by J2 Labs
ZeroMQ: Super Sockets - by J2 LabsZeroMQ: Super Sockets - by J2 Labs
ZeroMQ: Super Sockets - by J2 Labs
 
Socket Programming
Socket ProgrammingSocket Programming
Socket Programming
 
Python networking
Python networkingPython networking
Python networking
 
ZeroMQ - Sockets on steroids!
ZeroMQ - Sockets on steroids!ZeroMQ - Sockets on steroids!
ZeroMQ - Sockets on steroids!
 
Zmq in context of openstack
Zmq in context of openstackZmq in context of openstack
Zmq in context of openstack
 
ZeroMQ
ZeroMQZeroMQ
ZeroMQ
 
Lecture 07
Lecture 07Lecture 07
Lecture 07
 
Lindsay distributed geventzmq
Lindsay distributed geventzmqLindsay distributed geventzmq
Lindsay distributed geventzmq
 
Of the variedtypes of IPC, sockets arout and awaythe foremostcommon..pdf
Of the variedtypes of IPC, sockets arout and awaythe foremostcommon..pdfOf the variedtypes of IPC, sockets arout and awaythe foremostcommon..pdf
Of the variedtypes of IPC, sockets arout and awaythe foremostcommon..pdf
 
Tossim intro
Tossim introTossim intro
Tossim intro
 
Tcp sockets
Tcp socketsTcp sockets
Tcp sockets
 
Tcpsockets
TcpsocketsTcpsockets
Tcpsockets
 
Mac protocol for wmn
Mac protocol for wmnMac protocol for wmn
Mac protocol for wmn
 
Message queuing telemetry transport (mqtt) launch
Message queuing telemetry transport (mqtt) launchMessage queuing telemetry transport (mqtt) launch
Message queuing telemetry transport (mqtt) launch
 
Message queuing telemetry transport (mqtt) launch
Message queuing telemetry transport (mqtt) launchMessage queuing telemetry transport (mqtt) launch
Message queuing telemetry transport (mqtt) launch
 
Data link layer
Data link layer Data link layer
Data link layer
 
datalinklayermukesh
datalinklayermukeshdatalinklayermukesh
datalinklayermukesh
 
The Onion Routing (TOR)
The Onion Routing (TOR)The Onion Routing (TOR)
The Onion Routing (TOR)
 
CN_UNIT4.ppt ytutuim jykhjl fjghkhj gjjj
CN_UNIT4.ppt ytutuim jykhjl fjghkhj gjjjCN_UNIT4.ppt ytutuim jykhjl fjghkhj gjjj
CN_UNIT4.ppt ytutuim jykhjl fjghkhj gjjj
 
Cisco switching and spanning tree protocol (stp) basics
Cisco switching and spanning tree protocol (stp) basicsCisco switching and spanning tree protocol (stp) basics
Cisco switching and spanning tree protocol (stp) basics
 

Recently uploaded

Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Mind IT Systems
 
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, BetterWebinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
XfilesPro
 
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
 
Need for Speed: Removing speed bumps from your Symfony projects ⚡️
Need for Speed: Removing speed bumps from your Symfony projects ⚡️Need for Speed: Removing speed bumps from your Symfony projects ⚡️
Need for Speed: Removing speed bumps from your Symfony projects ⚡️
Łukasz Chruściel
 
Quarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden ExtensionsQuarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden Extensions
Max Andersen
 
OpenMetadata Community Meeting - 5th June 2024
OpenMetadata Community Meeting - 5th June 2024OpenMetadata Community Meeting - 5th June 2024
OpenMetadata Community Meeting - 5th June 2024
OpenMetadata
 
LORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOM
LORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOMLORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOM
LORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOM
lorraineandreiamcidl
 
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
 
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data AnalysisProviding Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
Globus
 
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
 
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptxTop Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
rickgrimesss22
 
Empowering Growth with Best Software Development Company in Noida - Deuglo
Empowering Growth with Best Software  Development Company in Noida - DeugloEmpowering Growth with Best Software  Development Company in Noida - Deuglo
Empowering Growth with Best Software Development Company in Noida - Deuglo
Deuglo Infosystem Pvt Ltd
 
Mobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona InfotechMobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona Infotech
Drona Infotech
 
E-commerce Application Development Company.pdf
E-commerce Application Development Company.pdfE-commerce Application Development Company.pdf
E-commerce Application Development Company.pdf
Hornet Dynamics
 
Game Development with Unity3D (Game Development lecture 3)
Game Development  with Unity3D (Game Development lecture 3)Game Development  with Unity3D (Game Development lecture 3)
Game Development with Unity3D (Game Development lecture 3)
abdulrafaychaudhry
 
Nidhi Software Price. Fact , Costs, Tips
Nidhi Software Price. Fact , Costs, TipsNidhi Software Price. Fact , Costs, Tips
Nidhi Software Price. Fact , Costs, Tips
vrstrong314
 
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
 
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
 
GraphSummit Paris - The art of the possible with Graph Technology
GraphSummit Paris - The art of the possible with Graph TechnologyGraphSummit Paris - The art of the possible with Graph Technology
GraphSummit Paris - The art of the possible with Graph Technology
Neo4j
 
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
 

Recently uploaded (20)

Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
 
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, BetterWebinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
 
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
 
Need for Speed: Removing speed bumps from your Symfony projects ⚡️
Need for Speed: Removing speed bumps from your Symfony projects ⚡️Need for Speed: Removing speed bumps from your Symfony projects ⚡️
Need for Speed: Removing speed bumps from your Symfony projects ⚡️
 
Quarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden ExtensionsQuarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden Extensions
 
OpenMetadata Community Meeting - 5th June 2024
OpenMetadata Community Meeting - 5th June 2024OpenMetadata Community Meeting - 5th June 2024
OpenMetadata Community Meeting - 5th June 2024
 
LORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOM
LORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOMLORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOM
LORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOM
 
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
 
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data AnalysisProviding Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
 
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)
 
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptxTop Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
 
Empowering Growth with Best Software Development Company in Noida - Deuglo
Empowering Growth with Best Software  Development Company in Noida - DeugloEmpowering Growth with Best Software  Development Company in Noida - Deuglo
Empowering Growth with Best Software Development Company in Noida - Deuglo
 
Mobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona InfotechMobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona Infotech
 
E-commerce Application Development Company.pdf
E-commerce Application Development Company.pdfE-commerce Application Development Company.pdf
E-commerce Application Development Company.pdf
 
Game Development with Unity3D (Game Development lecture 3)
Game Development  with Unity3D (Game Development lecture 3)Game Development  with Unity3D (Game Development lecture 3)
Game Development with Unity3D (Game Development lecture 3)
 
Nidhi Software Price. Fact , Costs, Tips
Nidhi Software Price. Fact , Costs, TipsNidhi Software Price. Fact , Costs, Tips
Nidhi Software Price. Fact , Costs, Tips
 
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
 
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
 
GraphSummit Paris - The art of the possible with Graph Technology
GraphSummit Paris - The art of the possible with Graph TechnologyGraphSummit Paris - The art of the possible with Graph Technology
GraphSummit Paris - The art of the possible with Graph Technology
 
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
 

ØMQ - An Introduction

  • 1. MILANO 1863 POLITECNICO ØMQ - ZeroMQ AN INTRODUCTION Carlo Bernaschina – carlo.bernaschina@polimi.it
  • 2. “We took a normal TCP socket, injected it with a mix of radioactive isotopes stolen from a secret Soviet atomic research project, bombarded it with 1950-era cosmic rays, and put it into the hands of a drug-addled comic book author with a badly-disguised fetish for bulging muscles clad in spandex. Yes, ZeroMQ sockets are the world-saving superheroes of the networking world.” History HOW IT BEGAN
  • 3. The original Ø meant: • Zero Broker • Zero Latency (as close as possible) With time it also started meaning: • Zero Administration • Zero Cost • Zero Waste The Real History WHAT Ø STANDS FOR
  • 4. Let’s build a simple lock-stepped Client Server communication. Lock-Stepped Communication: Each end of the communication sends a message in turn REQ - REP A BASIC EXAMPLE
  • 5. Context ctx = ZMQ.context(1); Socket socket = ctx.socket(ZMQ.REP); socket.bind("tcp://*:5555"); while (true) { byte[] req = socket.recv(); socket.send(req); } REQ - REP A BASIC EXAMPLE (2)
  • 6. Context ctx = ZMQ.context(1); Socket socket = ctx.socket(ZMQ.REQ); socket.connect("tcp://localhost:5555"); socket.send("hello".getBytes()); byte[] rep = socket.recv(); REQ - REP A BASIC EXAMPLE (3)
  • 7. In ZeroMQ all the IO operations are done in the background by a thread pool. A Context groups a set of sockets which share the same pool. ZMQ.Context ctx = ZMQ.context(1); The parameter of the ZMQ.context Factory is the number of threads active in the thread-pool. (1 is a good default for most of the cases) Context HOW IS IO HANDLED?
  • 8. In ZeroMQ Sockets are logical endpoints of a communication system. They abstract different physical connections and protocols. Socket socket = ctx.socket(ZMQ.REP); They can be of different types: Socket HOW TO SEND AND RECEIVE DATA? • PUB • SUB • REQ • REP • ROUTER • DEALER • PUSH • PULL • PAIR
  • 9. ZeroMQ socket types are agnostic w.r.t. the endpoint responsible of initiating the communication, any socket type can be a TCP Server or Client. One socket should bind to one (or more) address. socket.bind("tcp://*:5555"); The other should connect to it (or them). socket.connect("tcp://localhost:5555"); General role of thumb the endpoint which is “static” should bind the “dynamic” ones should connect. Binding vs Connecting WHO IS THE SERVER?
  • 10. Each ZeroMQ socket type pair has its own rules related to sending and receiving data. Different type pairs allow developers to achieve different result. The API to send and receive, though, are independent from the socket type. byte[] data = socket.recv(); socket.send(data); Sending vs Receiving HOW DO THEY COMMUNICATE?
  • 11. ZeroMQ is a message based system even though it can use stream based transport protocol like TCP. The message on the wire follows a simple format: • Length • Content Packet Format HOW ARE THE DATA OF THE WIRE?
  • 12. ZeroMQ packets can contain more than one Frame each one following the [length | content] format. Here is the default message envelope. Frames & Envelops CAN WE SEND DIFFERENT PARTS IN THE SAME MESSAGE?
  • 13. A ZMsg is convenient Java Class which hides away the complexity of decoding and the message envelope. ZMsg msg = ZMsg.recvMsg(socket); msg.send(socket); Frames are managed as a stack and they can be pushed/popped in/from the message. Byte[] frame= msg.pop(); msg.push(frame); ZMsg IS THERE SOME HELP IN MANAGING COMPLEXITY?
  • 14. ZeroMQ sockets identify each connected endpoint with a unique identifier called Identity. The message envelope can be extended to contain one (or more) Identities. They are stored before the “empty delimiter frame”. Identifies HOW CAN WE IDENTIFY THE ORIGIN OF A PACKET?
  • 15. We will focus on Request Response pairs. The Valid Combinations are the following: • REQ to REP • DEALER to REP • REQ to ROUTER • DEALER to ROUTER • DEALER to DEALER • ROUTER to ROUTER Valid Socket Combinations HOW CAN WE PUT BUILDING BLOCKS TOGETHER?
  • 16. REQ Sockets are synchronous. After connection/binding they can just send a message, trying to receive in this state will produce an exception. Once one (and only one) message has been sent, they can just receive a message, trying to send in this state will produce an exception. Once one (and only one) message has been received, the sockets go back to its initial state. REQ REQUEST AND WAIT
  • 17. REP Sockets are synchronous. After connection/binding they can just receive a message, trying to send in this state will produce an exception. The Identity of the source endpoint is stored into an internal state and not exposed to user code. Once one (and only one) message has been received, the sockets can just send a message, which will be sent to the stored Identity. Once one (and only one) message has been sent, the sockets go back to its initial state. REP WAIT AND REPLY
  • 18. DEALER Sockets are asynchronous. After connection/binding they can send as many messages as necessary. Any number of messages can also be received, no identity though is attached to them. DEALER REQUEST AND FORGET
  • 19. ROUTER Sockets are asynchronous. After connection/binding they can receive many messages. Each message is enriched with and extra frame containing the Identity of the source endpoint. These sockets can send messages just to other endpoints from which they received messages. The messages which are going to be sent MUST contain an initial frame with the Identity of the target endpoint. ROUTER ENREACH THE MESSAGE
  • 20. DEALER – ROUTER Socket Pairs can be used to orchestrate a classic Client - Server communication channels with one (or more) clients and one servers. Once the DEALER sends a message it can start waiting for answers. The single server architecture guarantees the origin of the message. DEALER - ROUTER ASYNCHRONOUS CLIENT SERVER COMMUNICATION Once the ROUTER receives a message it can store the Identity and use it to forward the message back to the correct endpoint when the answer is ready. Meanwhile it can go on waiting for other requests.
  • 21. Download Source Code https://github.com/zeromq/jeromq Download the JAR file http://central.maven.org/maven2/org/zeromq/jeromq JeroMQ A PURE JAVA IMPLEMENTATION
  • 22. Let’s open our IDEs DEMO TIME PRACTICE NEVER HURTS