From 9aad1a0a96758c3531bfa5f7cea4bda13ae3e810 Mon Sep 17 00:00:00 2001 From: Gustavo Martin Morcuende Date: Fri, 2 Dec 2016 21:58:55 +0100 Subject: [PATCH] Having fun with RxJava --- Allgemeines/RxJava/pom.xml | 100 +++++++++++++++++++++ .../src/main/java/de/rxjava/tests/MainRxJava.java | 12 +++ .../rxjava/tests/httpclient/CustomHTTPClient.java | 65 ++++++++++++++ .../rxjava/tests/service/impl/AsyncHTTPClient.java | 63 +++++++++++++ Allgemeines/RxJava/src/main/resources/log4j2.xml | 37 ++++++++ 5 files changed, 277 insertions(+) create mode 100644 Allgemeines/RxJava/pom.xml create mode 100644 Allgemeines/RxJava/src/main/java/de/rxjava/tests/MainRxJava.java create mode 100644 Allgemeines/RxJava/src/main/java/de/rxjava/tests/httpclient/CustomHTTPClient.java create mode 100644 Allgemeines/RxJava/src/main/java/de/rxjava/tests/service/impl/AsyncHTTPClient.java create mode 100644 Allgemeines/RxJava/src/main/resources/log4j2.xml diff --git a/Allgemeines/RxJava/pom.xml b/Allgemeines/RxJava/pom.xml new file mode 100644 index 0000000..b011e85 --- /dev/null +++ b/Allgemeines/RxJava/pom.xml @@ -0,0 +1,100 @@ + + + 4.0.0 + de.rxjava.tests + rxjava-tests + jar + 1.0-SNAPSHOT + rxjava-tests + http://gumartinm.name + SQL Tests + + Gustavo Martin Morcuende + http://www.gumartinm.name + + + scm:git:http://git.gumartinm.name/JavaForFun + http://git.gumartinm.name/JavaForFun + + + UTF-8 + + + + + org.apache.logging.log4j + log4j-slf4j-impl + 2.3 + + + + + org.apache.logging.log4j + log4j-core + 2.3 + + + + + org.slf4j + jcl-over-slf4j + 1.7.12 + + + + io.reactivex + rxjava + 1.2.3 + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.3 + + 1.8 + 1.8 + ${project.build.sourceEncoding} + -Xlint:deprecation + + + + org.apache.maven.plugins + maven-resources-plugin + 2.7 + + ${project.build.sourceEncoding} + + + + org.apache.maven.plugins + maven-jar-plugin + 2.4 + + + + ${project.description} + ${project.version} + ${project.organization.name} + ${project.description} + ${project.version} + ${project.organization.name} + + + + + + + diff --git a/Allgemeines/RxJava/src/main/java/de/rxjava/tests/MainRxJava.java b/Allgemeines/RxJava/src/main/java/de/rxjava/tests/MainRxJava.java new file mode 100644 index 0000000..09c0d70 --- /dev/null +++ b/Allgemeines/RxJava/src/main/java/de/rxjava/tests/MainRxJava.java @@ -0,0 +1,12 @@ +package de.rxjava.tests; + +import de.rxjava.tests.service.impl.AsyncHTTPClient; + +public class MainRxJava { + + public static void main(String[] args) { + AsyncHTTPClient asyncHTTPClient = new AsyncHTTPClient(); + + asyncHTTPClient.getPages(); + } +} diff --git a/Allgemeines/RxJava/src/main/java/de/rxjava/tests/httpclient/CustomHTTPClient.java b/Allgemeines/RxJava/src/main/java/de/rxjava/tests/httpclient/CustomHTTPClient.java new file mode 100644 index 0000000..d639b03 --- /dev/null +++ b/Allgemeines/RxJava/src/main/java/de/rxjava/tests/httpclient/CustomHTTPClient.java @@ -0,0 +1,65 @@ +/** + * Copyright 2014 Gustavo Martin Morcuende + * + * 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 de.rxjava.tests.httpclient; + +import java.io.BufferedInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.net.HttpURLConnection; +import java.net.URL; +import java.nio.charset.Charset; + +public class CustomHTTPClient { + private final String userAgent; + + private CustomHTTPClient(String userAgent) { + this.userAgent = userAgent; + } + + public String retrieveDataAsString(final URL url) throws IOException { + final HttpURLConnection connection = (HttpURLConnection) url.openConnection(); + try { + connection.setRequestProperty("User-Agent", userAgent); + connection.setRequestProperty("Cache-Control", "no-cache"); + final InputStream in = new BufferedInputStream(connection.getInputStream()); + final ByteArrayOutputStream buffer = readInputStream(in); + // No easy way of retrieving the charset from urlConnection.getContentType() + // Currently OpenWeatherMap returns: application/json; charset=utf-8 + // Let's hope they will not change the content-type :/ + return new String(buffer.toByteArray(), Charset.forName("UTF-8")); + } finally { + connection.disconnect(); + } + } + + private ByteArrayOutputStream readInputStream (final InputStream inputStream) throws IOException { + final ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream(); + final int bufferSize = 1024; + final byte[] buffer = new byte[bufferSize]; + + int len = 0; + while ((len = inputStream.read(buffer)) != -1) { + byteBuffer.write(buffer, 0, len); + } + + return byteBuffer; + } + + public static final CustomHTTPClient newInstance(final String userAgent) { + return new CustomHTTPClient(userAgent); + } +} diff --git a/Allgemeines/RxJava/src/main/java/de/rxjava/tests/service/impl/AsyncHTTPClient.java b/Allgemeines/RxJava/src/main/java/de/rxjava/tests/service/impl/AsyncHTTPClient.java new file mode 100644 index 0000000..5dd7ca2 --- /dev/null +++ b/Allgemeines/RxJava/src/main/java/de/rxjava/tests/service/impl/AsyncHTTPClient.java @@ -0,0 +1,63 @@ +package de.rxjava.tests.service.impl; + +import java.io.IOException; +import java.net.URL; + +import de.rxjava.tests.httpclient.CustomHTTPClient; +import rx.Observable; +import rx.schedulers.Schedulers; + +public class AsyncHTTPClient { + + public void getPages() { + + getDataAsync("https://github.com/gumartinm") + // fancy Java way of using lambdas. + .subscribe(System.out::println); //The subscribe method starts to run the code implemented in getDataSync + //subscribeOn just declares who is going to run my code (a pool of threads) + //subscribe is the guy who starts to run my code!!! + //JavaScript does the same with Promises but in a cleaner way (IMHO), it does not + //need a subscribe method for starting the machinery (the machinery is underneath + //implemented by the Web Browser with its asynchronous callbacks) + getDataAsync("http://www.google.de").subscribe(page -> { + System.out.println("Another way, no so cool (with lambdas)"); + System.out.println(Thread.currentThread().getName()); + System.out.println(page); + }); + + + System.out.println("YOU SEE ME FIRST!!!!"); + + + try { + Thread.sleep(30000); + } catch (InterruptedException exception) { + } + + } + + private Observable getDataAsync(String uri) { + return getDataSync(uri) + .subscribeOn(Schedulers.io()); // Creates a pool of threads for us which will run the code implemented below :) + // THIE METHOD DOES NOT START TO RUN MY CODE!!! IT IS DONE BY subscribe METHOD!!! + } + + private Observable getDataSync(String uri) { + return Observable.create(observer -> { + System.out.println(Thread.currentThread().getName()); + String data = ""; + try { + data = CustomHTTPClient.newInstance("RxJavaTest").retrieveDataAsString(new URL(uri)); + + // Making it slower as if having a bad connection :) + Thread.sleep(2000); + } catch (IOException | InterruptedException exception) { + observer.onError(exception); + } + + // When do you use this stuff? + // observer.onCompleted(); + observer.onNext(data); + }); + } +} diff --git a/Allgemeines/RxJava/src/main/resources/log4j2.xml b/Allgemeines/RxJava/src/main/resources/log4j2.xml new file mode 100644 index 0000000..2cf5f88 --- /dev/null +++ b/Allgemeines/RxJava/src/main/resources/log4j2.xml @@ -0,0 +1,37 @@ + + + + + + + + + + + + + + + + + + + + + + -- 2.1.4