Adam Bien's Weblog

Think WARs, High Productivity And 10k Transactions Per Second On Stock Java EE 7

Pavel, please introduce yourself

My name is Pavel Pscheidl and my role can be best recognized by “full stack developer”. I live and work as a Java EE developer. Recently, I’ve started Ph.D. studies at Faculty of Informatics, University of Hradec Kralove. Personally, I’m interested in automation systems used for decision support in daily life. Home automation and healthcare systems are my current focus. I’m also a passionate user of Mozilla’s Rust programming language.

What are you building with Java EE?

Commercially, my last allocation was at Zentity, where we built mobile banking backend systems, where all the business logic lies. Zentity’s main customers are big european banks and telecommunication providers. Our backend systems must integrate into existing bank environment, provide the necessary security layer, service orchestration, caching or load balancing. I used Java EE for these projects of course.

However, there is also plenty of space for Java EE at the University. First, Java EE is taught there very well and many things are explained to students - from basic understanding of application servers and the principle behind servlets to advanced stuff. It has it’s space in research projects as well. Home automation systems and Internet of Things projects are current projects with Java EE involved. We also have internal systems based on Java EE. Lately, we’re building lots of microservices with “very fat” business logic for home automation. JAX-RS is clear choice here.

There are also lots of other projects, both commercial or internal projects at the university. It is fair to say that I use Java EE 7 for a long time. We’ve flawlessly migrated older projects to Java EE 7 containers long time ago.

Can you share with us some geeky numbers like e.g. TX per seconds, heap sizes etc -- whatever Java EE devs might find interesting.

I’ve spent quite some time measuring the performance of REST APIs on my projects. The banking systems we built were tested to handle 10,000 requests per second. Benchmarked with both Resteasy and Jersey as JAX-RS implementation. It could actually be achieved with WildFly and it’s load balancing capabilities pretty easily and cheaply - I found that fact interesting. It depends how the request looks like of course. I can not discuss details publicly, but I can tell that one request could allocate up to 5 additional threads from the thread pool managed by the application server as it waited for other services and the whole request could last for up to a second. Typically between 150-200 milliseconds. Java itself and of course Java EE always proved to be very fast and easily scalable here. Under usual load, the heap usage is always under 1 GB per node and I’ve never seen it exceeding 2 GB on common projects. I can’t actually offer big, sensational numbers. And that is what I enjoy probably the most. Having a virtual machine with 8 threads and 2 GB of memory spawned in no time and letting it handle thousands of clients for several years without problems is what impresses me. Just yesterday, I was forced to restart WildFly 8 after a year and seven months of uptime due to CentOS upgrade.

Are you happy with Java EE so far? Is Java EE productive?

Very happy. Ease of development is the main reason. Everything is kind-of prepared and standard ways of doing things make sense, for example data sources and JNDI. Customers are happy with application servers, where everything can be set up easily via web interface in detail. There is no configuration hell and JAR hell. Everything required is already present at the application server. I commonly use few additional CDI extensions in every project that further improve simplicity. There are many, but I’d like to point out Apache Deltaspike. We even developed set of custom CDI extensions that are very specific for given domain - they do non-trivial work for developers and newcomers are not even aware of them. Overall, I like how Java EE reduces application’s complexity to bare minimum. Projects in Java EE 7 are highly domain oriented - this means there is minimum amount of configuration and new developers orient easily. In enterprise environment, this is actually a big deal.

Which tools or IDEs are you using?

Any Linux-like OS is what I strongly prefer - a proper terminal and a set of basic tools is a must (ab, curl, scp, ssh…). I use IntelliJ Idea for some Java EE projects based on Gradle. However, NetBeans is my strong preference. So for the majority of time, I use NetBeans + Maven rather than IntelliJ. When NetBeans comes with decent support for Java EE projects built with Gradle, I will probably stop using IntelliJ completely. Besides Java IDE, I always have Sublime Text 3 and Postman opened. I should also mention Enunciate - a tool to generate JAX-RS REST API documentation. That is pretty much everything I commonly need.

You migrated to Java EE 7 and WildFly. Are you happy with the decision? What was the outcome?

Yes, I am extremely happy with the decision. I migrated to pure Java EE 7 projects mainly because I started to seek an alternative. I developed projects using many technologies and frameworks. Some projects were in Ruby and I was even using Python + Django for websites. Mainly, I was using Spring. I was taught to use it by everyone “since Kindergarten”. I still do not get it - heavy marketing, fancy words, but the reality is different and ugly with many custom fixes and lots of custom code not oriented on the problem domain. I am awared that this is a controversial topic, but after the years I just feel this way. I soon dropped all of the previously mentioned and focused only on Java EE 6, later Java EE 7. And life became easier - which is exactly what I wanted. So the overall outcome of switching to pure Java EE 7 is easy living :) There are many open standards which reflect real world requirements in Java EE. Such set of open standards is then implemented and usually undergoes a process of testing, improving and bug fixing - bug free software is a myth. An application server represents a well tried, periodically fixed set of such libraries used by many. And when it is used and developed by many, it is usually more trouble free then solutions wired together individually. Wiring everything together on our own always ended up in troubles. The problem repeated across companies and developers. And I don't want that back. Projects are now tidy and domain oriented, no configuration hell, no dependency hell. Java EE has CDI, which I personally call “Java EE-core”. Majority of things in Java EE relies on Dependency Injection and design patterns built around/on top of it. And CDI is extremely well designed and feature-complete. I think it is the top reason why working with Java EE is so easy and understandable. With extensions, it is a pleasure to work with. Even Spring Data is now available as a CDI extension. But I’ve tried it and happily returned to Deltaspike Data. A real benefit is that projects are also actually transferable from one AS to another with little effort. The certification process helps here. In real world, the choice of application server proved to be of great importance. It doesn’t matter that much for the application itself, but the environment the application is deployed into matters. Servers often come with some additional functionality. For example, WildFly has great load balancing capabilities. I’ve used that several times and it was capable of literally saving client’s ass - for free. I also started to use Payara for both production and development not so long ago. Right now, I am exploring newest WebLogic, just to discover new possibilities. So I’m not a RedHat-centric person, but I found Wildfly/JBoss to be well documented and easy to understand for both developers and administrators. What I don’t like is the presence of Hibernate. EclipseLink is my strong preference. Hibernate sometimes requires special treatment when used as JPA implementation (e.g. LazyInitializationException). I have definitely faced some bugs and troubles, especially in the beginning. But everything was resolved quite fast and it was AN EXCEPTION, NOT A RULE - and this is what matters to me.

How big is your WAR?

Smallest ones are hundreds of kilobytes. Projects under 1 Megabyte are usually REST-based services connected to some kind of relational database. More complex applications are about 1-4 Megabytes with all libraries included. When the application is a website, the WAR gets bigger - all the graphics and styles take some place. My recent web projects were slightly above 5 MB. I took a quick look to my repositories and the largest project I could found is slightly over 7 MB. And that includes Primefaces themes, which we’ve also used on some projects.

Can you briefly describe the architecture of your application?

To my surprise, I do service-oriented architecture more than ever. In these cases, the complete system usually consists of < 5 WARs connected with REST interface (JAX-RS). There is no service hell, usually there is only the main application carrying all the bussiness logic and other applications oriented onto a different domain, for example security. Horizontal scaling can than be done independently. I am heavily influenced by domain-oriented design, so the components themselves are organized and named according to the problem domain. Projects created with domain-oriented design in mind proved to be much more easier to understand to other developers. For web-based applications, one WAR with JavaServer Faces is the golden standard. For projects based on AngularJS and simillar frameworks, a REST API is created (again using JAX-RS).

How important are standards for you? Does your application depend on application server specific APIs?

Standards are very important. But not everything can (and probably should ?) be standardized, I think... It is probably better to use new things in a form of external library or let’s say a CDI extension. If possible. As I stated earlier, standards represent a collective effor. And Java EE has very capable and enthusiastic people in it’s community. My applications do not use any application server specific APIs. Sometimes, they use specific APIs for databases, where I completely drop JPA and use for example Morphia for MongoDB, integrated via a simple producer.

Which Java EE APIs are you using in your products?

Many. JAX-RS, CDI, JPA, EJB, JSF and JAX-WS are the most commonly used ones. Also managed thread pool is accessed via Java EE API. Rarely, JMS is used.

Take a look at the Java EE 8 APIs. Which of the APIs are most interesting / important to you?

I am definitely most interested in JSR 372: JSF 2.3, JSR 370: JAX-RS 2.1 and JSR 375: Java EE Security API. Java EE has some security modules in a form of CDI extension and securing a JSF-based web projects is a piece of cake, but I believe the time has come for a standardized solution.

Can you share any resources (blogs, etc feel free to promote yourself) with us?

The best resource is currently my personal blog / website. There are few other resources as well: Faculty of Informatics, UHK, or our Home Automation System. I also have a personal GIT repository. More projects there are actually not a big secret, but hidden. My GitHub will be growing soon I hope, because we’ve decided to license all our university projects under GNU GPL and make them publicly available.


NEW workshop: Building HTML 5 Applications With Angular 2
Full week in December 2016: from Java EE 7 Bootstrap, Effective, Architectures over Testing to Microservices
On demand workshops: Java EE 7: Bootstrap, Effective, Testing and Microservices available for streaming.

Newsletter: airhacksnews.com

A book about rethinking Java EE Patterns

Lightning Fast Java EE 8 and 9 Releases, Running JMS Servers, Configuration Managment or Questions for the 30th airhacks.tv Q&A

Questions for the 30th airhacks.tv, today (August, Monday, 29th) at 6pm CET (checkout recent episodes):

  1. Lightning Fast Java EE 8 and 9 Releases
  2. Docklands on hub.docker.com
  3. How to run JMS Servers
  4. MDB and AMQP
  5. Managing environment specific configuration without a DB
  6. How to stress test WebSockets?
  7. Nashorn and Docklands
  8. Using DTOs as data encapsulation pattern
  9. Personal opinion about Angular 2, React and Co.
  10. Manipulating data in JPA Entity accessors
  11. Storing data in a secure cookies
  12. Generating large PDF documents
  13. A Java Project of the month

The questions above are going to be answered during the next "Airhacks Q & A" http://airhacks.tv live. You can subscribe to the channel and / or for the next event: http://www.ustream.tv/channel/adambien (both is optional, no registration required to watch this event live).

If you still miss a question, ask at https://gist.github.com/AdamBien/37a17360d3cdc278c1a98847dc1c5359, write a comment on this post, tweet your question with the hashtag "#airhacks" or mention me https://twitter.com/AdamBien.

See you at Java EE Workshops at Munich Airport, Terminal 2 or Virtual Dedicated Workshops / consulting. Is Munich's airport too far? Learn from home: airhacks.io.


NEW workshop: Building HTML 5 Applications With Angular 2
Full week in December 2016: from Java EE 7 Bootstrap, Effective, Architectures over Testing to Microservices
On demand workshops: Java EE 7: Bootstrap, Effective, Testing and Microservices available for streaming.

Newsletter: airhacksnews.com

A book about rethinking Java EE Patterns

HAProxy, TomEE 7.01 Plus, Elasticsearch -- New Docklands Images Arrived

HAProxy, TomEE 7.0.1 Plus and Elasticsearch Docker images were added to github.com/AdamBien/docklands. Docklands already comes with minimalistic images for Postgress, DerbyDB,Tomcat, Payara Server (aka commercially supported GlassFish), WildFly, Weblogic and WebSphere Liberty Profile.

Docklands layering (image inheritance) aims for build speed, low memory overhead and productivity.

I'm using docklands as base for my consulting work and many microservices projects.

See you at Java EE Workshops at Munich Airport, Terminal 2 and particularly at Java EE 7 Microservices. Is MUC too far? Checkout http://javaeemicro.services


NEW workshop: Building HTML 5 Applications With Angular 2
Full week in December 2016: from Java EE 7 Bootstrap, Effective, Architectures over Testing to Microservices
On demand workshops: Java EE 7: Bootstrap, Effective, Testing and Microservices available for streaming.

Newsletter: airhacksnews.com

A book about rethinking Java EE Patterns

Angular 2: Styling The Generated Host Tag

Angular 2 replaces / generates a "synthetic" tag for each component without any styling with the name specified in the selector

The component below:


import { Component } from '@angular/core';
@Component({
  selector: 'hello-app',
  template: '<h1>Styling the host component</h1>',
})
export class AppComponent { }

generates the following markup:

<hello-app>
 <h1>Styling the host component</h1>
</hello-app>

Additional styling of the "host" tag can be added with the host object:


import { Component } from '@angular/core';
@Component({
  selector: 'hello-app',
  template: '<h1>Styling the host component</h1>',
  host: {"class":"nice"}
})
export class AppComponent { }

which generates the following output:

<hello-app class="nice">
	<h1>Styling the host component</h1>
</hello-app>

Putting the selector into angle brackets: selector: '[hello-app]' searches for an attribute, not a tag, with the name of the selector, e.g.:


<div hello-app></div>

See you at Java EE Workshops at Munich Airport, Terminal 2 and particularly at Building Angular 2 Applications and Building React Applications.


NEW workshop: Building HTML 5 Applications With Angular 2
Full week in December 2016: from Java EE 7 Bootstrap, Effective, Architectures over Testing to Microservices
On demand workshops: Java EE 7: Bootstrap, Effective, Testing and Microservices available for streaming.

Newsletter: airhacksnews.com

A book about rethinking Java EE Patterns

Oracle on Java EE 8 News, Monitoring, Sonar Setup, BCE Entities or 29th airhacks.tv Questions and Answers

90 live attendees from all over the world (according to chat: France, Nigeria, Costa Rica, Netherlands (...)) participated in the 29th edition of airhacks.tv starting with the "Oracle and Java EE 8" discussion including press releases, then I answered questions ranging from Akka interoperability, over Java EE monitoring, to file access from EJBs and Sonar Setup:

Any questions left? Ask now and see your topic discussed at the first Monday of the month, 6 pm CET.

See you at Java EE Workshops at Munich Airport, Terminal 2 or Virtual Dedicated Workshops / consulting. Is Munich's airport too far? Learn from home: airhacks.io.


NEW workshop: Building HTML 5 Applications With Angular 2
Full week in December 2016: from Java EE 7 Bootstrap, Effective, Architectures over Testing to Microservices
On demand workshops: Java EE 7: Bootstrap, Effective, Testing and Microservices available for streaming.

Newsletter: airhacksnews.com

A book about rethinking Java EE Patterns

How to merge javax.json.JsonObject instances

Combining multiple javax.json.JsonObject instances requires copying their contents into a single JsonObjectBuilder instance:


@Test
public void merge() {
	JsonObject dev = Json.createObjectBuilder().
			add("developer", "duke").
			build();
	JsonObject lang = Json.createObjectBuilder().
			add("language", "java").
			build();

	JsonObjectBuilder result = Json.createObjectBuilder();
	dev.entrySet().forEach(s -> result.add(s.getKey(), s.getValue()));
	lang.entrySet().forEach(s -> result.add(s.getKey(), s.getValue()));
	JsonObject merged = result.build();
	assertThat(merged.getString("developer"), is("duke"));
	assertThat(merged.getString("language"), is("java"));
}

Adding additional attribute to a JsonObject instance works similarly.

See you at Java EE Workshops at Munich Airport, Terminal 2, particularly at: Effective Java EE 7! Is MUC too far? Checkout effectivejavaee.com


NEW workshop: Building HTML 5 Applications With Angular 2
Full week in December 2016: from Java EE 7 Bootstrap, Effective, Architectures over Testing to Microservices
On demand workshops: Java EE 7: Bootstrap, Effective, Testing and Microservices available for streaming.

Newsletter: airhacksnews.com

A book about rethinking Java EE Patterns

How to add an attribute to javax.json.JsonObject

Although javax.json.JsonObject implements Map<String, JsonValue> -- it is immutable. Any modification attempt results in UnsupportedOperationException:


    @Test(expected = UnsupportedOperationException.class)
    public void immutable() {
        JsonObject dev = Json.createObjectBuilder().build();
        dev.put("dev", JsonValue.NULL);
    }

To add a new attribute to an existing JsonObject instance, you will have to copy it's attributes into a JsonObjectBuilder instance, add any attributes and eventually build a new instance:

package com.airhacks.jsonp;

import javax.json.Json;
import javax.json.JsonObject;
import javax.json.JsonObjectBuilder;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import org.junit.Test;

public class JsonTest {

    static final String STATUS_KEY = "status";

	
    @Test
    public void addAttributeToObject() {
        JsonObject dev = Json.createObjectBuilder().
                add("developer", "duke").
                build();
        String expected = "master";

        JsonObject devWithStatus = enrich(dev, STATUS_KEY, expected);
        assertThat(devWithStatus.getString(STATUS_KEY), is(expected));
        System.out.println(devWithStatus);
    }
	

    public JsonObject enrich(JsonObject source, String key, String value) {
        JsonObjectBuilder builder = Json.createObjectBuilder();
        builder.add(key, value);
        source.entrySet().
                forEach(e -> builder.add(e.getKey(), e.getValue()));
        return builder.build();
    }

}

See you at Java EE Workshops at Munich Airport, Terminal 2, particularly at: Effective Java EE 7! Is MUC too far? Checkout effectivejavaee.com


NEW workshop: Building HTML 5 Applications With Angular 2
Full week in December 2016: from Java EE 7 Bootstrap, Effective, Architectures over Testing to Microservices
On demand workshops: Java EE 7: Bootstrap, Effective, Testing and Microservices available for streaming.

Newsletter: airhacksnews.com

A book about rethinking Java EE Patterns

Using Excel As Source For Unit Tests

Sophisticated business logic requires a high amount of high quality data for test purposes. Tabular data is easier maintainable in Excel then directly in Java code.

With Java 8 it is easy to provide a function which converts an Excel Row (actually a HSSF Row) into whatever POJO you like:


 Function pojoMapper() {
        return (row) -> {
            Iterator cells = row.cellIterator();
            return new Input(
                    asLong(cells.next()),
                    asLong(cells.next()),
                    asLong(cells.next()));
        };
    }


A few additional lines of code convert a Stream<Row> into a Stream<POJO>:


public static <T> Stream<T> load(Function<Row, T> mapper,...) {
        int skipCount = 0;
        if (hasHeader) {
            skipCount = 1;
        }
        String fileName = //...
        try (InputStream inp = new BufferedInputStream(new FileInputStream(fileName));) {
            try (XSSFWorkbook wb = new XSSFWorkbook(inp)) {
                XSSFSheet sheet = wb.getSheetAt(tab);
                Stream<Row> stream = StreamSupport.stream(sheet.spliterator(), false);
                return stream.skip(skipCount).map(mapper);

            }
        } catch (IOException ex) {
            throw new IllegalStateException("Problems processing file: " + fileName, ex);
        }
    }


Parameterized JUnit tests can be used to define the locations of excel files and load the sheets representing different use cases, test suites or regression tests.

Hence the code above is reusable, andit was extracted and released as: https://github.com/AdamBien/sheetfit/ (the pronunciation is semi-accidental :-)). This one-class utility is also available directly from maven central:

<dependency>
	<groupId>com.airhacks</groupId>
	<artifactId>sheetfit</artifactId>
	<version>0.0.1</version>
</dependency>


Example is implemented as system test: https://github.com/AdamBien/sheetfit/tree/master/sheetfit-st.

Also checkout javaeetesting.com or come to MUC: http://workshops.adam-bien.com/about-testing.htm


NEW workshop: Building HTML 5 Applications With Angular 2
Full week in December 2016: from Java EE 7 Bootstrap, Effective, Architectures over Testing to Microservices
On demand workshops: Java EE 7: Bootstrap, Effective, Testing and Microservices available for streaming.

Newsletter: airhacksnews.com

A book about rethinking Java EE Patterns

Sending An OPTIONS Request With Angular 2

The Angular 2 Http class does not offer an OPTIONS convenience method out-of-the box. Interestingly, all the major HTTP methods are supported: GET, POST, PUT, delete, patch and head, but the OPTIONS was not implemented. To perform an e.g. POST request, you can use the built-in method:


import { Http } from '@angular/http';
import 'rxjs/add/operator/toPromise';

export default class XYZService{
   
    constructor(private http:Http){}

    performPost(uri,payload):Promise<Workshop[]>{
        return this.http.post(uri,payload).
        toPromise().
        then(r => r.json()).
        catch(this.handleError);
    }

   handleError(error){
        return Promise.reject(error.messages || error);
    }
//(...)

For the initiation of an OPTIONS request you will have to rely on the generic request method of the same Http class:

    options(uri):Promise{
        return this.http.request(uri,{method:'OPTIONS'}).
        toPromise().
        then(r => r.json()).
        catch(this.handleError);
    }

See you at Java EE Workshops at Munich Airport, Terminal 2 and particularly at Building Angular 2 Applications or Building React Applications.


NEW workshop: Building HTML 5 Applications With Angular 2
Full week in December 2016: from Java EE 7 Bootstrap, Effective, Architectures over Testing to Microservices
On demand workshops: Java EE 7: Bootstrap, Effective, Testing and Microservices available for streaming.

Newsletter: airhacksnews.com

A book about rethinking Java EE Patterns

Java EE 8 Plans, Oracles Statement, Performance Smoke, Software Quality Rules, Cross-Jars Alternatives or 29th airhacks.tv Questions and Answers

Questions for the 29th a airhacks.tv, Today (August, 1st) at 6pm CET (see archives):

  1. Java EE 8 news: semi-official Oracle plans for Java EE 8, JavaOne (discussion)
  2. Serving files without a WAR
  3. Java EE performance monitoring and results interpretation
  4. Software quality with Sonar and Java EE: rules, defaults and real world
  5. Accessing EJBs from EntityListener's
  6. Running "Server Smoke" on 141 Processors and 10 TB
  7. Cross-Jar @Alternative's
  8. Referencing Entity Manager from JPA Entities
  9. JAX-RS ExceptionMappers in internet-facing API
  10. EntityManager, serialization and clustering
  11. BCE: referencing JPA entities from other components

The questions above are going to be answered during the next "Airhacks Q & A" http://airhacks.tv live. You can subscribe to the channel and / or for the next event: http://www.ustream.tv/channel/adambien (both is optional, no registration required to watch this event live).

If you still miss a question, ask at https://gist.github.com/AdamBien/48148f11f649dd756993a876f7354061, write a comment on this post, tweet your question with the hashtag "#airhacks" or mention me https://twitter.com/AdamBien.

See you at Java EE Workshops at Munich Airport, Terminal 2 or Virtual Dedicated Workshops / consulting. Is Munich's airport too far? Learn from home: airhacks.io.


NEW workshop: Building HTML 5 Applications With Angular 2
Full week in December 2016: from Java EE 7 Bootstrap, Effective, Architectures over Testing to Microservices
On demand workshops: Java EE 7: Bootstrap, Effective, Testing and Microservices available for streaming.

Newsletter: airhacksnews.com

A book about rethinking Java EE Patterns

realworldpatterns.com
Online Workshops
...the last 150 posts
...the last 10 comments
License