Coder Social home page Coder Social logo

akka-spring-boot's Introduction

Akka Spring Boot Integration (akka-spring-boot)

Scala-based integration of Akka with Spring Boot. Two-way Akka<->Spring configuration bindings and convention over configuration with sensible automatic defaults get your project running quickly.

The goal of this project is to produce bootable, Scala-based Spring Boot applications with minimal configuration.

Key Benefits

  1. Full Spring dependency injection support
    • Autowire any dependency into your actors and leverage the full Spring ecosystem where it makes sense
    • Use existing Spring components to enable gradual migration or reuse of suitable existing enterprise components
    • Avoid the downsides of using Scala implicits or abstract types to implement dependency injection. While both are excellent features, they can also lead to tight coupling and less maintainable code.
  2. Configure Akka via any Spring property source
    • Use your Spring Boot configuration (YAML, properties files, or any property source) to set Akka properties. Any property set via Spring is visible via Akka Config.
    • Seamless two-way integration of Akka and Spring configuration - any property defined in Akka configuration is accessible via Spring and vice versa.
  3. Pre-configured default actor system that's managed for you
    • No need to create and manage an actor system for your actors. A default actor system will be created when your application starts and terminated when your application is stopped.
  4. Easy creation of actor beans and actor references
    • Simple, standard annotations and familiar actorOf() methods are all that's required to create actors that integrate with Spring.

Getting Started

build.sbt
libraryDependencies ++= "com.github.scalaspring" %% "akka-spring-boot" % "0.3.1"
Create an Actor and a Spring configuration
@ActorComponent
class EchoActor extends Actor {
  override def receive = {
    case message โ‡’ sender() ! message
  }
}

@Configuration
@ComponentScan
@Import(Array(classOf[AkkaAutoConfiguration]))
class EchoConfiguration extends ActorSystemConfiguration {

  @Bean
  def echoActor = actorOf[EchoActor]

}
Notes on the code
  • Actors
    • Annotate your actors with @ActorComponent, a Spring meta-annotation. This is simply a more readable way of marking your actors as Spring prototype beans.
  • Configurations
    • Extend the ActorSystemConfiguration trait, which includes the helpful actorOf() methods
    • Import the AkkaAutoConfiguration configuration, which creates and manages the default actor system
    • Note that the @ComponentScan annotation will cause the EchoActor class to get picked up as a bean.
  • Configuration Properties
    • Use the akka.actorSystem.lifecycle.phase configuration property to control when the underlying ActorSystem is terminated. The default value is -10 to ensure its termination after any default beans.
Test the Configuration

Create a ScalaTest-based test that uses the configuration (see the scalatest-spring project)

@ContextConfiguration(
  loader = classOf[SpringApplicationContextLoader],
  classes = Array(classOf[EchoConfiguration])
)
class EchoConfigurationSpec extends FlatSpec with TestContextManagement with Matchers with AskSupport with ScalaFutures with StrictLogging {

  implicit val timeout: Timeout = (1 seconds)

  @Autowired val echoActor: ActorRef = null

  "Echo actor" should "receive and echo message" in {
    val message = "test message"
    val future = echoActor ? message

    whenReady(future) { result =>
      logger.info(s"""received result "$result"""")
      result should equal(message)
    }
  }

}

FAQ

How do I inject dependencies into my Scala classes?
Option 1: Constructor injection (Recommended)

Use the standard Spring @Autowired (or Java's @Inject) annotation on your class constructor(s). Note that the parentheses on the @Autowired annotation are required.

For example, assuming a bean of type MyService is defined in your configuration, the following actor will be injected with the appropriate dependency. Note that this technique works with any Scala class, not just Actors. Use one of the standard Spring annotations (@Component, @Service, etc.) instead of @ActorComponent.

@Service
class SomeService {
  def someMethod() = { ... }
}

@ActorComponent
class SomeActor @Autowired() (val service: SomeService) extends Actor {
  override def receive = {
    // Call methods on service ...
  }
}
Option 2: Field injection

Use the standard Spring @Autowired (or Java's @Inject) annotation on class fields. Note that Spring will set read-only (val) fields.

@Component
class SomeComponent {
  def someMethod() = { ... }
}

@ActorComponent
class SomeActor extends Actor {

  @Autowired val component: SomeComponent = null

  override def receive = {
    // Call methods on component ...
  }
}
How is this project different than the spring-scala project from Pivotal Labs?

The two projects have different purposes and approaches. The scala-spring project strives to make Spring accessible via functional configuration. This project uses a different approach, relying on standard Spring annotations, and more tightly integrates with Akka. Note that the scala-spring project is no longer maintained.

akka-spring-boot's People

Contributors

lancearlaus avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.