SlideShare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our User Agreement and Privacy Policy.
SlideShare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our Privacy Policy and User Agreement for details.
Successfully reported this slideshow.
Activate your 14 day free trial to unlock unlimited reading.
Why actor-based systems are the best for microservices
22.
Actors and messages
ActiveMQ message listener in Java
23.
Actors and messages
ActiveMQ message listener in Java (zoom-in)
public class Server implements MessageListener {
// …
public void onMessage(Message message) {
try {
TextMessage response = this.session.createTextMessage();
if (message instanceof TextMessage) {
TextMessage txtMsg = (TextMessage) message;
String messageText = txtMsg.getText();
response.setText(this.messageProtocol.handleProtocolMessage(messageText));
}
response.setJMSCorrelationID(message.getJMSCorrelationID());
this.replyProducer.send(message.getJMSReplyTo(), response);
} catch (JMSException e) {
}
}
// …
}
24.
Actors and messages
Actor-based (Akka) message listener in Scala
class CustomerService extends Actor with ActorLogging with Consumer {
val camel = CamelExtension(system)
camel.context.addComponent("activemq", ActiveMQComponent.activeMQComponent(
“tcp://localhost:61616”
))
def endpointUri = "activemq:topic:events"
def receive = {
case e: CamelMessage => {
sender() ! “SomeMessage”
}
}
}
25.
Actors and messages
Btw, some magic is provided by: Apache Camel
“The most unknown coolest library out there”
JM
26.
Actors and messages
Actors give you:
• Simple and high-level abstractions for distribution, concurrency and parallelism
• Asynchronous, non-blocking and highly performant message-driven programming model
• Very lightweight event-driven processes (several million actors per GB of heap memory)
29.
All combined… and more
Summary:
• It’s easy to build microservices from scratch using messaging patterns
• All these patterns are well-known, tested and documented
• Actor model gives very simple abstractions to process and send messages
• Bonus: all kinds of routing models, back-pressure, reliable queues, broadcasts