2012-01-20 11:30:33 +01:00
|
|
|
/**
|
2018-01-04 17:26:29 +00:00
|
|
|
* Copyright (C) 2009-2018 Lightbend Inc. <https://www.lightbend.com>
|
2012-01-20 11:30:33 +01:00
|
|
|
*/
|
2018-03-13 23:45:55 +09:00
|
|
|
|
2017-03-16 09:30:00 +01:00
|
|
|
package jdocs.extension;
|
2012-01-20 11:30:33 +01:00
|
|
|
|
|
|
|
|
//#imports
|
|
|
|
|
import akka.actor.Extension;
|
|
|
|
|
import akka.actor.AbstractExtensionId;
|
|
|
|
|
import akka.actor.ExtensionIdProvider;
|
|
|
|
|
import akka.actor.ActorSystem;
|
2012-01-24 11:33:40 +01:00
|
|
|
import akka.actor.ExtendedActorSystem;
|
2012-10-15 16:18:52 +02:00
|
|
|
import scala.concurrent.duration.Duration;
|
2012-01-20 11:30:33 +01:00
|
|
|
import com.typesafe.config.Config;
|
|
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
|
|
|
|
//#imports
|
|
|
|
|
|
2017-03-16 09:30:00 +01:00
|
|
|
import jdocs.AbstractJavaTest;
|
improve AbstractActor, #21717
* Receive class that wraps PartialFunction, to avoid
scary scala types
* move AbstractActorContext to AbstractActor.ActorContext
* converting docs, many, many UntypedActor
* removing UntypedActor docs
* add unit test for ReceiveBuilder
* MiMa filters
* consistent use of getContext(), self(), sender()
* rename cross references
* migration guide
* skip samples for now
* improve match type safetyi, add matchUnchecked
* the `? extends P` caused code like this to compile:
`match(String.class, (Integer i) -> {})`
* added matchUnchecked, since it can still be useful (um, convenient)
to be able to do:
`matchUnchecked(List.class, (List<String> list) -> {})`
* eleminate some scala.Option
* preRestart
* findChild
* ActorIdentity.getActorRef
2016-12-13 10:59:29 +01:00
|
|
|
import akka.actor.AbstractActor;
|
2012-01-20 11:30:33 +01:00
|
|
|
import org.junit.Test;
|
|
|
|
|
|
2016-02-11 16:39:25 +01:00
|
|
|
public class SettingsExtensionDocTest extends AbstractJavaTest {
|
2012-01-20 11:30:33 +01:00
|
|
|
|
2012-09-26 10:56:25 +02:00
|
|
|
static
|
2012-01-20 11:30:33 +01:00
|
|
|
//#extension
|
2012-09-26 10:56:25 +02:00
|
|
|
public class SettingsImpl implements Extension {
|
2012-01-20 11:30:33 +01:00
|
|
|
|
|
|
|
|
public final String DB_URI;
|
|
|
|
|
public final Duration CIRCUIT_BREAKER_TIMEOUT;
|
|
|
|
|
|
|
|
|
|
public SettingsImpl(Config config) {
|
2012-05-09 09:36:16 +02:00
|
|
|
DB_URI = config.getString("myapp.db.uri");
|
2012-09-26 10:56:25 +02:00
|
|
|
CIRCUIT_BREAKER_TIMEOUT =
|
2014-03-24 15:35:54 +01:00
|
|
|
Duration.create(config.getDuration("myapp.circuit-breaker.timeout",
|
|
|
|
|
TimeUnit.MILLISECONDS), TimeUnit.MILLISECONDS);
|
2012-01-20 11:30:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//#extension
|
|
|
|
|
|
2012-09-26 10:56:25 +02:00
|
|
|
static
|
2012-01-20 11:30:33 +01:00
|
|
|
//#extensionid
|
2012-09-26 10:56:25 +02:00
|
|
|
public class Settings extends AbstractExtensionId<SettingsImpl>
|
|
|
|
|
implements ExtensionIdProvider {
|
2012-01-23 16:11:58 +01:00
|
|
|
public final static Settings SettingsProvider = new Settings();
|
2012-01-20 11:30:33 +01:00
|
|
|
|
2013-06-24 20:27:12 +02:00
|
|
|
private Settings() {}
|
|
|
|
|
|
2012-01-20 11:30:33 +01:00
|
|
|
public Settings lookup() {
|
2012-01-23 16:11:58 +01:00
|
|
|
return Settings.SettingsProvider;
|
2012-01-20 11:30:33 +01:00
|
|
|
}
|
|
|
|
|
|
2012-01-24 11:33:40 +01:00
|
|
|
public SettingsImpl createExtension(ExtendedActorSystem system) {
|
2012-01-20 11:30:33 +01:00
|
|
|
return new SettingsImpl(system.settings().config());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//#extensionid
|
|
|
|
|
|
2012-09-26 10:56:25 +02:00
|
|
|
static
|
2012-01-20 11:30:33 +01:00
|
|
|
//#extension-usage-actor
|
improve AbstractActor, #21717
* Receive class that wraps PartialFunction, to avoid
scary scala types
* move AbstractActorContext to AbstractActor.ActorContext
* converting docs, many, many UntypedActor
* removing UntypedActor docs
* add unit test for ReceiveBuilder
* MiMa filters
* consistent use of getContext(), self(), sender()
* rename cross references
* migration guide
* skip samples for now
* improve match type safetyi, add matchUnchecked
* the `? extends P` caused code like this to compile:
`match(String.class, (Integer i) -> {})`
* added matchUnchecked, since it can still be useful (um, convenient)
to be able to do:
`matchUnchecked(List.class, (List<String> list) -> {})`
* eleminate some scala.Option
* preRestart
* findChild
* ActorIdentity.getActorRef
2016-12-13 10:59:29 +01:00
|
|
|
public class MyActor extends AbstractActor {
|
2012-09-26 10:56:25 +02:00
|
|
|
// typically you would use static import of the Settings.SettingsProvider field
|
|
|
|
|
final SettingsImpl settings =
|
2017-03-16 09:30:00 +01:00
|
|
|
Settings.SettingsProvider.get(getContext().getSystem());
|
2012-09-26 10:56:25 +02:00
|
|
|
Connection connection =
|
|
|
|
|
connect(settings.DB_URI, settings.CIRCUIT_BREAKER_TIMEOUT);
|
2012-01-20 11:30:33 +01:00
|
|
|
|
2012-09-26 10:56:25 +02:00
|
|
|
//#extension-usage-actor
|
2012-01-20 11:30:33 +01:00
|
|
|
|
|
|
|
|
public Connection connect(String dbUri, Duration circuitBreakerTimeout) {
|
|
|
|
|
return new Connection();
|
|
|
|
|
}
|
|
|
|
|
|
improve AbstractActor, #21717
* Receive class that wraps PartialFunction, to avoid
scary scala types
* move AbstractActorContext to AbstractActor.ActorContext
* converting docs, many, many UntypedActor
* removing UntypedActor docs
* add unit test for ReceiveBuilder
* MiMa filters
* consistent use of getContext(), self(), sender()
* rename cross references
* migration guide
* skip samples for now
* improve match type safetyi, add matchUnchecked
* the `? extends P` caused code like this to compile:
`match(String.class, (Integer i) -> {})`
* added matchUnchecked, since it can still be useful (um, convenient)
to be able to do:
`matchUnchecked(List.class, (List<String> list) -> {})`
* eleminate some scala.Option
* preRestart
* findChild
* ActorIdentity.getActorRef
2016-12-13 10:59:29 +01:00
|
|
|
@Override
|
|
|
|
|
public Receive createReceive() {
|
|
|
|
|
return AbstractActor.emptyBehavior();
|
2012-01-20 11:30:33 +01:00
|
|
|
}
|
2012-09-26 10:56:25 +02:00
|
|
|
//#extension-usage-actor
|
2012-01-20 11:30:33 +01:00
|
|
|
}
|
2012-09-26 10:56:25 +02:00
|
|
|
//#extension-usage-actor
|
2012-01-20 11:30:33 +01:00
|
|
|
|
|
|
|
|
public static class Connection {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void demonstrateHowToCreateAndUseAnAkkaExtensionInJava() {
|
|
|
|
|
final ActorSystem system = null;
|
|
|
|
|
try {
|
|
|
|
|
//#extension-usage
|
2012-09-26 10:56:25 +02:00
|
|
|
// typically you would use static import of the Settings.SettingsProvider field
|
2012-01-23 16:11:58 +01:00
|
|
|
String dbUri = Settings.SettingsProvider.get(system).DB_URI;
|
2012-01-20 11:30:33 +01:00
|
|
|
//#extension-usage
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
//do nothing
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|