2012-01-20 11:30:33 +01:00
|
|
|
/**
|
|
|
|
|
* Copyright (C) 2009-2012 Typesafe Inc. <http://www.typesafe.com>
|
|
|
|
|
*/
|
2012-05-22 11:37:09 +02:00
|
|
|
package docs.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-06-29 13:33:20 +02:00
|
|
|
import scala.concurrent.util.Duration;
|
2012-01-20 11:30:33 +01:00
|
|
|
import com.typesafe.config.Config;
|
|
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
|
|
|
|
//#imports
|
|
|
|
|
|
|
|
|
|
import akka.actor.UntypedActor;
|
|
|
|
|
import org.junit.Test;
|
|
|
|
|
|
|
|
|
|
public class SettingsExtensionDocTestBase {
|
|
|
|
|
|
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 =
|
|
|
|
|
Duration.create(config.getMilliseconds("myapp.circuit-breaker.timeout"),
|
2012-01-20 11:30:33 +01:00
|
|
|
TimeUnit.MILLISECONDS);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//#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
|
|
|
|
|
|
|
|
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
|
2012-09-26 10:56:25 +02:00
|
|
|
public class MyActor extends UntypedActor {
|
|
|
|
|
// typically you would use static import of the Settings.SettingsProvider field
|
|
|
|
|
final SettingsImpl settings =
|
|
|
|
|
Settings.SettingsProvider.get(getContext().system());
|
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void onReceive(Object msg) {
|
|
|
|
|
}
|
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
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|