Category Archives: Development

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.

Simple CDI Events Example

In this post I will show you how to produce a CDI event and how to handle it even if it is deployed in another application.

The Event class

A simple JSF Page

This page only shows a simple message from the backing bean (the call to the bean produces the bean’s initialization).

Backing Bean

Here we Inject an event (LoggerEvent) via CDI Inject annotation. Then inside the init method annotated with PostConstruct we fire the event with a new instance of the event. This is the simplest way to fire a CDI event. No matter who are listening to this event will be notified.

A simple Logger Event Handler

This class will listen for events annotated with the keyword @Observes. Because of this, it will execute the method. In this case it will print to the console a log message passed to the constructor of the event instance when it was created: Hello from CDI events as you can see in the next picture.

Result (console result)

cdi events
cdi events

AS you can see, it’s just that simple to create a CDi event and listen to it. This technique uses the Observer design pattern here with the help of @Observes annotation.

Feel free to comment. Download the souce code CDIEvents.

Thanks.

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.

Static Binding vs Dynamic Binding in JAVA

World Cup Brazil v.1.3.4 is now available on google play store

20140515-011301.jpg

The newest version of the app above is now available with:

1. a new and much more pretty icon,
2. support for Brazilian Portuguese,
3. a totally redesigned and optimized new way of fetching the data,
4. a new splash screen on startup (while fetching data),
5. the matches are all numbered,
6. playoff matches named.

Please if you find any bugs or just have a suggestion please let me know about it.

Thanks.