alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Akka/Scala example source code file (ErrorThrowingConsumer.java)

This example Akka source code file (ErrorThrowingConsumer.java) is included in my "Source Code Warehouse" project. The intent of this project is to help you more easily find Akka and Scala source code examples by using tags.

All credit for the original source code belongs to akka.io; I'm just trying to make examples easier to find. (For my Scala work, see my Scala examples and tutorials.)

Akka tags/keywords

actor, akka, camel, camelmessage, dispatch, errorthrowingconsumer, exception, mapper, option, override, processordefinition, string, untypedconsumeractor

The ErrorThrowingConsumer.java Akka example source code

package docs.camel;
//#ErrorThrowingConsumer
import akka.actor.Status;
import akka.camel.CamelMessage;
import akka.camel.javaapi.UntypedConsumerActor;
import akka.dispatch.Mapper;
import org.apache.camel.builder.Builder;
import org.apache.camel.model.ProcessorDefinition;
import org.apache.camel.model.RouteDefinition;
import scala.Option;

public class ErrorThrowingConsumer extends UntypedConsumerActor{
  private String uri;

  private static Mapper<RouteDefinition, ProcessorDefinition<?>> mapper =
    new Mapper<RouteDefinition, ProcessorDefinition<?>>() {
      public ProcessorDefinition<?> apply(RouteDefinition rd) {
        // Catch any exception and handle it by returning the exception message
        // as response
        return rd.onException(Exception.class).handled(true).
          transform(Builder.exceptionMessage()).end();
      }
    };

  public ErrorThrowingConsumer(String uri){
    this.uri = uri;
  }

  public String getEndpointUri() {
    return uri;
  }

  public void onReceive(Object message) throws Exception{
    if (message instanceof CamelMessage) {
      CamelMessage camelMessage = (CamelMessage) message;
      String body = camelMessage.getBodyAs(String.class, getCamelContext());
      throw new Exception(String.format("error: %s",body));
    } else
      unhandled(message);
  }

  @Override
  public Mapper<RouteDefinition,
    ProcessorDefinition<?>> getRouteDefinitionHandler() {
    return mapper;
  }

  @Override
  public void preRestart(Throwable reason, Option<Object> message) {
    getSender().tell(new Status.Failure(reason), getSelf());
  }
}
//#ErrorThrowingConsumer

Other Akka source code examples

Here is a short list of links related to this Akka ErrorThrowingConsumer.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.