• No results found

49Communicating with actors

In document Exploring Microservices (Page 54-57)

Message Behaviour

49Communicating with actors

tor defined for sending a message. For example, if we wanted to send a vocal greeting message to the actor we defined earlier then we can do it as follows.

actorRef.Tell(new VocalGreeting(“Hello”));

There may be times we don’t have an actor reference and on those occasions we’ll look up an actor by it’s address. In order to look up an actor, we need something capa- ble of providing references to other actors. This may be the actor system hosting the actor, or it may be the Context associated with a specific actor. In order to select the actor deployed earlier, we can use the actor system to select the actor by address.

var selection = actorSystem.ActorSelection(“actorA”);

In each of these cases, the actor system provides the root location from which actors will be retrieved, which for the actor system is directly beneath the user actor. If we’d a second actor deployed alongside our first, we could use our first actor reference as our anchor to other actor locations.

var selection = actorRef.ActorSelection(“../actorB”);

Once we’ve got an address, we can pass messages to it in the same way we do with an actor reference.

selection.Tell(new VocalGreeting(“Hello”));

Actors are designed to completely encapsulate any state to ensure that nothing out- side of the system is capable of mutating it. This ensures that Akka.Net retains full control over the processing stage, by only allowing one message to be processed at a time. This leaves all code thread-safe, but it adds difficulty to access the data. To access data from outside the system, we need to send a message that specifically requests the data to be sent back. Akka.Net provides another method which allows for request- reply scenarios to be used through the use of Ask. Ask is an asynchronous method designed to form a layer of abstraction over the top of the messaging which is required.

var response = await selection.Ask(new Wave());

As Ask is an asynchronous construct, in your code you’ll need to factor the length of time it takes to get a response. By default, Ask has a timeout of ten seconds within which the actor needs to respond to your initial request message, otherwise the request times out with an exception. It’s important to realise that your actor has no way of knowing that the sender is expecting a reply, and it’s down to how you, the developer, handle this scenario.

3.4.3 Summary

Messages form an integral part of the design of a system using Akka.Net and are the key to communication between multiple actors, or even other entities outside of the

look at techniques such as Event Sourcing and Domain Driven Design as a means of modeling certain interactions between actors. At this stage it’s likely that most actors will react to events or respond to commands.

Whilst the name message is used, Akka.Net doesn’t require anything special with regards to the design of a message and they can be .Net classes or structs. The only requirement when designing these messages is that they be immutable to ensure that the thread safety guarantees specified by Akka.Net can’t be broken anywhere within the application.

3.5

Summary

This chapter has covered the basics of creating the core components of an application built on top of the actor based concurrency provided by Akka.Net. The rest of the book focuses on how we can use these components and build on top of them to create more advanced applications which follow the traits specified by the reactive manifesto. In this chapter you’ve seen:

 How to define an actor and how each part relates to the actor model

 How to deploy that actor within your application

51 Summary

Developing applications in a reactive style ensures that the experience is always responsive. Akka.NET is a framework for building distributed, message-driven applications which are able to stay responsive for the user even in the face of failure or when faced with more users. It makes it easy for .NET developers to write applications which are able to react to changes in their environment.

Reactive Applications with Akka.NET begins with an overview of reactive and a sample application written in the reactive style. You'll learn concepts of the actor model and what these mean in a real-world reactive context. This hands-on book builds on fundamental concepts to teach you how to cre- ate reliable and resilient applications. You'll also learn useful Akka.NET features for building real-world applications. By the end of the book, you'll be able to look at a problem domain and understand how to create applications which are able to with- stand the demands of a modern day application.

What's inside:

 Real-world applications of Akka.NET and reactive applications

 Designing an Internet of Things architecture with reactive in mind

 Building applications to handle the demands of the modern era of software

 Integrating Akka.NET with your existing .NET stack

 Deploying Akka.NET applications to the cloud

Readers should be comfortable with C# or F# and the .NET framework. No previous reactive experience needed.

M

icroservices are not very interesting if they only run on developer machines. They must be deployed to a production environment to provide busi- ness value. This chapter outlines how to approach deployment specifically within a microservice context.

In document Exploring Microservices (Page 54-57)