2011-12-19 11:07:59 +01:00
|
|
|
/**
|
2012-01-19 18:21:06 +01:00
|
|
|
* Copyright (C) 2009-2012 Typesafe Inc. <http://www.typesafe.com>
|
2011-12-19 11:07:59 +01:00
|
|
|
*/
|
2012-05-22 11:37:09 +02:00
|
|
|
package docs.extension;
|
2011-12-15 13:46:37 +01:00
|
|
|
|
|
|
|
|
//#imports
|
|
|
|
|
import akka.actor.*;
|
|
|
|
|
import java.util.concurrent.atomic.AtomicLong;
|
|
|
|
|
|
|
|
|
|
//#imports
|
|
|
|
|
|
|
|
|
|
import org.junit.Test;
|
|
|
|
|
|
|
|
|
|
public class ExtensionDocTestBase {
|
|
|
|
|
|
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 CountExtensionImpl implements Extension {
|
2012-01-20 11:30:33 +01:00
|
|
|
//Since this Extension is a shared instance
|
|
|
|
|
// per ActorSystem we need to be threadsafe
|
|
|
|
|
private final AtomicLong counter = new AtomicLong(0);
|
2011-12-15 13:46:37 +01:00
|
|
|
|
2012-01-20 11:30:33 +01:00
|
|
|
//This is the operation this Extension provides
|
|
|
|
|
public long increment() {
|
|
|
|
|
return counter.incrementAndGet();
|
2011-12-15 13:46:37 +01:00
|
|
|
}
|
2012-01-20 11:30:33 +01:00
|
|
|
}
|
2011-12-15 13:46:37 +01:00
|
|
|
|
2012-01-20 11:30:33 +01:00
|
|
|
//#extension
|
2011-12-15 13:46:37 +01:00
|
|
|
|
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 CountExtension extends AbstractExtensionId<CountExtensionImpl>
|
|
|
|
|
implements ExtensionIdProvider {
|
2011-12-15 13:46:37 +01:00
|
|
|
//This will be the identifier of our CountExtension
|
2012-01-23 16:11:58 +01:00
|
|
|
public final static CountExtension CountExtensionProvider = new CountExtension();
|
2012-01-20 11:30:33 +01:00
|
|
|
|
|
|
|
|
//The lookup method is required by ExtensionIdProvider,
|
|
|
|
|
// so we return ourselves here, this allows us
|
|
|
|
|
// to configure our extension to be loaded when
|
|
|
|
|
// the ActorSystem starts up
|
|
|
|
|
public CountExtension lookup() {
|
2012-01-23 16:11:58 +01:00
|
|
|
return CountExtension.CountExtensionProvider; //The public static final
|
2012-01-20 11:30:33 +01:00
|
|
|
}
|
2011-12-15 13:46:37 +01:00
|
|
|
|
2012-01-20 11:30:33 +01:00
|
|
|
//This method will be called by Akka
|
|
|
|
|
// to instantiate our Extension
|
2012-01-24 11:33:40 +01:00
|
|
|
public CountExtensionImpl createExtension(ExtendedActorSystem system) {
|
2012-01-20 11:30:33 +01:00
|
|
|
return new CountExtensionImpl();
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-12-15 13:46:37 +01:00
|
|
|
|
2012-01-20 11:30:33 +01:00
|
|
|
//#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 {
|
2012-01-20 11:30:33 +01:00
|
|
|
public void onReceive(Object msg) {
|
2012-09-26 10:56:25 +02:00
|
|
|
// typically you would use static import of the
|
|
|
|
|
// CountExtension.CountExtensionProvider field
|
2012-01-23 16:11:58 +01:00
|
|
|
CountExtension.CountExtensionProvider.get(getContext().system()).increment();
|
2011-12-15 13:46:37 +01:00
|
|
|
}
|
2012-01-20 11:30:33 +01:00
|
|
|
}
|
2011-12-15 13:46:37 +01:00
|
|
|
|
2012-01-20 11:30:33 +01:00
|
|
|
//#extension-usage-actor
|
2011-12-15 13:46:37 +01:00
|
|
|
|
2012-01-20 11:30:33 +01:00
|
|
|
@Test
|
|
|
|
|
public void demonstrateHowToCreateAndUseAnAkkaExtensionInJava() {
|
2011-12-15 13:46:37 +01:00
|
|
|
final ActorSystem system = null;
|
|
|
|
|
try {
|
|
|
|
|
//#extension-usage
|
2012-09-26 10:56:25 +02:00
|
|
|
// typically you would use static import of the
|
|
|
|
|
// CountExtension.CountExtensionProvider field
|
2012-01-23 16:11:58 +01:00
|
|
|
CountExtension.CountExtensionProvider.get(system).increment();
|
2011-12-15 13:46:37 +01:00
|
|
|
//#extension-usage
|
2012-01-20 11:30:33 +01:00
|
|
|
} catch (Exception e) {
|
2011-12-15 13:46:37 +01:00
|
|
|
//do nothing
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|