Source code for the article "An Intro to Spring with Akka" (#541)
This commit is contained in:
parent
b53e46246b
commit
ffcd83697a
|
@ -45,11 +45,6 @@
|
|||
<artifactId>spring-aspects</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-orm</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- persistence -->
|
||||
|
||||
<dependency>
|
||||
|
@ -92,6 +87,14 @@
|
|||
<scope>runtime</scope>
|
||||
</dependency>
|
||||
|
||||
<!-- Akka -->
|
||||
|
||||
<dependency>
|
||||
<groupId>com.typesafe.akka</groupId>
|
||||
<artifactId>akka-actor_2.11</artifactId>
|
||||
<version>2.4.8</version>
|
||||
</dependency>
|
||||
|
||||
<!-- util -->
|
||||
|
||||
<dependency>
|
||||
|
|
|
@ -0,0 +1,26 @@
|
|||
package org.baeldung.akka;
|
||||
|
||||
import akka.actor.ActorSystem;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import static org.baeldung.akka.SpringExtension.SPRING_EXTENSION_PROVIDER;
|
||||
|
||||
@Configuration
|
||||
@ComponentScan
|
||||
public class AppConfiguration {
|
||||
|
||||
@Autowired
|
||||
private ApplicationContext applicationContext;
|
||||
|
||||
@Bean
|
||||
public ActorSystem actorSystem() {
|
||||
ActorSystem system = ActorSystem.create("akka-spring-demo");
|
||||
SPRING_EXTENSION_PROVIDER.get(system).initialize(applicationContext);
|
||||
return system;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,43 @@
|
|||
package org.baeldung.akka;
|
||||
|
||||
import akka.actor.UntypedActor;
|
||||
import org.springframework.context.annotation.Scope;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import static org.springframework.beans.factory.config.ConfigurableBeanFactory.SCOPE_PROTOTYPE;
|
||||
|
||||
@Component
|
||||
@Scope(SCOPE_PROTOTYPE)
|
||||
public class GreetingActor extends UntypedActor {
|
||||
|
||||
private GreetingService greetingService;
|
||||
|
||||
public GreetingActor(GreetingService greetingService) {
|
||||
this.greetingService = greetingService;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onReceive(Object message) throws Throwable {
|
||||
if (message instanceof Greet) {
|
||||
String name = ((Greet) message).getName();
|
||||
getSender().tell(greetingService.greet(name), getSelf());
|
||||
} else {
|
||||
unhandled(message);
|
||||
}
|
||||
}
|
||||
|
||||
public static class Greet {
|
||||
|
||||
private String name;
|
||||
|
||||
public Greet(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
package org.baeldung.akka;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class GreetingService {
|
||||
|
||||
public String greet(String name) {
|
||||
return "Hello, " + name;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,28 @@
|
|||
package org.baeldung.akka;
|
||||
|
||||
import akka.actor.Actor;
|
||||
import akka.actor.IndirectActorProducer;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
|
||||
public class SpringActorProducer implements IndirectActorProducer {
|
||||
|
||||
private ApplicationContext applicationContext;
|
||||
|
||||
private String beanActorName;
|
||||
|
||||
public SpringActorProducer(ApplicationContext applicationContext, String beanActorName) {
|
||||
this.applicationContext = applicationContext;
|
||||
this.beanActorName = beanActorName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Actor produce() {
|
||||
return (Actor) applicationContext.getBean(beanActorName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<? extends Actor> actorClass() {
|
||||
return (Class<? extends Actor>) applicationContext.getType(beanActorName);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,33 @@
|
|||
package org.baeldung.akka;
|
||||
|
||||
import akka.actor.AbstractExtensionId;
|
||||
import akka.actor.ExtendedActorSystem;
|
||||
import akka.actor.Extension;
|
||||
import akka.actor.Props;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
|
||||
public class SpringExtension extends AbstractExtensionId<SpringExtension.SpringExt> {
|
||||
|
||||
public static final SpringExtension SPRING_EXTENSION_PROVIDER = new SpringExtension();
|
||||
|
||||
@Override
|
||||
public SpringExt createExtension(ExtendedActorSystem system) {
|
||||
return new SpringExt();
|
||||
}
|
||||
|
||||
public static class SpringExt implements Extension {
|
||||
|
||||
private volatile ApplicationContext applicationContext;
|
||||
|
||||
public void initialize(ApplicationContext applicationContext) {
|
||||
this.applicationContext = applicationContext;
|
||||
}
|
||||
|
||||
public Props props(String actorBeanName) {
|
||||
return Props.create(SpringActorProducer.class, applicationContext, actorBeanName);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,48 @@
|
|||
package org.baeldung.akka;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import akka.actor.ActorRef;
|
||||
import akka.actor.ActorSystem;
|
||||
import akka.util.Timeout;
|
||||
import org.baeldung.akka.GreetingActor.Greet;
|
||||
import org.junit.After;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
|
||||
import scala.concurrent.Await;
|
||||
import scala.concurrent.Future;
|
||||
import scala.concurrent.duration.FiniteDuration;
|
||||
|
||||
import static akka.pattern.Patterns.ask;
|
||||
import static org.baeldung.akka.SpringExtension.SPRING_EXTENSION_PROVIDER;
|
||||
|
||||
@ContextConfiguration(classes = AppConfiguration.class)
|
||||
public class SpringAkkaTest extends AbstractJUnit4SpringContextTests {
|
||||
|
||||
@Autowired
|
||||
private ActorSystem system;
|
||||
|
||||
@Test
|
||||
public void whenCallingGreetingActor_thenActorGreetsTheCaller() throws Exception {
|
||||
ActorRef greeter = system.actorOf(
|
||||
SPRING_EXTENSION_PROVIDER.get(system)
|
||||
.props("greetingActor"), "greeter");
|
||||
|
||||
FiniteDuration duration = FiniteDuration.create(1, TimeUnit.SECONDS);
|
||||
Timeout timeout = Timeout.durationToTimeout(duration);
|
||||
|
||||
Future<Object> result = ask(greeter, new Greet("John"), timeout);
|
||||
|
||||
Assert.assertEquals("Hello, John", Await.result(result, duration));
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
system.shutdown();
|
||||
system.awaitTermination();
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue