Notes from react.sphere 2018

https://www.gremlin.com – chaos engineering tool (aka symian army from netflix)

highscalability.com – solutions for high scalability of internet giants

http://nurkiewicz.github.io/talks/2018/reactive-lessons/#/18 – slides from presentation of Tomasz Nurkiewicz

https://theburningmonk.com – articles about serverless (AWS lambda in particular)

https://github.com/codepitbull/vertx-scala-kubernetes-demo

Misc thoughts

Never mock a type you don’t own //context is lost, perhaps taken from here

Lambda per verb (i.e. delete user) – allows permissions segregation, better finance tracking, better insight on capabilities (it’s clear what it can do based on it’s name) //advice from theburningmonk

Write metrics to log to avoid io waits //advice from theburningmonk, mentioned in the context of lambdas topic

Notes from JDD 2016

Topic: Flying to cloud
Main idea: How to move an app to the cloud, how easily this can be done with spring-cloud, spring wraps lots of libraries from netflix to make it integrated with its own ecosystem + adds some default behavior

Good book: Migrating to cloud native application architecture (http://12factor.net)
Possible problems: Global locks, leadership election

Spring cloud benefits:
Circuit breaker (histryx) works OOTB
@FeignClient and @EnableFeignClients allows to group replicated clients and add load balancing (with Ribbon annotation), also OOTB

media-20161010-1 media-20161010-2 media-20161010

 


Topic: Fast feedback
Main idea: Lots of upcoming problems could be solved by achieving the fast feedback in many different areas. Author describes many techniques he uses on his projects

When throwing exception: add some context on different stages, then rethrow – this practice eliminates time spent on the issue analysis
mvn – T – allows parallel build execution (has many options):

mvn -T 4 clean install # Builds with 4 threads
mvn -T 1C clean install # 1 thread per cpu core
mvn -T 1.5C clean install # 1.5 thread per cpu core

Static analysis:

  • use blacklist (filter) instead of whitelist of rules, to stay up to date with new rules suggested by PMD, FindBugs etc.
  • ben-manes plugin scans your project and shows if there are new libraries to use
  • mutation testing: pitest.org
  • NonDex – analyze wrong assumptions about Java api
  • CodeNarc – static code analyzer for Groovy

Topic: Security
Main idea: Author gives some advices on how to keep quality of the security on the project

Letsencrypt – gives free SSL certificates (yet you should pay for hosting of the certificate)
OWASP ASVS – security standards for developer
ZAP Zed attack proxy – helps to find security vulnerabilities
SAMM Overview – maturity model


Topic: Non-blocking microservices
Main idea: How one team moved their code and infrastructure to non-blocking IO

Someone had calculated that 1 thread ~1mb, which costs 8$ per year to maintain
First they’ve looked into JAX-RX 2.0 which supports async responses.
BUT:
Non-blocking servlet servlet will require non-blocking filters, which have to be written customly
So they moved to pure HTTP.
CompletableFuture (Java 8) allows to chain futures (like promises in JS).
Migration requires functional decomposition, to allow easy chaining.
Recommended Netty + async http client.
Move to NIO rather in case of low CPU loads, to benefit


Topic: Metrics gathering
Main idea: How one team gathered metrics

They’re using io.dropwizard.metrics:
– metrics-core
– metrics-spring – spring integration
– metrics-jvm – collect gc metrics
Works with @Timed annotation

Graphite and graphana – ui and storage for metrics
Read More

Apace CXF for REST application

I started to configure my first RESTful app using Apache CXF library and bumped into issue with JAX-RS binding. No matter how I tried to map my classes, I’ve still been getting this error in response instead of JSON data:

No message body writer has been found for response class ArrayList.

The solution was to add dependency on JSON mapping provider since Apache CXF does not include it. It can be XStream or Jackson, I used the last one.

Step 1.

Add dependency for org.codehaus.jackson:jackson-jaxrs to your project

Step 2.

Add JSON provider to Spring context configuration:

<beans xmlns="http://www.springframework.org/schema/beans"...>
...
    <jaxrs:server id="serviceId">
        <jaxrs:serviceBeans>
            <ref bean="myService"/>
        </jaxrs:serviceBeans>
        <jaxrs:providers>
            <bean class="org.codehaus.jackson.jaxrs.JacksonJsonProvider"/>
        </jaxrs:providers>
    </jaxrs:server>
...
</beans>

 

GWT Designer for Eclipse Juno on Ubuntu 12.X

Today I’ve tried to install Google Eclipse Plugin to draw some forms for my GWT-based app and caught the exception:

GWT http-server started at 127.0.0.1:45658
#
# A fatal error has been detected by the Java Runtime Environment:
#
#  SIGSEGV (0xb) at pc=0x00007fb2229d45a0, pid=8150, tid=140404439688960
#
# JRE version: 6.0_37-b06
# Java VM: Java HotSpot(TM) 64-Bit Server VM (20.12-b01 mixed mode linux-amd64 compressed oops)
# Problematic frame:
# C  [libwebkitgtk-1.0.so.0+0x14245a0]  void WTF::freeOwnedGPtr<_GdkEvent>(_GdkEvent*)+0x15df0
#
# An error report file with more information is saved as:
# /home/ipcreeper/hs_err_pid8150.log
#
# If you would like to submit a bug report, please visit:
#   http://java.sun.com/webapps/bugreport/crash.jsp
# The crash happened outside the Java Virtual Machine in native code.
# See problematic frame for where to report the bug.
#

Searching for at least an hour I’ve finally found the solution:

 

Step 1:

Download and install libhunspell package (xulrunner has a dependency on it):

http://packages.ubuntu.com/oneiric/amd64/libhunspell-1.2-0/download

sudo dpkg -i libhunspell-1.2-0_1.2.14-4_amd64.deb

Step 2:

Download and install xulrunner package:

http://packages.ubuntu.com/lucid/amd64/xulrunner-1.9.2/download

sudo dpkg -i xulrunner-1.9.2_1.9.2.28+build1+nobinonly-0ubuntu0.11.04.1_amd64.deb

Be aware that both libhunspell and xulrunner should be either i386 or amd64 version.

Step 3:

Open eclipse.ini and add path to installed xulrunner:
-Dorg.eclipse.swt.browser.XULRunnerPath=/usr/lib/xulrunner-1.9.2.28

Step 4:

If editor still not running, try to set the environment variable GDK_NATIVE_WINDOWS to false before starting Eclipse. Do it with following command:

export GDK_NATIVE_WINDOWS=false

How to switch version of Java in Ubuntu

Had a little headache trying to change version of JDK back from 1.7 to 1.6, so here is the instruction:

sudo add-apt-repository ppa:nilarimogard/webupd8
sudo apt-get update
sudo apt-get install update-java

Then type:

sudo update-java

… and you’ll see the window with versions of Java to switch to.

Voila.

P.S. To extend the list of available JDK’s download the version you need and extract it to /usr/lib/jvm/

The importance of the hashCode()

Interviewers like to ask questions about the importance of hashCode() function, kind of “what would be if the hashCode() returns the same value for different objects” and vise versa, “what could be if it returns random number every time, can it break anything”?

I think the best way to memorize the answers for such a questions is to understand how it actually works in a real life code. Simple remember the following implementation from java.util.HashMap:

public V get(Object key) {
        if (key == null)
            return getForNullKey();
        int hash = hash(key.hashCode());
        for (Entry<K,V> e = table[indexFor(hash, table.length)];
             e != null;
             e = e.next) {
            Object k;
            if (e.hash == hash && ((k = e.key) == key || key.equals(k)))
                return e.value;
        }
        return null;
    }

Even just these two lines:

if (e.hash == hash && ((k = e.key) == key || key.equals(k)))
                return e.value;

As we see, two different keys can have the same hashes. If we make the hashCode() always return the same integer it won’t break anything but it will decrease performance of searching the key. HashSet collection is based on the HashMap and works the same way when we use it’s contains(Object key); method (basically it just delegates the task to the hashMap’s getEntry(Object key); method which is using the above algorithm).

So, the answers are: if we use a custom object as a key, we need to provide our own hashCode() to have a greater performance. If hashCode() returns random integer every time, the hashMap won’t probably ever find the key you’ve put into it.

HTML parsing in Java is really simple with Jsoup

The other day I needed to write an application which parses different data from a pile of web pages. I’ve found an excellent library that parses HTML with JQuery-like selection methods. You do not need any regexes which cause a headache.

For example, you need to get a title and URL of anchor from the HTML code below:

String html = "<div><a href="http://blog.romanvlasenko.com/">Click here...</a></div>";
Document doc = Jsoup.parse(html);
String title = doc.select("div.some-class a").text();
String url = doc.select("div.some-class a").attr("href");

Voila! You have what you needed.

If you need to parse a real web page, use Jsoup.connect() method instead of the Jsoup.parse()
Jsoup has it’s own HttpConnection that encapsulates some extra-work which you were needed to do with Java’s HttpURLConnection.

Document webPage = Jsoup.connect("http://blog.romanvlasenko.com/").userAgent("Mozilla").get();

Now you have downloaded web page ready for processing. Jsoup provides both POST and GET methods. For more details see the Jsoup project homepage.