Skip to content

Instantly share code, notes, and snippets.

@kjsaila kjsaila/FunctionActorBean.java Secret
Last active Aug 29, 2015

Embed
What would you like to do?
Example Akka actor inside Spring app
package com.gofore.example.actors;
import static org.thirdparty.example.SpringExtension.SpringExtProvider;
import akka.actor.*;
import akka.event.Logging;
import akka.event.LoggingAdapter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.context.annotation.Scope;
import javax.inject.Named;
import com.gofore.example.dao.MyDAO;
import com.gofore.example.actors.messages.Recursion;
import com.gofore.example.actors.exceptions.ActorException;
import com.gofore.example.model.Function;
@Named("FunctionActorBean")
@Component
@Scope(value = "prototype")
public class FunctionActorBean extends UntypedActor {
private int childCount = 0;
@Autowired
private MyDAO myDAO;
private Function original;
private ActorRef actorParent = null;
public FunctionActorBean() {}
public void onReceive(Object message) throws Exception {
if (message instanceof Function) {
Function response = (Function)message;
for(FunctionArgument arg : original.getFunctionArguments()) {
if(arg.getFunctionChild() != null && arg.getFunctionChild().getId().equals(response.getId())) {
arg.setFunctionChild(response);
}
}
childCount--;
if(childCount <= 0) {
ActorRef par = getContext().parent();
if(par.equals(actorParent)) {
par.tell(original, getSelf());
} else {
actorParent.tell(original, getSelf());
}
getContext().stop(self());
}
} else if(message instanceof Recursion) {
actorParent = sender();
Recursion rec = (Recursion)message;
original = myDAO.getFunction(rec.getId());
if (original == null) {
self().tell(new ActorException("Function (" + rec.getId()+ ") doesn't exist", id), getSelf());
} else {
if(original.getFunctionArguments() == null || original.getFunctionArguments().size() == 0) {
self().tell(original, getSelf());
} else {
childCount = original.getFunctionArguments().size();
for (FunctionArgument fa : original.getFunctionArguments()) {
ActorSystem system = getContext().system();
if (fa.getFunctionChild() != null) {
ActorRef child = getContext().actorOf(SpringExtProvider.get(system).props("FunctionActorBean"), UUID.randomUUID().toString());
child.tell(new Recursion(fa.getFunctionChild().getId()), self());
} else {
self().tell(original, getSelf());
}
}
}
}
} else if(message instanceof Exception) {
ActorRef par = getContext().parent();
if(par.equals(actorParent)) {
par.tell(message, ActorRef.noSender());
} else {
Throwable ex = new ActorException("Epic Fail");
actorParent.tell(new Status.Failure(ex), ActorRef.noSender());
}
getContext().stop(self());
} else {
unhandled(message);
getContext().stop(getSelf());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.