2018-10-29 17:19:37 +08:00
|
|
|
/*
|
2019-01-02 18:55:26 +08:00
|
|
|
* Copyright (C) 2009-2019 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
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
// #imports
|
2012-01-20 11:30:33 +01:00
|
|
|
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-01-20 11:30:33 +01:00
|
|
|
import com.typesafe.config.Config;
|
|
|
|
|
import java.util.concurrent.TimeUnit;
|
2018-06-26 15:41:30 +02:00
|
|
|
import java.time.Duration;
|
2012-01-20 11:30:33 +01:00
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
// #imports
|
2012-01-20 11:30:33 +01:00
|
|
|
|
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
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
public
|
|
|
|
|
// #extension
|
|
|
|
|
static 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 =
|
2019-01-12 04:00:53 +08:00
|
|
|
Duration.ofMillis(
|
|
|
|
|
config.getDuration("myapp.circuit-breaker.timeout", TimeUnit.MILLISECONDS));
|
2012-01-20 11:30:33 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
// #extension
|
2012-01-20 11:30:33 +01:00
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
public
|
|
|
|
|
// #extensionid
|
|
|
|
|
static class Settings extends AbstractExtensionId<SettingsImpl> implements ExtensionIdProvider {
|
|
|
|
|
public static final 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());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
// #extensionid
|
2012-01-20 11:30:33 +01:00
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
public
|
|
|
|
|
// #extension-usage-actor
|
|
|
|
|
static class MyActor extends AbstractActor {
|
2012-09-26 10:56:25 +02:00
|
|
|
// typically you would use static import of the Settings.SettingsProvider field
|
2019-01-12 04:00:53 +08:00
|
|
|
final SettingsImpl settings = Settings.SettingsProvider.get(getContext().getSystem());
|
|
|
|
|
Connection connection = connect(settings.DB_URI, settings.CIRCUIT_BREAKER_TIMEOUT);
|
2012-01-20 11:30:33 +01:00
|
|
|
|
2019-01-12 04:00:53 +08: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
|
|
|
}
|
2019-01-12 04:00:53 +08:00
|
|
|
// #extension-usage-actor
|
2012-01-20 11:30:33 +01:00
|
|
|
}
|
2019-01-12 04:00:53 +08:00
|
|
|
// #extension-usage-actor
|
2012-01-20 11:30:33 +01:00
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
public static class Connection {}
|
2012-01-20 11:30:33 +01:00
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void demonstrateHowToCreateAndUseAnAkkaExtensionInJava() {
|
|
|
|
|
final ActorSystem system = null;
|
|
|
|
|
try {
|
2019-01-12 04:00:53 +08:00
|
|
|
// #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;
|
2019-01-12 04:00:53 +08:00
|
|
|
// #extension-usage
|
2012-01-20 11:30:33 +01:00
|
|
|
} catch (Exception e) {
|
2019-01-12 04:00:53 +08:00
|
|
|
// do nothing
|
2012-01-20 11:30:33 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|