concept producer in category akka

appears as: producer, producer, producers, The producer, A producer
Reactive Design Patterns

This is an excerpt from Manning's book Reactive Design Patterns.

package main import ( #1     "fmt"     "time" ) func main() {     iterations := 10     myChannel := make(chan int) #2     go producer(myChannel, iterations)     go consumer(myChannel, iterations) #3     time.Sleep(500 * time.Millisecond) } func producer(myChannel chan int, iterations int) {     for i := 1; i <= iterations; i++ {         fmt.Println("Sending: ", i)         myChannel <- i #4     } } func consumer(myChannel chan int, iterations int) {     for i := 1; i <= iterations; i++ {         recVal := <-myChannel #5         fmt.Println("Received: ", recVal)     } } #1 - Imports required language libraries #2 - Creates a communication channel for integer values #3 - Starts asynchronous execution of producer and consumer #4 - Sends a message to the channel #5 - Receives a message from the channel
  • When the producer is faster than the consumer, the producer will eventually run out of demand. The system runs in “pull” mode, with the consumer pulling work items from the producer with each request it makes.
  • When the producer is slower than the consumer, the consumer will always have demand outstanding. The system runs in “push” mode, where the producer never needs to wait for a work request from the consumer.
  • Akka in Action

    This is an excerpt from Manning's book Akka in Action.

    Listing 12.11. Test the producer with responses
    implicit val ExecutionContext = system.dispatcher val probe = TestProbe() val camelUri ="mina:tcp://localhost:9889?textline=true" val consumer = system.actorOf(   Props(new OrderConfirmConsumerXml(camelUri, probe.ref))) val producer = system.actorOf(   Props(new OrderConfirmProducerXml(camelUri))) val activatedCons = CamelExtension(system).activationFutureFor(   consumer)(timeout = 10 seconds, executor = system.dispatcher) val activatedProd = CamelExtension(system).activationFutureFor(   producer)(timeout = 10 seconds, executor = system.dispatcher) val camel = Future.sequence(List(activatedCons, activatedProd)) Await.result(camel, 5 seconds) val probeSend = TestProbe() val msg = new Order("me", "Akka in Action", 10) probeSend.send(producer, msg) probe.expectMsg(msg) #1 probeSend.expectMsg("OK") #2 system.stop(producer) system.stop(consumer) #1 - Message is received by Consumer #2 - Confirmation is received by your test class !@%STYLE%@! {"css":"{\"css\": \"font-weight: bold;\"}","target":"[]"} !@%STYLE%@!

    One of the challenges for applications that consume streams is that you can’t know beforehand how much data you’ll need to handle, because more data may be produced at any time. Another challenge in streaming applications is dealing with the varying speeds of producers and consumers. If your application mediates between streaming producers and consumers, there’s one problem you’ll have to solve: how to buffer data without running out of memory. How can a producer know if a consumer can or can’t keep up?

    The log-stream processor will have to balance supply and demand to make sure that when one of the services applies back pressure, this won’t slow down the producer of log events. In the next section, we’ll discuss how buffers can be used to achieve this.