Giter Site home page Giter Site logo

cdi-akka's Introduction

Build Status

cdi-akka: Basic idea

Some basic CDI integration for Akka.

Akka(CDI)Extension

The com.github.rmannibucau.cdi.akka.internal.AkkaExtension is simply a CDI extension to allow to import in Akka actors world some CDI features and the opposite.

The first feature is to expose to CDI world akka.actor.ActorRef.

In Java it looks like:

@Inject @Akka(MyActor.class)
private ActorRef ref;

In Scala it will probably be:

@Inject @Akka(classOf(MyActor))
private var ref: ActorRef = _

So then you can send messages to Akka as simple as usually:

ref ! Foo

If you want some more advanced usage you can inject using the same kind of trick the ActorSystem:

@Inject @Akka(ActorSystem)
private var system: ActorSystem = _

Here a Java sample:

@ApplicationScoped
public class SomeBusiness {
    @Inject @Akka(PongActor.class)
    private ActorRef pongActor;

    @Inject @Akka(PingActor.class)
    private ActorRef pingActor;

    @Inject @Akka(ActorSystem.class)
    private ActorSystem system;

    public void sendPong() {
        System.out.println("PingActor! from " + system.name());
        answer();
    }

    private void answer() {
        pongActor.tell(Pong$.MODULE$, pingActor); // Pong is a scala case class
    }
}

Note that in this extension actors are @Dependent beans so you can inject any CDI bean you want in it:

class PingActor extends Actor {
  @Inject
  private var business: SomeBusiness = _

  def receive = {
    case Ping =>
      business.sendPong()
  }
}

Using CdiCreator without the extension

To get actor managed by cdi you can use the com.github.rmannibucau.cdi.akka.internal.CdiCreator even if you don't use the extension.

In Java it will look like:

system.actorOf(new Props(beanClass).withCreator(new CdiCreator(beanManager, actorType)), "myActor);
// or (will use deltaspike BeanManagerProvider)
system.actorOf(new Props(beanClass).withCreator(new CdiCreator(actorType)), "myActor);

(De)Activation

The module is activated by default but if you want to use only some part of it (will be explained later) you can deactivate the cdi extension using deltaspike deactivable mecanism on the extension com.github.rmannibucau.cdi.akka.internal.AkkaExtension.

Note: The easier is probably to implement org.apache.deltaspike.core.spi.activation.ClassDeactivator.

cdi-akka's People

Contributors

rmannibucau avatar

Stargazers

Francesco Colucci avatar Paweł Kaczor avatar  avatar Daniel Cunha avatar taichi avatar Jonas Bonér avatar  avatar

Watchers

James Cloos avatar  avatar  avatar

cdi-akka's Issues

License type?

is it possible to get the license type for this project? Thank you!

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.