2011-12-19 11:07:59 +01:00
|
|
|
/**
|
2017-01-04 17:37:10 +01:00
|
|
|
* Copyright (C) 2009-2017 Lightbend Inc. <http://www.lightbend.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
|
|
|
|
|
|
2016-02-11 16:39:25 +01:00
|
|
|
import docs.AbstractJavaTest;
|
2011-12-15 13:46:37 +01:00
|
|
|
import org.junit.Test;
|
|
|
|
|
|
2016-02-11 16:39:25 +01:00
|
|
|
public class ExtensionDocTest extends AbstractJavaTest {
|
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
|
|
|
//#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
|
|
|
|
2013-06-24 20:27:12 +02:00
|
|
|
private 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
|
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 {
|
|
|
|
|
@Override
|
|
|
|
|
public Receive createReceive() {
|
|
|
|
|
return receiveBuilder()
|
|
|
|
|
.matchAny(msg -> {
|
|
|
|
|
// typically you would use static import of the
|
|
|
|
|
// CountExtension.CountExtensionProvider field
|
|
|
|
|
CountExtension.CountExtensionProvider.get(getContext().system()).increment();
|
|
|
|
|
})
|
|
|
|
|
.build();
|
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
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|