concept consumer in category akka

appears as: consumer, consumer, consumers, consumers
Reactive Design Patterns

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

So far, this book has used the word user informally and mostly in the sense of humans who interact with a computer. You interact only with your web browser in order to read and write emails, but many computers are needed in the background to perform these tasks. Each of these computers offers a certain set of services, and the consumer or user of these services will in most cases be another computer that is acting on behalf of a human, either directly or indirectly.

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
  • The Pull pattern propagates back pressure from consumers to producers.
  • 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.

    You see that it doesn’t matter which order we use; the pipeline gives us the same functionality. This flexibility is the strength of this pattern. In our example we used actual filters, but this pattern can be extended; the processing pipeline isn’t limited to filters. As long as the process accepts and produces the same types of messages, and is independent of the other processes, this pattern applies. In the next section, you’ll see a pattern that enables a divide-and-conquer approach, which requires concurrency, and Akka again makes it easy. We’ll scatter units of work among a number of processing actors and then gather their results into a single set, allowing the consumer of the work product to just make a request and get a single response.

    Listing 12.2. Test the order consumer
    val msg = new Order("me", "Akka in Action", 10) val xml = <order>             <customerId>{ msg.customerId }</customerId>             <productId>{ msg.productId }</productId>             <number>{ msg.number }</number>           </order> #1 val msgFile = new File(dir, "msg1.xml") FileUtils.write(msgFile, xml.toString()) #2 probe.expectMsg(msg) #3 system.stop(consumer) #1 - Creates XML content #2 - Writes file in message directory #3 - Expects Order message to be sent by Consumer !@%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?