Author: Michael Müller
Using WebSockets with Java EE, part 2.3
- Technical aspects of the WebSocket protocol
- WebSockets in a Java EE 7 application
- JSF 2.3 and WebSocket
As mentioned before, I divided part two into a couple of sub parts. Today I’ll finish with WebSockets in a Java EE 7.
If we want the user to choose a different chat room which is operated on the same endpoint, or if we want to show the user’s name, we need a way to pass this information to our endpoint.
One solution would be to provide information about the room by the endpoint URI. Let’s assume, the room is identified by a name, then the code might look like the following excerpt.
@ServerEndpoint("/simplechat/{room}")
public class SimpleChat {
@OnOpen
public void onOpen(Session peer) {
String class = peer.getRequestParameterMap().get("room").get(0);
[...]
}
[...]
}
This is quite the style Java EE supports by JAX-RS (Java API for RESTful Web Services): parts of the URI are variable and will be accessible by the request parameter map as shown in the onOpen method. Really easy, isn’t it?
The WebSocket connection is a channel between the client and the server. As an upgrade from HTTP the first request is initiated by the client. What happens, if the user fakes the URI and chooses a different classroom? And, we want to include the user within each message. Do you like somebody to spoof the user? Regardless the approach shown above is really simple, it might not fit our requirements in matters of security. And why should the client provide information, the server already knows?
With these thoughts in mind, we need to prefer a different approach. User and classroom are well known at server side, and thus they will be injected into the endpoint.
Let’s assume, we hold this information within a class called UserController:
public class UserController implements Serializable {
private Account _account;
private FinalYear _finalYear;
[Getter/Setter and other fields omitted for brevity]
}
It seems to be easy to inject this user controller into our endpoint:
@Inject UserController _userController;
We simply replace
sendObject(message);
by
String namedMsg = _userController.GetAccount().getDisplayName()
+ ": " + message;sendObject(namedMsg);
The first message might be send correctly, but with the next message a null pointer exception occurs. Why?
The reason is quite simple: The endpoint will be created within that request when the user enters the classroom. The injected user controller contains all the values we expect. CDI does not really inject an instance of the class UserController, but a proxy pointing to the active user controller object of the current request. The WebSocket channels is opened by an upgrade from HTTP. The request terminates normally whilst the channel remains open. Now, if the user enters a message, it is send via the WebSocket channel, not by a new HTTP request. Since there is no HTTP request at that time, the CDI proxy cannot refer to any bean, albeit the user controller is of session scope. Thus the proxy resolves to `null`.
To tackle this challenge we use a constructor injection and store the injected object into an object attribute (field). By this, we “conserve” the original session controller.
Now we are able to use it as expected.
@ServerEndpoint("/simplechat")
public class SimpleChat {
UserController _userController;
public SimpleChat(UserController userController){
_userController = userController; }
[...]}
Today’s article is a simplified version of the relevant chapter of my book. Next will be JSF 2.3 and WebSocket.
Stay tuned!
Oshiro Makro Objektiv
Die Photos auf meinem Blog zeigen, dass ich gerne Nahaufnahmen mache. Bisher nutzte ich dafür das Olympus 14-42 mm Objektiv mit Makrokonverter an einer OM-D 10 Mark II . Auf der Suche nach einem “richtigen” Makroobjektiv habe ich einerseits mit dem Olympus 60mm geliebäugelt. Andererseits kommt es bei so kurzen Distanzen auf die richtige Fokusierung an. Und gerade bei Insekten, die mal schnell ihren Platz wechseln, liegt der Autofokus gerne schon einmal einige Millimeter daneben. So wird vielleicht das Blatt, auf dem ein Insekt sitzt scharf, das Tierchen selbst ist bei der geringen Schärfentiefe jedoch kaum noch zu erkennen. Also lautet die Devise oftmals: Manuell fokussieren. Und so bin ich auf das Oshiro 60mm f/2.8 mit einem Abbildungsmaßstab von bis zu 2:1 gestoßen. Rein mechanisch, dafür deutlich günstiger (ca. 135 Euro).
Das Objektiv wurde als Objektiv für Micro Four Thirds beworben. Erst bei genauer Betrachtung stellte sich heraus, dass dies eigentlich nur für das Canon-Bajonett verfügbar ist und es sich um ein Bundle mit entsprechendem Adapter handelte. Während die Olympus-Objektive fest auf dem Gehäuse sitzen, kann der Adapter auf dem Gehäuse geringfügig wackeln, ebenso wie das Objektive auf dem Adapter. Ob dies ausschließlich am Adapter, oder aber auch am Objektiv liegt, kann mangels alternativem Adapter nicht beurteilt werden. In der Praxis jedenfalls hat sich diese leichte mechanische Instabilität nicht negativ bemerkbar gemacht.
Für die Fokussierung steht weniger als eine halbe Umdrehung am Fokusring zur Verfügung. Es erfordert schon ein wenig Übung, um hier ähnlich fein fokussieren zu können, wie der motorgetriebene manuelle Fokus bei den elektrischen Objektiven. Auch die Blende wird rein mechanisch eingestellt. Mangels elektrischer Verbindung gibt es so keinerlei Information zur gewählten Blende, was der Belichtungsmessung der Kamera keinen Abbruch tut, aber halt später auf dem Foto nicht als Info zur Verfügung steht. Auch erkennt die Kamera nicht, wann der Fokusring betätig wird und kann so nicht das automatische Fokus-Peaking einschalten. Dies kann jedoch manuell zugeschaltet werden.
Dieser Nachteile sollte man sich bewusst sein, wenn die Entscheidung für dieses Objektiv fällt. Ich habe diese ob des günstigen Preises in Kauf genommen.
Der minimale Fokusabstand beträgt 18,5 cm. Da das Objektiv bereits rund 13 cm lang ist und etwas Abstand zum Sensor hat, heißt dies, man kann mit der Frontlinse bis auf rund 5cm an das zu fotografierende Objekt heran. Un damit sind dann wahrhaft riesige Aufnahmen möglich. Die Beispielbilder wurden mit der größtmöglichen Blende von 2,8 erstellt. Damit sind dann extrem kurze Verschlußzeiten möglich, um auch den Flügelschlag von Insekten “einzufrieren”. Nachteilig ist die geringe Tiefenschärfe. Das Objektiv kann jedoch bis auf Blende 22 abgeblendet werden, so dass eine deutlich größere Tiefenschärfe erzielt werden kann.
Die Beispielfotos wurden beschnitten und verlustbehaftet komprimiert, damit sie unterhalb der Maximalgröße für diesen Blog bleiben. Im Original ist es also noch besser.




Dank der für diese Preisklasse großen Blende von 2,8 sind mit diesem Objektiv auch Aufnahmen bei ungünstigen Lichtverhältnissen möglich. Insbesondere im Wald, bei den Ameisen, macht sich dies positiv bemerkbar. Fotografieren mit Bokeh ist ebenfalls recht gut möglich. Dank der 14 Blendenelemente ergeben sich fast runde Unschärfekreise.

Erkennbar ist dies beim zweiten Ameisenbild im Vordergrund. Richtig Spaß macht das Fotografieren mit Bokeh bei Portrait-Aufnahmen. Das Objektiv kann nicht nur auf 18,5 cm, sondern auch auf Unendlich fokussieren, so dass Portraits oder ganz allgemein Personen-Aufnahmen gut möglich sind. Lediglich für Schnappschüsse ist es mangels Autofokus weniger gut geeignet.
Impressions from Tenerife VI
Impressions from Tenerife V
Hunting session is open
Do you like hunting for bugs? Or confirming that NetBeans can be used without serious bugs? Then participate the Apache NetBeans (Incubating) IDE Community Acceptance Testing (NetCAT) program for NetBeans 9.0. Become one of the first who work with the upcoming version of this great Open Source IDE.
Read the NetCAT FAQ [1] and join the community!
[1] cwiki.apache.org/confluence/display/NETBEANS/NetCAT+FAQ
Impressions from Tenerife IV
Impressions from Tenerife III
Impressions from Tenerife II
I took a snapshot down into a Aloa Vera or similar plant. The camera automatically focused onto the ground and the picture shows very clear a leaf at the bottom of this plant.
Then I manually focused on that little animal I discovered there. Two photos taken from the same position, showing very different details. Just by changing the focus point.
Isn’t that similar to software development? Sometimes we need to go back one step and to observe our product with a different focus. This often brings up valuable details which help us to improve our software. Always keep in mind to take different point of views. You are not only the NetBeans geek, who develops the software. Try to wear the cap of the user, of a decision maker, and other care holders.
For me, this often is very helpful. And I assume, for you, too.
Enjoy my personal impressions and keep developing good software!
JavaLand 2017
The fourth edition of JavaLand had been on March 28th + 29th 2017. JavaLand is one of the biggest Java Congresses in Europe and is hosted in my home town. Yesterday I wrote my reports for an online channel as well as for a German printed magazine. Here I’ll blog about it from three personal perspectives.
I’m a NetBeans Dream Team Member, a member of the JSF expert group and an author. These are the three perspectives. In the last role, I write for a couple of different publishers, but here I’m reporting from the role of being a Heise author.
NetBeans Dream Team
A couple of NetBeans Dream Team members were involved in the current issue of JavaLand. First of all, I need to mention Markus Eisele, who had been responsible for the great conference program.
A couple of NetBeans Dream Team members talked at JavaLand. Geertjan Wielenga’s subject was “From AngularJS to Oracle JET”. As usual, he showed the famous interaction from NetBeans with the browser. Ivar Grimstad demonstrated the current state of MVC. This framework formerly had been part of Java EE 8, but was kicked out by Oracle. Currently, this project is in the final state of transition to Ivar. Ivar mentioned NetBeans being the best IDE to develop MVC. Anton Epple hold two talks, one called “hack your toothbrush” affecting the Internet of things and the second about Java on the client. Sadly Arun Gupta’s talk about Docker container orchestration was canceled due to personal reasons. And last but not least myself, talking about the JShell and its integration into NetBeans.
Michael Simons represented his user group at the JUG café (with the red/black shirt). And I’m sure, some more NetBeans Dream Team members attended this conference.
JSF Expert Group
Usually a couple of JSF expert group members meet in the early adapters area of JavaLand. This time we celebrated the JSF 2.3 release. JSF 2.3 final is shipped to JCP and Maven Central. The picture shows the cake cutting ceremony at JavaLand.
All of us were involved in the current editon of JavaLand. I mentioned my talk above. Get the slides in my previous article.
Ed Burns, one of the two JSF spec leads gave a class on the workshop day, which immediately followed the main conference. And Bernd Müller had been member of the program committee.
Heise Author
A couple of authors writing for Heise and its subsidiaries hold their talks at JavaLand or where involved in some other way. I suggested to arrange a short meeting to meet each other in person. Alexander Neumann of Heise arranged a round table and more than 10 authors attended. Sadly there have been two different cafés and such another handful of authors met each other at the other café.
One of the authors, Thorben Janssen proudly presented the first printed copy of his new book “Hibernate Tips”. I just started to read this book. Once I finished, I’ll write a review.
Of course, there is a fourth perspective: From the attendee’s point of view. That’s what I wrote about for the other channels. In a few words: I learned interesting new stuff and met a couple of nice people. And coming to the perspective of being a JUG Cologne member: Some of these people promised me to talk at out local JUG.
I’m looking forward to the next edition of JavaLand. See you there on March 20th/21th 2018.







