Akka Java Documentation Release 2.2.4
Total Page:16
File Type:pdf, Size:1020Kb
Akka Java Documentation Release 2.2.4 Typesafe Inc March 04, 2014 CONTENTS 1 Introduction 1 1.1 What is Akka?............................................1 1.2 Why Akka?..............................................3 1.3 Getting Started............................................3 1.4 The Obligatory Hello World.....................................7 1.5 Use-case and Deployment Scenarios.................................8 1.6 Examples of use-cases for Akka...................................9 2 General 10 2.1 Terminology, Concepts........................................ 10 2.2 Actor Systems............................................ 12 2.3 What is an Actor?.......................................... 14 2.4 Supervision and Monitoring..................................... 16 2.5 Actor References, Paths and Addresses............................... 19 2.6 Location Transparency........................................ 25 2.7 Akka and the Java Memory Model.................................. 26 2.8 Message Delivery Guarantees.................................... 28 2.9 Configuration............................................. 33 3 Actors 65 3.1 Actors................................................ 65 3.2 Typed Actors............................................. 84 3.3 Fault Tolerance............................................ 88 3.4 Dispatchers.............................................. 103 3.5 Mailboxes.............................................. 106 3.6 Routing................................................ 111 3.7 Building Finite State Machine Actors................................ 126 3.8 Testing Actor Systems........................................ 129 4 Futures and Agents 143 4.1 Futures................................................ 143 4.2 Software Transactional Memory................................... 151 4.3 Agents................................................ 152 4.4 Transactors.............................................. 154 5 Networking 158 5.1 Cluster Specification......................................... 158 5.2 Cluster Usage............................................. 166 5.3 Remoting............................................... 193 5.4 Serialization............................................. 213 5.5 I/O.................................................. 219 5.6 Encoding and decoding binary data................................. 221 5.7 Using TCP.............................................. 229 5.8 Using UDP.............................................. 242 5.9 ZeroMQ............................................... 245 i 5.10 Camel................................................. 250 6 Utilities 268 6.1 Event Bus............................................... 268 6.2 Logging................................................ 271 6.3 Scheduler............................................... 276 6.4 Duration............................................... 279 6.5 Circuit Breaker............................................ 280 6.6 Akka Extensions........................................... 283 6.7 Durable Mailboxes.......................................... 286 6.8 Microkernel............................................. 289 7 HowTo: Common Patterns 293 7.1 Scheduling Periodic Messages.................................... 293 7.2 Single-Use Actor Trees with High-Level Error Reporting...................... 294 7.3 Template Pattern........................................... 297 8 Experimental Modules 298 8.1 Multi Node Testing.......................................... 298 8.2 External Contributions........................................ 303 9 Information for Akka Developers 323 9.1 Building Akka............................................ 323 9.2 Multi JVM Testing.......................................... 325 9.3 I/O Layer Design........................................... 329 9.4 Developer Guidelines........................................ 330 9.5 Documentation Guidelines...................................... 332 9.6 Team................................................. 334 10 Project Information 335 10.1 Migration Guides........................................... 335 10.2 Issue Tracking............................................ 341 10.3 Licenses............................................... 342 10.4 Sponsors............................................... 342 10.5 Project................................................ 342 11 Additional Information 345 11.1 Books................................................. 345 11.2 Here is a list of recipes for all things Akka............................. 345 11.3 Other Language Bindings...................................... 345 11.4 Akka in OSGi............................................ 345 11.5 Incomplete List of HTTP Frameworks................................ 346 ii CHAPTER ONE INTRODUCTION 1.1 What is Akka? Scalable real-time transaction processing We believe that writing correct concurrent, fault-tolerant and scalable applications is too hard. Most of the time it’s because we are using the wrong tools and the wrong level of abstraction. Akka is here to change that. Using the Actor Model we raise the abstraction level and provide a better platform to build correct, concurrent, and scalable applications. For fault-tolerance we adopt the “Let it crash” model which the telecom industry has used with great success to build applications that self-heal and systems that never stop. Actors also provide the abstraction for transparent distribution and the basis for truly scalable and fault-tolerant applications. Akka is Open Source and available under the Apache 2 License. Download from http://typesafe.com/stack/downloads/akka/ Please note that all code samples compile, so if you want direct access to the sources, have a look over at the Akka Docs Project. 1.1.1 Akka implements a unique hybrid Actors Actors give you: • Simple and high-level abstractions for concurrency and parallelism. • Asynchronous, non-blocking and highly performant event-driven programming model. • Very lightweight event-driven processes (approximately 2.7 million actors per GB RAM). See Actors (Scala) and Untyped Actors (Java) Fault Tolerance • Supervisor hierarchies with “let-it-crash” semantics. • Supervisor hierarchies can span over multiple JVMs to provide truly fault-tolerant systems. • Excellent for writing highly fault-tolerant systems that self-heal and never stop. See Fault Tolerance (Scala) and Fault Tolerance (Java) Location Transparency Everything in Akka is designed to work in a distributed environment: all interactions of actors use pure message passing and everything is asynchronous. For an overview of the remoting see Location Transparency 1 Akka Java Documentation, Release 2.2.4 Transactors Transactors combine actors and Software Transactional Memory (STM) into transactional actors. It allows you to compose atomic message flows with automatic retry and rollback. See Transactors (Scala) and Transactors (Java) 1.1.2 Scala and Java APIs Akka has both a scala-api and a Java Documentation. 1.1.3 Akka can be used in two different ways • As a library: used by a web app, to be put into WEB-INF/lib or as a regular JAR on your classpath. • As a microkernel: stand-alone kernel to drop your application into. See the Use-case and Deployment Scenarios for details. 1.1.4 What happened to Cloudy Akka? The commercial offering was earlier referred to as Cloudy Akka. This offering consisted of two things: • Cluster support for Akka • Monitoring & Management (formerly called Atmos) Cloudy Akka has been discontinued and the Cluster support is now being moved into the Open Source version of Akka (the upcoming Akka 2.1), while Monitoring & Management (Atmos) has been re-branded as the Typesafe Console, which is now part of the commercial subscription for the Typesafe Stack (see below for details). 1.1.5 Typesafe Stack Akka is now also part of the Typesafe Stack. The Typesafe Stack is a modern software platform that makes it easy for developers to build scalable software applications. It combines the Scala programming language, Akka, the Play! web framework and robust developer tools in a simple package that integrates seamlessly with existing Java infrastructure. The Typesafe Stack is all fully open source. 1.1.6 Typesafe Console On top of the Typesafe Stack we also have a commercial product called Typesafe Console which provides the following features: 1. Slick Web UI with real-time view into the system 2. Management through Dashboard, JMX and REST 3. Dapper-style tracing of messages across components and remote nodes 4. Real-time statistics 5. Very low overhead monitoring agents (should always be on in production) 6. Consolidation of statistics and logging information to a single node 7. Storage of statistics data for later processing 8. Provisioning and rolling upgrades Read more here. 1.1. What is Akka? 2 Akka Java Documentation, Release 2.2.4 1.2 Why Akka? 1.2.1 What features can the Akka platform offer, over the competition? Akka provides scalable real-time transaction processing. Akka is an unified runtime and programming model for: • Scale up (Concurrency) • Scale out (Remoting) • Fault tolerance One thing to learn and admin, with high cohesion and coherent semantics. Akka is a very scalable piece of software, not only in the context of performance but also in the size of applications it is useful for. The core of Akka, akka-actor, is very small and easily dropped into an existing project where you need asynchronicity and lockless concurrency without hassle. You can choose to include only the parts of akka