Last active
October 29, 2021 21:26
-
-
Save NiteshKant/89e26e5c04df0910cb8536013cbbc296 to your computer and use it in GitHub Desktop.
HTTP Helloworld
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright © 2018 Apple Inc. and the ServiceTalk project authors | |
* | |
* Licensed under the Apache License, Version 2.0 (the "License"); | |
* you may not use this file except in compliance with the License. | |
* You may obtain a copy of the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software | |
* distributed under the License is distributed on an "AS IS" BASIS, | |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
* See the License for the specific language governing permissions and | |
* limitations under the License. | |
*/ | |
package io.servicetalk.examples.http.helloworld.async.streaming; | |
import io.servicetalk.http.api.StreamingHttpClient; | |
import io.servicetalk.http.api.StreamingHttpResponse; | |
import io.servicetalk.http.netty.HttpClients; | |
import java.nio.charset.Charset; | |
import java.time.Duration; | |
import static io.servicetalk.http.api.HttpSerializers.appSerializerUtf8FixLen; | |
public final class HelloWorldStreamingClient { | |
public static void main(String[] args) throws Exception { | |
try (StreamingHttpClient client = HttpClients.forSingleAddress("localhost", 8082).buildStreaming()) { | |
client.request(client.get("/sayHello") | |
.payloadBody(client.executionContext().executor() | |
.timer(Duration.ofSeconds(1)) | |
.repeat(__ -> true) | |
.map(__ -> client.executionContext().bufferAllocator().fromAscii("foo")) | |
.whenOnNext(buffer -> System.out.println("Sending buffer: " + buffer.toString(Charset.defaultCharset()))))) | |
.beforeOnSuccess(response -> System.out.println(response.toString((name, value) -> value))) | |
.flatMapPublisher(StreamingHttpResponse::payloadBody) | |
.ignoreElements() | |
.retry((i, throwable) -> { | |
System.out.println("Retrying .."); | |
return true; | |
}) | |
.toFuture().get(); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright © 2018 Apple Inc. and the ServiceTalk project authors | |
* | |
* Licensed under the Apache License, Version 2.0 (the "License"); | |
* you may not use this file except in compliance with the License. | |
* You may obtain a copy of the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software | |
* distributed under the License is distributed on an "AS IS" BASIS, | |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
* See the License for the specific language governing permissions and | |
* limitations under the License. | |
*/ | |
package io.servicetalk.examples.http.helloworld.async.streaming; | |
import io.servicetalk.http.netty.HttpServers; | |
import java.nio.charset.Charset; | |
import static io.servicetalk.concurrent.api.Publisher.from; | |
import static io.servicetalk.concurrent.api.Single.succeeded; | |
import static io.servicetalk.http.api.HttpSerializers.appSerializerUtf8FixLen; | |
public final class HelloWorldStreamingServer { | |
public static void main(String[] args) throws Exception { | |
HttpServers.forPort(8082) | |
.listenStreamingAndAwait((ctx, request, responseFactory) -> | |
succeeded(responseFactory.ok() | |
.payloadBody(request.payloadBody() | |
.whenOnNext(buffer -> System.out.println("Received buffer: " + buffer.toString(Charset.defaultCharset())))))) | |
.awaitShutdown(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment