Formatting java codes with sbt-java-formatter.

This commit is contained in:
hepin1989 2019-01-12 04:00:53 +08:00
parent 27500001ea
commit 998c5a9285
401 changed files with 19750 additions and 17450 deletions

View file

@ -48,15 +48,17 @@ public class IntegrationDocTest extends AbstractJavaTest {
@BeforeClass
public static void setup() {
final Config config = ConfigFactory.parseString("" +
"blocking-dispatcher { \n" +
" executor = thread-pool-executor \n" +
" thread-pool-executor { \n" +
" core-pool-size-min = 10 \n" +
" core-pool-size-max = 10 \n" +
" } \n" +
"} \n" +
"akka.actor.default-mailbox.mailbox-type = akka.dispatch.UnboundedMailbox\n");
final Config config =
ConfigFactory.parseString(
""
+ "blocking-dispatcher { \n"
+ " executor = thread-pool-executor \n"
+ " thread-pool-executor { \n"
+ " core-pool-size-min = 10 \n"
+ " core-pool-size-max = 10 \n"
+ " } \n"
+ "} \n"
+ "akka.actor.default-mailbox.mailbox-type = akka.dispatch.UnboundedMailbox\n");
system = ActorSystem.create("ActorPublisherDocTest", config);
mat = ActorMaterializer.create(system);
@ -71,28 +73,27 @@ public class IntegrationDocTest extends AbstractJavaTest {
ref = null;
}
class AddressSystem {
//#email-address-lookup
// #email-address-lookup
public CompletionStage<Optional<String>> lookupEmail(String handle)
//#email-address-lookup
{
// #email-address-lookup
{
return CompletableFuture.completedFuture(Optional.of(handle + "@somewhere.com"));
}
//#phone-lookup
// #phone-lookup
public CompletionStage<Optional<String>> lookupPhoneNumber(String handle)
//#phone-lookup
{
// #phone-lookup
{
return CompletableFuture.completedFuture(Optional.of("" + handle.hashCode()));
}
}
class AddressSystem2 {
//#email-address-lookup2
// #email-address-lookup2
public CompletionStage<String> lookupEmail(String handle)
//#email-address-lookup2
{
// #email-address-lookup2
{
return CompletableFuture.completedFuture(handle + "@somewhere.com");
}
}
@ -186,18 +187,17 @@ public class IntegrationDocTest extends AbstractJavaTest {
this.probe = probe;
}
//#email-server-send
// #email-server-send
public CompletionStage<Email> send(Email email) {
// ...
//#email-server-send
// #email-server-send
probe.tell(email.to, ActorRef.noSender());
return CompletableFuture.completedFuture(email);
//#email-server-send
// #email-server-send
}
//#email-server-send
// #email-server-send
}
static class SmsServer {
public final ActorRef probe;
@ -205,15 +205,15 @@ public class IntegrationDocTest extends AbstractJavaTest {
this.probe = probe;
}
//#sms-server-send
// #sms-server-send
public boolean send(TextMessage text) {
// ...
//#sms-server-send
// #sms-server-send
probe.tell(text.to, ActorRef.noSender());
//#sms-server-send
// #sms-server-send
return true;
}
//#sms-server-send
// #sms-server-send
}
static class Save {
@ -246,12 +246,12 @@ public class IntegrationDocTest extends AbstractJavaTest {
return tweet != null ? tweet.hashCode() : 0;
}
}
static class SaveDone {
public static SaveDone INSTANCE = new SaveDone();
private SaveDone() {
}
}
private SaveDone() {}
}
static class DatabaseService extends AbstractActor {
public final ActorRef probe;
@ -263,15 +263,17 @@ public class IntegrationDocTest extends AbstractJavaTest {
@Override
public Receive createReceive() {
return receiveBuilder()
.match(Save.class, s -> {
probe.tell(s.tweet.author.handle, ActorRef.noSender());
getSender().tell(SaveDone.INSTANCE, getSelf());
})
.build();
.match(
Save.class,
s -> {
probe.tell(s.tweet.author.handle, ActorRef.noSender());
getSender().tell(SaveDone.INSTANCE, getSelf());
})
.build();
}
}
//#sometimes-slow-service
// #sometimes-slow-service
static class SometimesSlowService {
private final Executor ec;
@ -283,46 +285,63 @@ public class IntegrationDocTest extends AbstractJavaTest {
public CompletionStage<String> convert(String s) {
System.out.println("running: " + s + "(" + runningCount.incrementAndGet() + ")");
return CompletableFuture.supplyAsync(() -> {
if (!s.isEmpty() && Character.isLowerCase(s.charAt(0)))
try { Thread.sleep(500); } catch (InterruptedException e) {}
else
try { Thread.sleep(20); } catch (InterruptedException e) {}
System.out.println("completed: " + s + "(" + runningCount.decrementAndGet() + ")");
return s.toUpperCase();
}, ec);
return CompletableFuture.supplyAsync(
() -> {
if (!s.isEmpty() && Character.isLowerCase(s.charAt(0)))
try {
Thread.sleep(500);
} catch (InterruptedException e) {
}
else
try {
Thread.sleep(20);
} catch (InterruptedException e) {
}
System.out.println("completed: " + s + "(" + runningCount.decrementAndGet() + ")");
return s.toUpperCase();
},
ec);
}
}
//#sometimes-slow-service
// #sometimes-slow-service
//#ask-actor
// #ask-actor
static class Translator extends AbstractActor {
@Override
public Receive createReceive() {
return receiveBuilder()
.match(String.class, word -> {
// ... process message
String reply = word.toUpperCase();
// reply to the ask
getSender().tell(reply, getSelf());
})
.build();
.match(
String.class,
word -> {
// ... process message
String reply = word.toUpperCase();
// reply to the ask
getSender().tell(reply, getSelf());
})
.build();
}
}
//#ask-actor
// #ask-actor
//#actorRefWithAck-actor
// #actorRefWithAck-actor
enum Ack {
INSTANCE;
}
static class StreamInitialized {}
static class StreamCompleted {}
static class StreamFailure {
private final Throwable cause;
public StreamFailure(Throwable cause) { this.cause = cause; }
public Throwable getCause() { return cause; }
public StreamFailure(Throwable cause) {
this.cause = cause;
}
public Throwable getCause() {
return cause;
}
}
static class AckingReceiver extends AbstractLoggingActor {
@ -336,72 +355,76 @@ public class IntegrationDocTest extends AbstractJavaTest {
@Override
public Receive createReceive() {
return receiveBuilder()
.match(StreamInitialized.class, init -> {
log().info("Stream initialized");
probe.tell("Stream initialized", getSelf());
sender().tell(Ack.INSTANCE, self());
})
.match(String.class, element -> {
log().info("Received element: {}", element);
probe.tell(element, getSelf());
sender().tell(Ack.INSTANCE, self());
})
.match(StreamCompleted.class, completed -> {
log().info("Stream completed");
probe.tell("Stream completed", getSelf());
})
.match(StreamFailure.class, failed -> {
log().error(failed.getCause(),"Stream failed!");
probe.tell("Stream failed!", getSelf());
})
.build();
.match(
StreamInitialized.class,
init -> {
log().info("Stream initialized");
probe.tell("Stream initialized", getSelf());
sender().tell(Ack.INSTANCE, self());
})
.match(
String.class,
element -> {
log().info("Received element: {}", element);
probe.tell(element, getSelf());
sender().tell(Ack.INSTANCE, self());
})
.match(
StreamCompleted.class,
completed -> {
log().info("Stream completed");
probe.tell("Stream completed", getSelf());
})
.match(
StreamFailure.class,
failed -> {
log().error(failed.getCause(), "Stream failed!");
probe.tell("Stream failed!", getSelf());
})
.build();
}
}
//#actorRefWithAck-actor
// #actorRefWithAck-actor
@SuppressWarnings("unchecked")
@Test
public void askStage() throws Exception {
//#ask
Source<String, NotUsed> words =
Source.from(Arrays.asList("hello", "hi"));
// #ask
Source<String, NotUsed> words = Source.from(Arrays.asList("hello", "hi"));
Timeout askTimeout = Timeout.apply(5, TimeUnit.SECONDS);
words
.ask(5, ref, String.class, askTimeout)
// continue processing of the replies from the actor
.map(elem -> elem.toLowerCase())
.runWith(Sink.ignore(), mat);
//#ask
.ask(5, ref, String.class, askTimeout)
// continue processing of the replies from the actor
.map(elem -> elem.toLowerCase())
.runWith(Sink.ignore(), mat);
// #ask
}
@Test
public void actorRefWithAckExample() throws Exception {
//#actorRefWithAck
Source<String, NotUsed> words =
Source.from(Arrays.asList("hello", "hi"));
// #actorRefWithAck
Source<String, NotUsed> words = Source.from(Arrays.asList("hello", "hi"));
final TestKit probe = new TestKit(system);
ActorRef receiver =
system.actorOf(Props.create(AckingReceiver.class, probe.getRef()));
ActorRef receiver = system.actorOf(Props.create(AckingReceiver.class, probe.getRef()));
Sink<String, NotUsed> sink = Sink.<String>actorRefWithAck(receiver,
new StreamInitialized(),
Ack.INSTANCE,
new StreamCompleted(),
ex -> new StreamFailure(ex)
);
Sink<String, NotUsed> sink =
Sink.<String>actorRefWithAck(
receiver,
new StreamInitialized(),
Ack.INSTANCE,
new StreamCompleted(),
ex -> new StreamFailure(ex));
words
.map(el -> el.toLowerCase())
.runWith(sink, mat);
words.map(el -> el.toLowerCase()).runWith(sink, mat);
probe.expectMsg("Stream initialized");
probe.expectMsg("hello");
probe.expectMsg("hi");
probe.expectMsg("Stream completed");
//#actorRefWithAck
// #actorRefWithAck
}
@Test
@ -412,29 +435,30 @@ public class IntegrationDocTest extends AbstractJavaTest {
final EmailServer emailServer = new EmailServer(probe.getRef());
{
//#tweet-authors
final Source<Author, NotUsed> authors = tweets
.filter(t -> t.hashtags().contains(AKKA))
.map(t -> t.author);
// #tweet-authors
final Source<Author, NotUsed> authors =
tweets.filter(t -> t.hashtags().contains(AKKA)).map(t -> t.author);
//#tweet-authors
// #tweet-authors
//#email-addresses-mapAsync
final Source<String, NotUsed> emailAddresses = authors
.mapAsync(4, author -> addressSystem.lookupEmail(author.handle))
.filter(o -> o.isPresent())
.map(o -> o.get());
// #email-addresses-mapAsync
final Source<String, NotUsed> emailAddresses =
authors
.mapAsync(4, author -> addressSystem.lookupEmail(author.handle))
.filter(o -> o.isPresent())
.map(o -> o.get());
//#email-addresses-mapAsync
// #email-addresses-mapAsync
//#send-emails
final RunnableGraph<NotUsed> sendEmails = emailAddresses
.mapAsync(4, address ->
emailServer.send(new Email(address, "Akka", "I like your tweet")))
.to(Sink.ignore());
// #send-emails
final RunnableGraph<NotUsed> sendEmails =
emailAddresses
.mapAsync(
4, address -> emailServer.send(new Email(address, "Akka", "I like your tweet")))
.to(Sink.ignore());
sendEmails.run(mat);
//#send-emails
// #send-emails
probe.expectMsg("rolandkuhn@somewhere.com");
probe.expectMsg("patriknw@somewhere.com");
@ -454,20 +478,19 @@ public class IntegrationDocTest extends AbstractJavaTest {
final AddressSystem2 addressSystem = new AddressSystem2();
{
final Source<Author, NotUsed> authors = tweets
.filter(t -> t.hashtags().contains(AKKA))
.map(t -> t.author);
final Source<Author, NotUsed> authors =
tweets.filter(t -> t.hashtags().contains(AKKA)).map(t -> t.author);
//#email-addresses-mapAsync-supervision
// #email-addresses-mapAsync-supervision
final Attributes resumeAttrib =
ActorAttributes.withSupervisionStrategy(Supervision.getResumingDecider());
ActorAttributes.withSupervisionStrategy(Supervision.getResumingDecider());
final Flow<Author, String, NotUsed> lookupEmail =
Flow.of(Author.class)
.mapAsync(4, author -> addressSystem.lookupEmail(author.handle))
.withAttributes(resumeAttrib);
.mapAsync(4, author -> addressSystem.lookupEmail(author.handle))
.withAttributes(resumeAttrib);
final Source<String, NotUsed> emailAddresses = authors.via(lookupEmail);
//#email-addresses-mapAsync-supervision
// #email-addresses-mapAsync-supervision
}
};
}
@ -480,26 +503,24 @@ public class IntegrationDocTest extends AbstractJavaTest {
final EmailServer emailServer = new EmailServer(probe.ref());
{
//#external-service-mapAsyncUnordered
// #external-service-mapAsyncUnordered
final Source<Author, NotUsed> authors =
tweets
.filter(t -> t.hashtags().contains(AKKA))
.map(t -> t.author);
tweets.filter(t -> t.hashtags().contains(AKKA)).map(t -> t.author);
final Source<String, NotUsed> emailAddresses =
authors
.mapAsyncUnordered(4, author -> addressSystem.lookupEmail(author.handle))
.filter(o -> o.isPresent())
.map(o -> o.get());
authors
.mapAsyncUnordered(4, author -> addressSystem.lookupEmail(author.handle))
.filter(o -> o.isPresent())
.map(o -> o.get());
final RunnableGraph<NotUsed> sendEmails =
emailAddresses
.mapAsyncUnordered(4, address ->
emailServer.send(new Email(address, "Akka", "I like your tweet")))
.to(Sink.ignore());
emailAddresses
.mapAsyncUnordered(
4, address -> emailServer.send(new Email(address, "Akka", "I like your tweet")))
.to(Sink.ignore());
sendEmails.run(mat);
//#external-service-mapAsyncUnordered
// #external-service-mapAsyncUnordered
}
};
}
@ -513,25 +534,29 @@ public class IntegrationDocTest extends AbstractJavaTest {
{
final Source<Author, NotUsed> authors =
tweets
.filter(t -> t.hashtags().contains(AKKA))
.map(t -> t.author);
tweets.filter(t -> t.hashtags().contains(AKKA)).map(t -> t.author);
final Source<String, NotUsed> phoneNumbers = authors.mapAsync(4, author -> addressSystem.lookupPhoneNumber(author.handle))
.filter(o -> o.isPresent())
.map(o -> o.get());
final Source<String, NotUsed> phoneNumbers =
authors
.mapAsync(4, author -> addressSystem.lookupPhoneNumber(author.handle))
.filter(o -> o.isPresent())
.map(o -> o.get());
//#blocking-mapAsync
// #blocking-mapAsync
final Executor blockingEc = system.dispatchers().lookup("blocking-dispatcher");
final RunnableGraph<NotUsed> sendTextMessages =
phoneNumbers
.mapAsync(4, phoneNo -> CompletableFuture.supplyAsync(() ->
smsServer.send(new TextMessage(phoneNo, "I like your tweet")), blockingEc))
.to(Sink.ignore());
phoneNumbers
.mapAsync(
4,
phoneNo ->
CompletableFuture.supplyAsync(
() -> smsServer.send(new TextMessage(phoneNo, "I like your tweet")),
blockingEc))
.to(Sink.ignore());
sendTextMessages.run(mat);
//#blocking-mapAsync
// #blocking-mapAsync
final List<Object> got = receiveN(7);
final Set<Object> set = new HashSet<>(got);
@ -557,24 +582,23 @@ public class IntegrationDocTest extends AbstractJavaTest {
{
final Source<Author, NotUsed> authors =
tweets
.filter(t -> t.hashtags().contains(AKKA))
.map(t -> t.author);
tweets.filter(t -> t.hashtags().contains(AKKA)).map(t -> t.author);
final Source<String, NotUsed> phoneNumbers = authors.mapAsync(4, author -> addressSystem.lookupPhoneNumber(author.handle))
.filter(o -> o.isPresent())
.map(o -> o.get());
final Source<String, NotUsed> phoneNumbers =
authors
.mapAsync(4, author -> addressSystem.lookupPhoneNumber(author.handle))
.filter(o -> o.isPresent())
.map(o -> o.get());
//#blocking-map
// #blocking-map
final Flow<String, Boolean, NotUsed> send =
Flow.of(String.class)
.map(phoneNo -> smsServer.send(new TextMessage(phoneNo, "I like your tweet")))
.withAttributes(ActorAttributes.dispatcher("blocking-dispatcher"));
final RunnableGraph<?> sendTextMessages =
phoneNumbers.via(send).to(Sink.ignore());
Flow.of(String.class)
.map(phoneNo -> smsServer.send(new TextMessage(phoneNo, "I like your tweet")))
.withAttributes(ActorAttributes.dispatcher("blocking-dispatcher"));
final RunnableGraph<?> sendTextMessages = phoneNumbers.via(send).to(Sink.ignore());
sendTextMessages.run(mat);
//#blocking-map
// #blocking-map
probe.expectMsg(String.valueOf("rolandkuhn".hashCode()));
probe.expectMsg(String.valueOf("patriknw".hashCode()));
@ -593,17 +617,18 @@ public class IntegrationDocTest extends AbstractJavaTest {
final TestProbe probe = new TestProbe(system);
final EmailServer emailServer = new EmailServer(probe.ref());
final ActorRef database = system.actorOf(Props.create(DatabaseService.class, probe.ref()), "db");
final ActorRef database =
system.actorOf(Props.create(DatabaseService.class, probe.ref()), "db");
{
//#save-tweets
// #save-tweets
final Source<Tweet, NotUsed> akkaTweets = tweets.filter(t -> t.hashtags().contains(AKKA));
final RunnableGraph<NotUsed> saveTweets =
akkaTweets
.mapAsync(4, tweet -> ask(database, new Save(tweet), Duration.ofMillis(300L)))
.to(Sink.ignore());
//#save-tweets
akkaTweets
.mapAsync(4, tweet -> ask(database, new Save(tweet), Duration.ofMillis(300L)))
.to(Sink.ignore());
// #save-tweets
saveTweets.run(mat);
@ -627,28 +652,33 @@ public class IntegrationDocTest extends AbstractJavaTest {
class MockSystem {
class Println {
public <T> void println(T s) {
if (s.toString().startsWith("after:"))
probe.ref().tell(s, ActorRef.noSender());
if (s.toString().startsWith("after:")) probe.ref().tell(s, ActorRef.noSender());
}
}
public final Println out = new Println();
}
private final MockSystem System = new MockSystem();
{
//#sometimes-slow-mapAsync
// #sometimes-slow-mapAsync
final Executor blockingEc = system.dispatchers().lookup("blocking-dispatcher");
final SometimesSlowService service = new SometimesSlowService(blockingEc);
final ActorMaterializer mat = ActorMaterializer.create(
ActorMaterializerSettings.create(system).withInputBuffer(4, 4), system);
final ActorMaterializer mat =
ActorMaterializer.create(
ActorMaterializerSettings.create(system).withInputBuffer(4, 4), system);
Source.from(Arrays.asList("a", "B", "C", "D", "e", "F", "g", "H", "i", "J"))
.map(elem -> { System.out.println("before: " + elem); return elem; })
.mapAsync(4, service::convert)
.runForeach(elem -> System.out.println("after: " + elem), mat);
//#sometimes-slow-mapAsync
.map(
elem -> {
System.out.println("before: " + elem);
return elem;
})
.mapAsync(4, service::convert)
.runForeach(elem -> System.out.println("after: " + elem), mat);
// #sometimes-slow-mapAsync
probe.expectMsg("after: A");
probe.expectMsg("after: B");
@ -672,28 +702,33 @@ public class IntegrationDocTest extends AbstractJavaTest {
class MockSystem {
class Println {
public <T> void println(T s) {
if (s.toString().startsWith("after:"))
getRef().tell(s, ActorRef.noSender());
if (s.toString().startsWith("after:")) getRef().tell(s, ActorRef.noSender());
}
}
public final Println out = new Println();
}
private final MockSystem System = new MockSystem();
{
//#sometimes-slow-mapAsyncUnordered
// #sometimes-slow-mapAsyncUnordered
final Executor blockingEc = system.dispatchers().lookup("blocking-dispatcher");
final SometimesSlowService service = new SometimesSlowService(blockingEc);
final ActorMaterializer mat = ActorMaterializer.create(
ActorMaterializerSettings.create(system).withInputBuffer(4, 4), system);
final ActorMaterializer mat =
ActorMaterializer.create(
ActorMaterializerSettings.create(system).withInputBuffer(4, 4), system);
Source.from(Arrays.asList("a", "B", "C", "D", "e", "F", "g", "H", "i", "J"))
.map(elem -> { System.out.println("before: " + elem); return elem; })
.mapAsyncUnordered(4, service::convert)
.runForeach(elem -> System.out.println("after: " + elem), mat);
//#sometimes-slow-mapAsyncUnordered
.map(
elem -> {
System.out.println("before: " + elem);
return elem;
})
.mapAsyncUnordered(4, service::convert)
.runForeach(elem -> System.out.println("after: " + elem), mat);
// #sometimes-slow-mapAsyncUnordered
final List<Object> got = receiveN(10);
final Set<Object> set = new HashSet<>(got);
@ -716,7 +751,7 @@ public class IntegrationDocTest extends AbstractJavaTest {
public void illustrateSourceQueue() throws Exception {
new TestKit(system) {
{
//#source-queue
// #source-queue
int bufferSize = 5;
int elementsToProcess = 3;
@ -727,14 +762,12 @@ public class IntegrationDocTest extends AbstractJavaTest {
.to(Sink.foreach(x -> System.out.println("got: " + x)))
.run(mat);
Source<Integer, NotUsed> source
= Source.from(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10));
Source<Integer, NotUsed> source = Source.from(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10));
source.map(x -> sourceQueue.offer(x)).runWith(Sink.ignore(), mat);
//#source-queue
// #source-queue
}
};
}
}