Month: November 2017

Simple reactive HTTP client & server with RxJava, Vert.x and Android

2017-11-09 Android, Java, RxJava, Vert.x No comments

During Hack Your Career event at the Silesian University of Technology, I’ve prepared a presentation titled Reactive Programming – Efficient Server Applications with a colleague from work. Arek told about theory of Reactive Programming, shown basic concepts, data types and a few examples in the code. During my part of the presentation, I’ve wrote a very simple server and client in Java (9 on the server, 7 on the client) with Vert.x (Core and Rx), RxJava 2, OkHttp 3, Android and RxAndroid. Presentation was targeted mainly to the university students with no experience with reactive programming, but it was an open event and anyone could attend it.

Below, we can see a very simple code snippet showing how to create a reactive HTTP server with Vert.x. We can create a stream of requests, make Flowable out of it, apply any kind of RxJava 2 operator including backpressure handling and subscribe the stream. Moreover, we can also reactively start the server with rxListen(int port) method. This is just a basic example, where will be sending request to the only one endpoint. In the case, when we want to handle more endpoints, we can use vertx-web library and design REST API.

final HttpServer server = Vertx
    .vertx()
    .createHttpServer();

server
    .requestStream()
    .toFlowable()
    .onBackpressureDrop()
    .subscribe(request -> {
      logger.info("{} {}", request.rawMethod(), request.absoluteURI());
      request.response().end("request received");
    });

server
    .rxListen(8080)
    .subscribe(httpServer -> logger.info("server is running at port 8080..."));

We can build this server with Gradle as follows:

./gradlew shadowJar

and then, we can run it:

java -jar build/libs/server-fat.jar

Our client will be an Android application, which will read data from the accelerometer sensor, send it to the server and display it in the TextView on a mobile device. We will use ReactiveSensors library (which was recently migrated to RxJava 2) for getting sensor readings as a Flowable data stream. Next, we will apply backpressure DROP strategy, filter only events of changing sensors (we neglect changing of the accuracy), read only one event per one second with throttleLast(int, TimeUnit) operator and map event to a String with device coordinates. Next, we are ready to send data with Completable performRequest(String), which we created earlier. Sensors readings are acquired in the computation() scheduler, send to the server with io() scheduler and passed to the UI thread on Android with AndroidSchedulers.mainThread(). Distributing operations to the different schedulers is made with subscribeOn(Scheduler) and observeOn(Scheduler).

reactiveSensors
    .observeSensor(Sensor.TYPE_ACCELEROMETER)
    .onBackpressureDrop()
    .filter(ReactiveSensorFilter.filterSensorChanged())
    .throttleLast(1, TimeUnit.SECONDS)
    .map(this::getSensorReading)
    .doOnNext(event -> performRequest(event)
        .subscribeOn(Schedulers.io())
        .subscribe())
    .subscribeOn(Schedulers.computation())
    .observeOn(AndroidSchedulers.mainThread())
    .subscribe(event -> tvReadings.setText(event));

It’s worth noticing that Completable performRequest(String) is using OkHttp 3 under the hood as a HTTP client, because it’s very simple example with one endpoint. In the case, we want to handle more endpoints on the client-side, it’s better to use Retrofit library.

It’s also interesting that in our case, we can simulate behavior of the accelerometer and other sensors with the latest Android device emulator available in the Android SDK. It works surprisingly smooth.

Complete working example can be found at: https://github.com/pwittchen/reactive-client-server.

Later, I’ve also shown, how to use RxJava to distribute computational operations to a different threads of the CPU cores, but I’ll probably publish a separate article about that on this blog. It was the same example I shown during my JDD presentation this year.

Slides from my part of the presentation are available below.

5 ways to attend expensive IT conferences for (almost) free

2017-11-02 Conferences No comments

Introduction

In the past months, I’ve attended a few conferences and meetups related to IT. I also plan to attend a few more in the last quarter of the year as a speaker. I’d like to show you a few tips, which can encourage you to attend such events, be more involved in them and spend some time away from your desk ;-). If you are working in a software company, in the most cases you could treat going for a conference as a non-customer facing business trip, so your regular days off will be safe and can be used for your personal plans and holidays :-).
I tried all of the methods described in this article both in Poland and abroad, so I can tell you they’re confirmed in practice.

Use your company’s educational budget

There are companies, which offer so called educational budget, which is defined each year. Such budget can be used for attending the conferences and trainings. In the company where I work now (SAP Hybris), I have such benefit and I could use it to go to the Spring I/O in the Barcelona, Spain last year, J On The Beach in Malaga, Spain and Devoxx in Kraków, Poland this year. Of course, travel, food and accommodation expanses were covered by this budget as well as conference tickets.


ICE Kraków Congress Centre – Devoxx Poland 2017 Venue


Malaga, Spain

Be a speaker

This year I was a speaker during JDD conference in Kraków, Poland. I had confirmed business trip and an accommodation in high quality hotel paid by the conference organizers. I could also attend a dinner with other conference speakers and see all conference talks and hang out with participants without spending a penny. In the December 2017, I’m also going to be a speaker during DroidCon Poland conference, where organizers offered to cover accommodation costs as well. Usually, you can expect that from good quality events, but it may happen that part of the costs will have to be covered by you, so it’s good to be prepared for that.


My JDD 2017 Conference Talk in Kraków, Poland

Be a rejected speaker

I’ve send my submission during Call For Papers for GDG Dev Fest in Lviv, Ukraine. I was rejected, but organizers offered me a free conference ticket. Organizers had over 150 C4P submissions including those of people from Google, Twitter, etc., so I wasn’t lucky this time. Ukraine is quite cheap country comparing to Poland and I wanted to visit Lviv, so I paid for travel and accommodation by myself and joined this event.


GDG Dev Fest Ukraine 2017 Conference in Lviv


View from Pravda Beer Theatre on the Market Square in Lviv, Ukraine

Be a blogger

I create this blog for a few years and I think its quality is becoming better and better every year and more people visit it. Last year, JDD conference organizers offered me a free conference ticket in exchange for promoting their conference on my blog. It was good exchange, because this event is interesting and related to the content of my website. In addition, conference ticket is not so cheap and costs about ~500 PLN or something like that. This situation encouraged me to be more engaged in the Java community and this year I attended the same conference as a speaker.

Be a volunteer

From a long time I wanted to visit London in UK and attend DroidCon London conference as well. It’s one of the most popular Android conferences in the Europe. Conference ticket is very expansive and costs 695 GBP, which is approximately ~3352 PLN. Nevertheless, you can save some money if you buy it very early (like almost one year before the conference). I used most of my personal educational budget in the company and I could, but I didn’t want to spend such amount of money from my own pocket just for a conference ticket. I’ve sent to the organizers a proposal of promotion of their event on my blog in exchange for a free conference ticket. They didn’t really want that, but they asked me to be a volunteer for a 2 days, so I could help them a little and get the free ticket. I decided to go for it. UK is much more expansive place than any city in Poland, so I had to spend some money for accommodation, food and transportation. Nevertheless, I don’t regret it, because it was nice experience and London is worth visiting at least for a few days. Moreover, I haven’t really encountered typical London weather. There was hot and sunny during my stay. Only one day was a bit cloudy with a small rain, but it wasn’t as cold as in Poland on my way back. I also made a few connections and gave my business cards to a few people. Such kind of networking can create new possibilities related to work, local & global communities, speaking on meetups, conferences etc. Of course, when you go to UK, don’t forget to check out the map of every pub in UK.


Business Design Centre London – DroidCon London 2017 Venue


London, UK in late October 2017 (The Shard is visible in the background)


Tower Bridge visible from The Shard, London, UK

Summary

As you can see, there are many ways to attend expensive IT conferences and you don’t really have to spend your own money on the tickets every time. You just need to be in a good company, a bit more engaged, give something from yourself to others and it will bring you profits. Attending many conferences won’t make you the best developer in the world, but sitting in front of your desk and doing the same thing for many days won’t do that either. It’s good to find a balance and time for the new experiences, changing your surroundings for some time, seeing new places and meeting new people. When you can mix that with your profession and work, that’s great!