Skip to content

Commit ffcd836

Browse files
forketyforkpivovarit
authored andcommitted
Source code for the article "An Intro to Spring with Akka" (eugenp#541)
1 parent b53e462 commit ffcd836

7 files changed

Lines changed: 198 additions & 5 deletions

File tree

‎spring-all/pom.xml‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,6 @@
4545
<artifactId>spring-aspects</artifactId>
4646
</dependency>
4747

48-
<dependency>
49-
<groupId>org.springframework</groupId>
50-
<artifactId>spring-orm</artifactId>
51-
</dependency>
52-
5348
<!-- persistence -->
5449

5550
<dependency>
@@ -92,6 +87,14 @@
9287
<scope>runtime</scope>
9388
</dependency>
9489

90+
<!-- Akka -->
91+
92+
<dependency>
93+
<groupId>com.typesafe.akka</groupId>
94+
<artifactId>akka-actor_2.11</artifactId>
95+
<version>2.4.8</version>
96+
</dependency>
97+
9598
<!-- util -->
9699

97100
<dependency>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package org.baeldung.akka;
2+
3+
import akka.actor.ActorSystem;
4+
import org.springframework.beans.factory.annotation.Autowired;
5+
import org.springframework.context.ApplicationContext;
6+
import org.springframework.context.annotation.Bean;
7+
import org.springframework.context.annotation.ComponentScan;
8+
import org.springframework.context.annotation.Configuration;
9+
10+
import static org.baeldung.akka.SpringExtension.SPRING_EXTENSION_PROVIDER;
11+
12+
@Configuration
13+
@ComponentScan
14+
public class AppConfiguration {
15+
16+
@Autowired
17+
private ApplicationContext applicationContext;
18+
19+
@Bean
20+
public ActorSystem actorSystem() {
21+
ActorSystem system = ActorSystem.create("akka-spring-demo");
22+
SPRING_EXTENSION_PROVIDER.get(system).initialize(applicationContext);
23+
return system;
24+
}
25+
26+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package org.baeldung.akka;
2+
3+
import akka.actor.UntypedActor;
4+
import org.springframework.context.annotation.Scope;
5+
import org.springframework.stereotype.Component;
6+
7+
import static org.springframework.beans.factory.config.ConfigurableBeanFactory.SCOPE_PROTOTYPE;
8+
9+
@Component
10+
@Scope(SCOPE_PROTOTYPE)
11+
public class GreetingActor extends UntypedActor {
12+
13+
private GreetingService greetingService;
14+
15+
public GreetingActor(GreetingService greetingService) {
16+
this.greetingService = greetingService;
17+
}
18+
19+
@Override
20+
public void onReceive(Object message) throws Throwable {
21+
if (message instanceof Greet) {
22+
String name = ((Greet) message).getName();
23+
getSender().tell(greetingService.greet(name), getSelf());
24+
} else {
25+
unhandled(message);
26+
}
27+
}
28+
29+
public static class Greet {
30+
31+
private String name;
32+
33+
public Greet(String name) {
34+
this.name = name;
35+
}
36+
37+
public String getName() {
38+
return name;
39+
}
40+
41+
}
42+
43+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package org.baeldung.akka;
2+
3+
import org.springframework.stereotype.Component;
4+
5+
@Component
6+
public class GreetingService {
7+
8+
public String greet(String name) {
9+
return "Hello, " + name;
10+
}
11+
12+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package org.baeldung.akka;
2+
3+
import akka.actor.Actor;
4+
import akka.actor.IndirectActorProducer;
5+
import org.springframework.context.ApplicationContext;
6+
7+
public class SpringActorProducer implements IndirectActorProducer {
8+
9+
private ApplicationContext applicationContext;
10+
11+
private String beanActorName;
12+
13+
public SpringActorProducer(ApplicationContext applicationContext, String beanActorName) {
14+
this.applicationContext = applicationContext;
15+
this.beanActorName = beanActorName;
16+
}
17+
18+
@Override
19+
public Actor produce() {
20+
return (Actor) applicationContext.getBean(beanActorName);
21+
}
22+
23+
@Override
24+
public Class<? extends Actor> actorClass() {
25+
return (Class<? extends Actor>) applicationContext.getType(beanActorName);
26+
}
27+
28+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package org.baeldung.akka;
2+
3+
import akka.actor.AbstractExtensionId;
4+
import akka.actor.ExtendedActorSystem;
5+
import akka.actor.Extension;
6+
import akka.actor.Props;
7+
import org.springframework.context.ApplicationContext;
8+
9+
public class SpringExtension extends AbstractExtensionId<SpringExtension.SpringExt> {
10+
11+
public static final SpringExtension SPRING_EXTENSION_PROVIDER = new SpringExtension();
12+
13+
@Override
14+
public SpringExt createExtension(ExtendedActorSystem system) {
15+
return new SpringExt();
16+
}
17+
18+
public static class SpringExt implements Extension {
19+
20+
private volatile ApplicationContext applicationContext;
21+
22+
public void initialize(ApplicationContext applicationContext) {
23+
this.applicationContext = applicationContext;
24+
}
25+
26+
public Props props(String actorBeanName) {
27+
return Props.create(SpringActorProducer.class, applicationContext, actorBeanName);
28+
}
29+
30+
}
31+
32+
33+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package org.baeldung.akka;
2+
3+
import java.util.concurrent.TimeUnit;
4+
5+
import akka.actor.ActorRef;
6+
import akka.actor.ActorSystem;
7+
import akka.util.Timeout;
8+
import org.baeldung.akka.GreetingActor.Greet;
9+
import org.junit.After;
10+
import org.junit.Assert;
11+
import org.junit.Test;
12+
import org.springframework.beans.factory.annotation.Autowired;
13+
import org.springframework.test.context.ContextConfiguration;
14+
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
15+
import scala.concurrent.Await;
16+
import scala.concurrent.Future;
17+
import scala.concurrent.duration.FiniteDuration;
18+
19+
import static akka.pattern.Patterns.ask;
20+
import static org.baeldung.akka.SpringExtension.SPRING_EXTENSION_PROVIDER;
21+
22+
@ContextConfiguration(classes = AppConfiguration.class)
23+
public class SpringAkkaTest extends AbstractJUnit4SpringContextTests {
24+
25+
@Autowired
26+
private ActorSystem system;
27+
28+
@Test
29+
public void whenCallingGreetingActor_thenActorGreetsTheCaller() throws Exception {
30+
ActorRef greeter = system.actorOf(
31+
SPRING_EXTENSION_PROVIDER.get(system)
32+
.props("greetingActor"), "greeter");
33+
34+
FiniteDuration duration = FiniteDuration.create(1, TimeUnit.SECONDS);
35+
Timeout timeout = Timeout.durationToTimeout(duration);
36+
37+
Future<Object> result = ask(greeter, new Greet("John"), timeout);
38+
39+
Assert.assertEquals("Hello, John", Await.result(result, duration));
40+
}
41+
42+
@After
43+
public void tearDown() {
44+
system.shutdown();
45+
system.awaitTermination();
46+
}
47+
48+
}

0 commit comments

Comments
 (0)