Scala HTTP Server using actors

In this tutorial I will be creating a very basic HTTP Server in Scala. In place of the Threads and ExecutorService I would probably use with core Java I will use the Actor pattern:

A main actor will be responsible for starting the listening server and creating child actors to respond to incoming requests.

The actors communicate by the sending of messages, to start the service I will use a StartListening message. I will implement this in scala as a ‘case class’ which will be an immutable value object to hold the port (to listen on) and the number of child threads to handle incoming clients.

Step 1. A Case class for startup options.

We will use the Actor pattern to create our simple HTTP Server


case class StartListening(port: Int = 8080, numberOfThreads: Int = 1)

The StartListening class demonstrates default parameters with default values – a default port of 8080 and default number of threads of 1.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s