GithubHelp home page GithubHelp logo

akka's Introduction

Go AKKA

Currently Not Available !!!

example.go

func main() {
	var err error
	defer func() {
		if err != nil {
			fmt.Println(err)
		}
	}()

	config := configuration.LoadConfig("akka.conf")

	var system ActorSystem
	if system, err = actor.NewActorSystem("test", config); err != nil {
		return
	}

	var props Props
	if props, err = actor.Props.Create((*HelloWorld)(nil)); err != nil {
		return
	}

	var helloWorld ActorRef
	if helloWorld, err = system.ActorOf(props, "HelloWorld"); err != nil {
		return
	}

	var terminatorProps Props
	if terminatorProps, err = actor.Props.Create((*Terminator)(nil), helloWorld); err != nil {
		return
	}

	if _, err = system.ActorOf(terminatorProps, "Terminator"); err != nil {
		return
	}

	fmt.Println("Press return to exit ...")
	fmt.Scanln()
}

akka.conf

akka {
  log-config-on-start = on
  stdout-loglevel = DEBUG
  loglevel = ERROR
  actor {
      provider = "LocalActorRefProvider"

      default-mailbox {
        mailbox-type = "akka.dispatch.unbounded-mailbox"
      }

      default-dispatcher {
        type = "dispatcher"
      }
 
      debug {  
        receive = on 
        autoreceive = on
        lifecycle = on
        event-stream = on
        unhandled = on
      }
  }
}

HelloWorld

type HelloWorld struct {
	*actor.UntypedActor
}

func (p *HelloWorld) PreStart() (err error) {
	var props Props
	props, err = actor.Props.Create((*Greeter)(nil))
	if err != nil {
		fmt.Println(err)
		return
	}

	var greeter ActorRef
	greeter, err = p.Context().ActorOf(props, "greeter")
	if err != nil {
		fmt.Println(err)
		return
	}

	greeter.Tell("hello greeter", p.Context().Self())

	return
}

func (p *HelloWorld) Receive(message interface{}) (unhandled bool, err error) {
	switch msg := message.(type) {
	case string:
		{
			p.Context().StopActor(p.Self())
		}
	default:
		unhandled = true
	}

	return
}

Greeter

type Greeter struct {
	*actor.UntypedActor
}

func (p *Greeter) PreStart() {
	fmt.Println("pre start at Greeter")
}

func (p *Greeter) Receive(message interface{}) (unhandled bool, err error) {

	switch msg := message.(type) {
	case string:
		{
			fmt.Println("Greeter received message:", msg)
		}
	default:
		unhandled = true
	}

	return
}

Terminator

type Terminator struct {
	*actor.UntypedActor

	ref ActorRef
}

func (p *Terminator) Terminator(ref ActorRef) (err error) {
	p.ref = ref
	p.Context().Watch(ref)
	return
}

func (p *Terminator) Receive(message interface{}) (unhandled bool, err error) {
	switch message.(type) {
	case Terminated:
		{
			fmt.Printf("%s has terminated, shutting down system", p.ref.Path())
			p.Context().System().Terminate()
		}
	default:
		unhandled = true
	}
	return
}

akka's People

Contributors

xujinzheng avatar

Watchers

 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.