Source code for the article "An Intro to Spring with Akka" (#541)
This commit is contained in:
committed by
Grzegorz Piwowarek
parent
b53e46246b
commit
ffcd83697a
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user