From Java to AngularJS (without pain)

At the jQuery Conf in Berlin (December 2015) and at the Rubyslava Meetup in Bratislava (17.3.2016) I was presenting my talk “From Java to AngularJS (without pain)”.

jQuery Conf

jquery-conf.png
jQuery Conference Berlin 2015

Rubyslava

rubyslava_meetup.png
Rubyslava Meetup March 2016

This is a (pretty shitty, sorry for that) video of my talk at Rubyslava – btw no, I’m still not a Ruby developer ;)

Talk Summary

The “Superheroic JavaScript MVW Framework” AngularJS is a state of the art JavaScript tool for single page web applications that offers a lot which JS alone does not – namely structure. As Java Developers we like the law and order of object oriented Java with all its tools; so I would like to show how pleased I was after discovering AngularJS for front end development (instead of JSF or Spring MVC).

My slides

Full abstract of the Talk

The “Superheroic JavaScript MVW Framework” AngularJS is a state of the art JavaScript tool for single page web applications that offers a lot which JS alone does not – namely structure. As Java Developers we like the law and order of object oriented Java with all its tools; so I would like to show how pleased I was after discovering AngularJS for front-end development (instead of JSF or Spring MVC).

In my presentation I will give a short introduction on basic principles of MVC in Java and point out some common solutions (JSP, Spring MVC…). Then I will continue with a description of AngularJS (Google yay!) and how client-side model–view–controller (MVC) can be achieved with Angular. Next I am going to show you how structure will be enforced by using extended HTML and JS vocabulary with modules, directives, and controllers (ng-what?? no JS/HTML mix). I will continue by answering the question of the implementation of the back-end (REST via jQuery) and add an overview of supporting state of the art frameworks (npm FTW!, bower, gulp, less, coffee script / jasmine, jade, bootstrap, lodash…). I am going to finish with some best practices and the downsides of Angular (no pain, no gain).

This talk is suitable for (intermediate and higher) Java (or JS) front-end developers who are eager to try out new front-end frameworks and principles. Some code examples will be presented on slides. If there is enough time left, I’ll show a live demo.

What’s next?

As speaker I’m going to have a keynote about “Simple Mobile Development With Ionic” at We Are Developers in Vienna April 13!

Upcoming Babe Tour Dates:
– 13.04. We Are Developers Conference – Ionic
– 26.04. Demo Night #4 – likeahipster.com
– 27.04. ViennaJS Meetup – Ionic
– 11.05. AngularJS Meetup – Ionic

Also I plan to visit some conferences such as the Craft conference in Budapest.

So stay tuned :)

Advertisement

Integration Test Coverage with Sonar

I love to develop test driven, but even more I love code analysis tools that show me what to improve. Sonar is quite “popular” and common for Java developers and I’m using it already for years in different projects.

If you want to see integration test coverage, you have to set the jacoco maven plugin together with the fail safe plugin in your maven config (pom.xml):

    <build>
       <plugins>
            <!-- integration tests -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-failsafe-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>integration-test</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <!-- for integration test coverage in sonar -->
            <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <id>jacoco-initialize</id>
                        <goals>
                            <goal>prepare-agent-integration</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

    <properties>
      <!-- needs to be set for jacoco's forked process -->
        <argLine>${env.MAVEN_OPTS}</argLine>
    </properties>

argLine is used as additional param in the javaagent command line section jacoco uses (needs to be set, otherwise the maven opts are not set for the forked process – OutOfMemoryException and so on…). See jacoco’s documentation for details.

The result in Sonar is pretty amazing:

sonar-jacoco

Java: break, continue, return in Loops

Although this is pretty basic Java functionality I often have to look this up, because I confound these calls, esp. break and continue. So this is my cheat sheet:

Break

for (..) {
  ..
  // leave loop immediately
  break;
  ..
}

Continue

for (..) {
  ..
  // continue loop with next iteration
  continue;
  ..
}

Return

for (..) {
  ..
  // leave calling method
  return;
  ..
}

Thread dumps, memory dump, and stack traces

1. Getting a Stack Trace

jstack -l [pid]

jstack is for java processes

2. Getting a Thread Dump

kill -3 [pid]

on unix

3. Surviving a OutOfMemoryException in Production

Sometimes you see an “OutOfMemoryException” in production and you need a memory dump of this process now to analyze the memory usage

jmap -J-d64 -dump:live,format=b,file=solr-memory-dump.bin [pid]
jmap generates a memory dump file solr-memory-dump.bin that can be used for further analysis

jhat -J-d64 -J-Xmx4096m solr-memory-dump.bin
jhat opens HTTP server on port ‘7000’

Java – CertificateException: No name matching xxx found

At paysafecard we have a lot to deal with certificates. For our test systems we use one SSL certificate for different sub-domains, e.g. one certificate for “https://test.yunacard.com&#8221; used for “https://testa.yunacard.com&#8221; and “https://testb.yunacard.com&#8221;.

But when you do this, you get a javax.net.ssl.SSLHandshakeException: java.security.cert.CertificateException: No name matching xxx found when trying to connect e.g. with new java.net.URL(url).openStream();

The work around for this problem is, to include the following in your Java class (found here):

public class ClassBla {
  static {
    javax.net.ssl.HttpsURLConnection.setDefaultHostnameVerifier(new javax.net.ssl.HostnameVerifier() {
      public boolean verify(String hostname, javax.net.ssl.SSLSession sslSession) {
        return true;
      }
   });
  }
  ..
}

There is a way to import a certificate issued to a different domain for another certain (sub-)domain with the java keytool as well.

Java: Rounding error from Double to BigDecimal

Today’s amusement in my job:

Rounding error from Double to BigDecimal, see java docs for public BigDecimal(double val).

Java creates a BigDecimal which is exactly equal to 0.1 (an unscaled value of 1, with a scale of 1), but it is actually equal to 0.1000000000000000055511151231257827021181583404541015625

 

Workaround 1: Convert Double to String before converting to BigDecimal, as suggested in the java docs for BigDecimal

BigDecimal b = new BigDecimal(Double.toString(double));

 

Workaround 2: Round!
Double d = ...; // your (rounded) value
BigDecimal b = new BigDecimal(d).setScale(2, BigDecimal.ROUND_HALF_UP);

.

Another problem with Double in Java: http://www.exploringbinary.com/java-hangs-when-converting-2-2250738585072012e-308/

Multiple writable mappings in Java Persistence API

Seit kurzem setzte ich mich mit der Java Persistence API auseinander, das mit Hibernate und dem von uns eingesetzten Oracle Toplink implementiert wurde.

Folgende Fehlermeldung beschäftigte mich bei einer OneToMany-Relation

Multiple writable mappings exist for the field [tabltenname.spaltenname]. Only one may be defined as writable, all others must be specified read-only.

Die Lösung war das explizite deklarieren der Spalte als “read only”, was man mit den Optionen nullable=false, insertable=false und updatable=false bei der @Column-Annotation erreicht.

Spannend, nicht? ;)

java-coffee-cup-300x219

convert date to calendar in java

heute wieder mal googlen müssen, weils mir wieder mal entfallen ist, deswegen gleich ein beitrag für die howto section.

in java sind die alten, sehr hilfreichen funktionen von java.util.Date, die teile des datums zurückgeben, wie date.getMonth(), leider depricated, und zwar schon seit v. 1.1. stattdessen wird heute java.util.Calendar verwendet. …und das geht so ;)

Date date = .. // das datum, das man aufsplitten will;
Calendar cal = Calendar.getInstance();
cal.setTime(date);
// monat fängt mit 0 an!
int month = cal.get(Calendar.MONTH) + 1;
int day = cal.get(Calendar.DAY_OF_MONTH);
int year = cal.get(Calendar.YEAR);

beim suchen hab ich eine hilfreiche seite gefunden mit vielen tutorials und beispielen gefunden.

.

wer ein formatiertes datum braucht, nimmt am besten:


Date date = new Date();
java.text.SimpleDateFormat df = new java.text.SimpleDateFormat( "yyyyMMdd" );

torque throws ORA-01424 – purge the recycle bin

schon wieder passiert und wieder mal länger nach der lösung gesucht, bis es mir wieder (spät, aber doch) eingefallen ist:

wird eine ORA-01424 oracle fehlermeldung beim generieren der java-klassen aus der DB über die torque geworfen, hat man wohl wieder das magische wort purge beim löschen der tabellen vergessen.

drop table drop_me purge
/

der fehler tritt auf, da die tabellen nicht vollständig gelöscht wurden und somit im recycle bin der DB liegen. purge bereinigt den papierkorb, bzw wird beim drop einer tabelle diese gleich vollständig gelöscht.

der fehler ORA-01424 deutet eigentlich auf ein fehlendes zeichen nach dem escape character hin und führt in diesem zusammenhang leider nur in eine falsche richtung.