Akka - Home: DOAG e.V. · What are reactive applications? New requirements demand new technologies...

39
Akka Reactive Applications made easy JavaLand, 2014-03-25 Michael Pisula

Transcript of Akka - Home: DOAG e.V. · What are reactive applications? New requirements demand new technologies...

Page 1: Akka - Home: DOAG e.V. · What are reactive applications? New requirements demand new technologies - The Reactive Manifesto event-driven scalable resilient i n t e r a c v

AkkaReactive Applications made easy

JavaLand, 2014-03-25Michael Pisula

Page 2: Akka - Home: DOAG e.V. · What are reactive applications? New requirements demand new technologies - The Reactive Manifesto event-driven scalable resilient i n t e r a c v

Akka

Akka mountain by Arvelius

Page 3: Akka - Home: DOAG e.V. · What are reactive applications? New requirements demand new technologies - The Reactive Manifesto event-driven scalable resilient i n t e r a c v

What are reactive applications?New requirements demand new technologies -

The Reactive Manifesto

event-driven

scalable resilient

interactive

Page 4: Akka - Home: DOAG e.V. · What are reactive applications? New requirements demand new technologies - The Reactive Manifesto event-driven scalable resilient i n t e r a c v

What is Akka?actor programming model+ fault tolerance+ location transparency

Page 5: Akka - Home: DOAG e.V. · What are reactive applications? New requirements demand new technologies - The Reactive Manifesto event-driven scalable resilient i n t e r a c v

Actors

Page 6: Akka - Home: DOAG e.V. · What are reactive applications? New requirements demand new technologies - The Reactive Manifesto event-driven scalable resilient i n t e r a c v

Creating an Actor is easyclass Adder extends UntypedActor {

private int sum = 0;

@Override public void onReceive(Object message) throws Exception { if (message instanceof Add) { sum += ((Add)message).value; } else if (message instanceof GetSum) { getSender().tell(new Sum(sum), getSelf()); } }

}

Page 7: Akka - Home: DOAG e.V. · What are reactive applications? New requirements demand new technologies - The Reactive Manifesto event-driven scalable resilient i n t e r a c v

Working with ActorsActorSystem system = ActorSystem.create("AdderSystem");ActorRef add = system.actorOf(Props.create(Adder.class));add.tell(new Add(1), null);

Code

Page 8: Akka - Home: DOAG e.V. · What are reactive applications? New requirements demand new technologies - The Reactive Manifesto event-driven scalable resilient i n t e r a c v

Props Props props1 = Props.create(SimpleCalc.class); Props props2 = Props.create(SimpleCalc.class, 5);

Page 9: Akka - Home: DOAG e.V. · What are reactive applications? New requirements demand new technologies - The Reactive Manifesto event-driven scalable resilient i n t e r a c v

PropsBest practice:

public static class Adder extends UntypedActor {

public static Props props(int initialValue) { return Props.create(Adder.class, initialValue); }

public Adder(int initialValue) { ... }

}...system.actorOf(Adder.props(42));

Page 10: Akka - Home: DOAG e.V. · What are reactive applications? New requirements demand new technologies - The Reactive Manifesto event-driven scalable resilient i n t e r a c v

Inside an ActorActor Incarnation

ActorRef

Mailbox ActorPathActorInstance

2

Page 11: Akka - Home: DOAG e.V. · What are reactive applications? New requirements demand new technologies - The Reactive Manifesto event-driven scalable resilient i n t e r a c v

Resilience

Eltern haften für ihre Kinder by Katharina Hamacher

Page 12: Akka - Home: DOAG e.V. · What are reactive applications? New requirements demand new technologies - The Reactive Manifesto event-driven scalable resilient i n t e r a c v

Creating a hierarchy is easy// top-level actorsystem.actorOf(Props.create(Adder.class));// child actorgetContext().actorOf(Props.create(Adder.class));

Page 13: Akka - Home: DOAG e.V. · What are reactive applications? New requirements demand new technologies - The Reactive Manifesto event-driven scalable resilient i n t e r a c v

The Akka Hierarchy

Page 14: Akka - Home: DOAG e.V. · What are reactive applications? New requirements demand new technologies - The Reactive Manifesto event-driven scalable resilient i n t e r a c v

Supervision strategiesOne For OneAll For One

Page 15: Akka - Home: DOAG e.V. · What are reactive applications? New requirements demand new technologies - The Reactive Manifesto event-driven scalable resilient i n t e r a c v

Supervision optionsResumeRestartStopEscalate

Page 16: Akka - Home: DOAG e.V. · What are reactive applications? New requirements demand new technologies - The Reactive Manifesto event-driven scalable resilient i n t e r a c v

Defining supervision is easy private SupervisorStrategy strategy = new OneForOneStrategy(10, Duration.create(5, TimeUnit.SECONDS), new Function() { @Override public Directive apply(Throwable t) throws Exception { return SupervisorStrategy.resume(); } }); @Override public SupervisorStrategy supervisorStrategy() { return strategy; }

Code

Page 17: Akka - Home: DOAG e.V. · What are reactive applications? New requirements demand new technologies - The Reactive Manifesto event-driven scalable resilient i n t e r a c v

Remoting & Clustering

Server room by Torkild Retvedt

Page 18: Akka - Home: DOAG e.V. · What are reactive applications? New requirements demand new technologies - The Reactive Manifesto event-driven scalable resilient i n t e r a c v

Configure the node first akka { actor { provider = "akka.remote.RemoteActorRefProvider" } remote { enabled-transports = ["akka.remote.netty.tcp"] netty.tcp { hostname = "127.0.0.1" port = 2552 } } }

Page 19: Akka - Home: DOAG e.V. · What are reactive applications? New requirements demand new technologies - The Reactive Manifesto event-driven scalable resilient i n t e r a c v

Remoting actors now is easy akka { actor { deployment { /echoActor { remote = "akka.tcp://[email protected]:2553" } } } }

Code

Page 20: Akka - Home: DOAG e.V. · What are reactive applications? New requirements demand new technologies - The Reactive Manifesto event-driven scalable resilient i n t e r a c v

Clustering akka { actor { provider = "akka.cluster.ClusterActorRefProvider" } remote { ... } cluster { seed-nodes = [ "akka.tcp://[email protected]:2551", "akka.tcp://[email protected]:2552"]

auto-down-unreachable-after = 10s } }

Code

Page 21: Akka - Home: DOAG e.V. · What are reactive applications? New requirements demand new technologies - The Reactive Manifesto event-driven scalable resilient i n t e r a c v

Camel Integration

Page 22: Akka - Home: DOAG e.V. · What are reactive applications? New requirements demand new technologies - The Reactive Manifesto event-driven scalable resilient i n t e r a c v

Greeting Web Service public class GreetingService extends UntypedConsumerActor {

@Override public void onReceive(Object message) throws Exception { if (message instanceof CamelMessage) { CamelMessage camelMsg = (CamelMessage) message; Map headers = camelMsg.getHeaders(); getSender().tell("Hello " + headers.get("greetee"), getSelf()); } }

@Override public String getEndpointUri() { return "jetty:http://localhost:4242/greet"; } }

ExampleCode

Page 23: Akka - Home: DOAG e.V. · What are reactive applications? New requirements demand new technologies - The Reactive Manifesto event-driven scalable resilient i n t e r a c v
Page 25: Akka - Home: DOAG e.V. · What are reactive applications? New requirements demand new technologies - The Reactive Manifesto event-driven scalable resilient i n t e r a c v

Testing with scalatest class AdderSpec extends TestKit with ImplicitSender with WordSpec ... { ... "An Adder actor" must {

"return 3 as sum of 1 and 2" in { val greeter = system.actorOf(Props[Adder]) greeter ! new Add(1) greeter ! new Add(2) greeter ! new GetSum() expectMsg(new Sum(3)) } } }

Page 26: Akka - Home: DOAG e.V. · What are reactive applications? New requirements demand new technologies - The Reactive Manifesto event-driven scalable resilient i n t e r a c v

Testing with JUnit @Test public void add2to1() { ActorRef adder = system.actorOf(Props.create(Adder.class)); JavaTestKit probe = new JavaTestKit(system); adder.tell(new Adder.Add(1), probe.getRef()); adder.tell(new Adder.Add(2), probe.getRef()); adder.tell(new Adder.GetSum(), probe.getRef());

probe.expectMsgEquals(new Adder.Sum(3)); }

Page 27: Akka - Home: DOAG e.V. · What are reactive applications? New requirements demand new technologies - The Reactive Manifesto event-driven scalable resilient i n t e r a c v

TestActorRef"An Adder actor" must { "know the Answer to the Question of Life, the Universe, and Everything" in { val greeter = TestActorRef[Adder] greeter.underlyingActor.sum = 42 greeter ! new GetSum() expectMsg(new Sum(42)) }}

Code

Page 28: Akka - Home: DOAG e.V. · What are reactive applications? New requirements demand new technologies - The Reactive Manifesto event-driven scalable resilient i n t e r a c v

Persistence

Fossil by Yaffa Phillips

Page 29: Akka - Home: DOAG e.V. · What are reactive applications? New requirements demand new technologies - The Reactive Manifesto event-driven scalable resilient i n t e r a c v

Persisting state public class PersistentAdder extends UntypedProcessor { private int sum = 0;

@Override public void onReceive(Object message) throws Exception { if (message instanceof Persistent) { Object payload = ((Persistent) message).payload(); if (payload instanceof Add) { sum += Integer.parseInt(((Add) payload).value); } } } }

Code

Page 30: Akka - Home: DOAG e.V. · What are reactive applications? New requirements demand new technologies - The Reactive Manifesto event-driven scalable resilient i n t e r a c v

Guaranteed delivery public void onReceive(Object message){ if (message instanceof ConfirmablePersistent) { ((ConfirmablePersistent) message).confirm(); getSender().tell(((ConfirmablePersistent) message).payload(), getSelf()); } } ... ActorRef channel = system.actorOf(Channel.props()); channel.tell(Deliver.create( Persistent.create("Hello Echo!"), echoActor.path()), inbox.getRef());

Code

Page 31: Akka - Home: DOAG e.V. · What are reactive applications? New requirements demand new technologies - The Reactive Manifesto event-driven scalable resilient i n t e r a c v

Putting it together

Page 32: Akka - Home: DOAG e.V. · What are reactive applications? New requirements demand new technologies - The Reactive Manifesto event-driven scalable resilient i n t e r a c v

Actors are your building blocksKeep actor logic shortDo not block in actorsUse meaningful names

Page 33: Akka - Home: DOAG e.V. · What are reactive applications? New requirements demand new technologies - The Reactive Manifesto event-driven scalable resilient i n t e r a c v

Designing a systemActor systems are message-basedDesign patterns need to be message-basedEnterprise Integration Patterns to the rescue:

Pipes and FiltersScatter-GatherAggregator...

Page 34: Akka - Home: DOAG e.V. · What are reactive applications? New requirements demand new technologies - The Reactive Manifesto event-driven scalable resilient i n t e r a c v

Akka und Java 8

Page 35: Akka - Home: DOAG e.V. · What are reactive applications? New requirements demand new technologies - The Reactive Manifesto event-driven scalable resilient i n t e r a c v

Lambdafy your Actor public class MyActor extends AbstractActor {

@Override public PartialFunction receive() { return ReceiveBuilder. match(String.class, s -> { log.info("Received String message: {}", s); }). matchAny(o -> log.info("received unknown message")). build(); } }

Page 36: Akka - Home: DOAG e.V. · What are reactive applications? New requirements demand new technologies - The Reactive Manifesto event-driven scalable resilient i n t e r a c v

Learning Akka

Page 37: Akka - Home: DOAG e.V. · What are reactive applications? New requirements demand new technologies - The Reactive Manifesto event-driven scalable resilient i n t e r a c v

Typesafe ActivatorGreat tool to learn about AkkaMany templates exist, from Hello World to complex examples81 templates are currently available, >30 for Akka

Link

Page 38: Akka - Home: DOAG e.V. · What are reactive applications? New requirements demand new technologies - The Reactive Manifesto event-driven scalable resilient i n t e r a c v

BooksAkka Concurrency, Derek WyattAkka in Action, Raymond Roestenburg