Skip to content

Instantly share code, notes, and snippets.

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

Embed
What would you like to do?
Example service with Spring and Akka
package com.gofore.example.service.impl;
import static org.thirdparty.example.SpringExtension.SpringExtProvider;
import scala.concurrent.Await;
import scala.concurrent.Future;
import scala.concurrent.duration.Duration;
import akka.actor.ActorRef;
import akka.actor.ActorSystem;
import akka.pattern.Patterns;
import akka.util.Timeout;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.gofore.example.dao.MyDAO;
import com.gofore.example.actors.messages.Recursion;
import com.gofore.example.actors.exceptions.ActorException;
@Service
@Transactional
public class MyServiceImpl implements MyService {
@Autowired
private MyDAO myDAO;
@Autowired
private ApplicationContext applicationContext;
@Transactional(readOnly = true)
public Function getFunctionRecursively(final Long id) throws ActorException {
ActorSystem system = ActorSystem.create("actorSystem");
SpringExtProvider.get(system).initialize(applicationContext);
Timeout timeout = new Timeout(Duration.create(30, "seconds"));
ActorRef master = system.actorOf(SpringExtProvider.get(system).props("FunctionActorBean"), "master");
Future<Object> future = Patterns.ask(master, new Recursion(id), timeout);
try {
Function function = (Function)Await.result(future, timeout.duration());
system.shutdown();
return function;
} catch (Exception e) {
system.shutdown();
throw new ActorException("Actors failed");
}
}
}
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.