Category Archives: web

7 Reasons Why JavaScript Async/Await Is Better Than Plain Promises (Tutorial)

Async/await was introduced in NodeJS 7.6 and is currently supported in all modern browsers. I believe it has been the single greatest addition to JS since 2017. If you are not convinced, here are a bunch of reasons with examples why you should adopt it immediately and never look back.

from Pocket https://ift.tt/2LNB5jo
via IFTTT

Java 8 – Some of the new features

In this post, some of the new features of JAVA 8…

 

…and here are the results from running the file above

Note that the build fails because we pass null to the Optional and we have a condition to throw an exception if null is passed to it (this is the reason why this line is the last statement of this example).

Thank you! 🙂

 

Design Pattern: Command and State together

In this article we will explain two JAVA design patterns:
Command and State.

In the main method we:

  1. first create the light object (aka receiver);
  2. then we check if the light is on or off;
  3. after that we create a light command passing to it the light we’ve just created;
  4. then we create a Remote Control and set the command into it;
  5. in the next step we press the button and we check the light state;
  6. finally, we repeat the process and check the light state again.

So lets start with the  code:

Here we have the client class (the one that have the main method)

 

The Command Design Pattern implementation
Here we define our Command interface

 

This is the remote control class.
Note that in the pressButton method we actually execute the Command passed previously in the set method. Notice that this class has no knowledge at all about the receiver. Instead it just knows that it has a Command and how to interact with it.

 

Here we have the concrete  light command (the implementation of the Command interface). In the execute method we specify what the command really does: in this case with the help of the State Design Pattern we are simply saying to the light to go to the next state.

 

The next section is the State Design Pattern part
This is the Light class. From the Command point of view this is the receiver and from the point of view of the state design pattern this is the object that keeps the current state of the light (also known as a wrapper that will be passed to the concrete states).

 

This is the state interface. Notice that here we have two methods that have to be implemented by the classes that implement the state interface.

 

The next class represents one of the possible state of the light (the concrete state that represents the ON state of the light)

 

This is the other possible state (the concrete state that represents the OFF state of the light)

 

And the results:

 

And that’s it. Feel free to comment.

Thank you!

Decoupling event producers and event consumers in Java EE 6 using CDI and JMS

In this post I will show you how to decouple event producer from the event consumers (aka async events).

  1. you must configure your application server (here the glassfish settings):
  2. call the EJB method sendEvent() to send the event (from a servlet or from a web page):
  3. the EJB fires the event anotaded with @InForeground qualifier.
  4. An observer method handle the event and send it to the JMS Queue with the help of a message producer:
  5. Now, in another (background) thread, the onMessage method handle the message and fires a new event (which wraps the received event) now annotated with @InBackground qualifier:
  6. And finally, if you have a method that consumes events annotated with @InBackground qualifier, then you receive the event and process it accordingly to your business needs.

The event object it’s just a POJO like this one:

You can get the full code from github. Thank you!

Feel free to comment.

Date Range Validation with Annotations in JAVA

In this post I will show you how to validate date ranges with JAVA annotation.

simple date range validation
simple date range validation

XHTML page:

This is a simple JSF page with two date inputs and a submit button. When we press the button a the validation process will start via the backing bean that call a super class method.

Backing Bean:

Note that in line 3 we have the super class named: AbstractController. In line 16, in the action method, we call a validate method and we pass as a parameter the dto wich has the data itself.

Abstract Controller

In this abstract class we simple call the validate() method in the received DTO.

Data Transfer Object(DTO):

Here is the point where we annotate at the class-level with our special annotation. We simple use the annotation with 2 parameters. This parameters are optional and it’s purpose is only to personalize the error message. In case that your date fields are different from those in this example you must provide the annotation with more parameters, as you will see in the next class.

Annotation:

Here is where the magic happens. I will not discuss how annotations work, but I will cover the essential part: the constraint validation. The inner class DateIntervalValidator is responsible for the validation of the date fields with the help of reflection. We get the get methods of the two date fields and then we verify if endDate is after startDate. If that condition is false, we return false, otherwise we return true.
The message is required if you want personalize your message.
The start is required if your first date field is different from startDate. It indicates the name of the first field.
The end is required if your end date field is different from endDate. It indicates the name of the second date field.
The purpose the startDesc and endDesc is to use as a placeholder in the message parameter.

Validator:

This class is the trigger of all the validation process. When in the DTO we call the validate method, is this method we are calling, because this class is the superclass of the DTO. It calls the annotation and validates the fields. If there are any erros it will collect them in a set, here called as result. Then we iterate all over the set and add the error messages to faces context.

The next few files demonstrate the same logic applied to 2 or more groups of date validations.


multiple date range validation
multiple date range validation

XHTML:

Backing Bean:

DTO:

Plase feel free to comment or to suggest new features.
You can read more about this topic here
Download source code.
Thanks.

Check out “Tides Calculator”

https://play.google.com/store/apps/details?id=pt.joaobrito.android.tides

This is not actually a new app. This is in update to an existing app that I have forgot the signing key password. So, as I’m not able anymore to update the other app, I’ve decided to publish this new one.

Of course I apologise for all of this but it’s nothing more I can do…

Please comment or suggest new features. Thanks in advance and I hope you comprehend…

Posted from WordPress for Android