Friday, May 20, 2016

JEE7 - Working with Asynchronous methods and Observer

Hi all,
Thanks for reading my posts.

Now, I would like to present some tips about Asynch implementation.
Let's see the following code below...

Environment:

Java EE 6/7 and Java SE 1.7/1.8
CDI 1.x
JBoss EAP/AS/Wildfly and so on...

- EJB Asynch


public interface CustomEvent extends Serializable {
    /**
     * Submit event execution

     *
     * @param event
     */
    void submit();
}


public class DispatchEvent implements CustomEvent {  private String name;
  protected int age;

  public DispatchEvent(String name, int age) {       this.name = name;
       this.age    = age;
  }


   public String getName() {
        return this.name;
   }   

   @Override
    public void submit() {

        // builder pattern sample...see other posts about this pattern
        StudentsBuilder studentsBuilder = new StudentsBuilder();
        studentsBuilder.addStudent()
                  .getCurrentStudentPosition()
                  .defineName(this.name)
                  .defineAge(this.age)
                  .build()
                  .print("Hello!!");
   }
}


public class Dispatch2Event extends DispatchEvent {
  private String nickname; 

  
  public Dispatch2Event(String name, int age, String nickname) {
       this.name = name;
       this.age    = age;
       this.nickname = nickname;
  }
  
  public String getNickname() {
        return this.nickname;
   }  
}
 
@Local
public interface StatelessEjbEventObserver extends Serializable {

    /**
     *  Observe the event execution...

     *
     * @param event
     */
    void onEjbAsynchEvent(@Observes
DispatchEvent event);

    void onEjbEvent(@Observes Dispatch2Event event);
}

@Stateless
public class EjbStatelessEjbEventObserverImpl implements StatelessEjbEventObserver {
 

    /**
     * serial
     */
    private static final long serialVersionUID = 2264643954576089691L;

    @Asynchronous
    @Override
    public void onEjbAsynchEvent(@Observes
DispatchEvent<E> event) {
        // do anything here...
        event.submit(); // execution

        System.out.println("Hello: " + event.getName());    }

    @Override
    public void onEjbEvent(@Observes
Dispatch2Event<E> event) {
        // do anything here...
        event.submit(); // execution

        System.out.println("Hello2: " + event.getNickname());
    }





}


Pay attention if you need to work with Thread Safe...
The event will be fired from a CDI Object and after that the EJB will dispatch the "submit" method as an Asynchronous operation.  
Take it easy to avoid overhead on performance stuff, if you are using transactions, because the container must lost the control over Asynch method.

- CDI Service

public class PersonService {
   @Inject
    private Event<DispatchEvent> dispatcher;
 
    @Inject
    private Event<Dispatch2Event> dispatcher2;
  
   public dispatch(DispatchEvent event) {

        dispatcher.fire(event);  
    }

    public dispatch(Dispatch2Event event) { 
        dispatcher2.fire(event);  
    }

} 
- Singleton EJB (Startup)

@Singleton
@Startup
@TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
public class EjbSingletonStartup {
   
   //@Inject // repository implementation...access person data information
   //private PersonRepository personRepo;
  
   @Inject PersonService pService;
 
   @PostConstruct
    protected void init() throws InterruptedException, ExecutionException, TimeoutException {

        //PersonLegacy p = personRepo.findById(0L); // ADMIN test sample
       
        pService.dispatch(new DispatchEvent("alvesfred", 38));
        pService.dispatch2(new Dispatch2Event("alvesfred", 38, "fred"));
   }
}


So, it is just a sample concept and not a real implementation or production code.
In JEE 7, I advice you to use services like ManagedExecutorService (see the assumptions over JSR-236 to know more about concurrency/multithread).

Thanks a lot and regards.



 

Thursday, May 12, 2016

Java Application Performance Management - inspectIT

Hi all...

Thanks again to stay here, reading my post.

Today i will show how to install and configure inspectIT. It is a good tool for java application performance management.

Let's see my environment:

- Ubuntu 15
- JBoss EAP 6.4
- inspectIT 1.5
- JDK 1.8

inspectIT Download

http://www.inspectit.rocks/

After complete the download of the jar file...

# java -jar inspectit.installer-all.linux.x64.jar

Install inspectIT and features by default...

Editing - inspectIT configuration file

# vim /opt/java/inspectIT/agent/config/inspectit-agent.cfg

Uncomment these lines:

$include common/ejb.cfg
$include common/http.cfg
$include common/hibernate.cfg
#$include common/struts.cfg
$include common/jsf.cfg
$include common/jpa.cfg
$include common/jta.cfg
$include common/webservice.cfg


Save the file...

Edit the JBoss Standalone configuration file

# vim $jboss-home/bin/standalone.conf

...
# Added for logging problem
JBOSS_MODULES_SYSTEM_PKGS="org.jboss.logmanager"

if [ "x$JBOSS_MODULES_SYSTEM_PKGS" = "x" ]; then
   JBOSS_MODULES_SYSTEM_PKGS="org.jboss.byteman"
fi

...

#inspectIT
INSPECTIT_AGENT_HOME="/opt/java/inspectIT/agent"
echo $INSPECTIT_AGENT_HOME

INSPECTIT_CONFIG_HOME="/opt/java/inspectIT/agent/config/"
echo $INSPECTIT_CONFIG_HOME

INSPECTIT_JAVA_OPTS="-Xbootclasspath/p:$INSPECTIT_AGENT_HOME/inspectit-agent.jar -javaagent:$INSPECTIT_AGENT_HOME/inspectit-agent.jar -Dinspectit.config=$INSPECTIT_CONFIG_HOME"
echo $INSPECTIT_JAVA_OPTS

...

# Sample JPDA settings for remote socket debugging
JAVA_OPTS="$INSPECTIT_JAVA_OPTS $JAVA_OPTS -agentlib:jdwp=transport=dt_socket,address=8787,server=y,suspend=n"

# Added for logging problem
JBOSS_HOME="/opt/java/jboss/jboss-eap-6.4.0"
JAVA_OPTS="$JAVA_OPTS -Djava.util.logging.manager=org.jboss.logmanager.LogManager -Xbootclasspath/p:$JBOSS_HOME/modules/system/layers/base/org/jboss/logmanager/main/jboss-logmanager-1.5.4.Final-redhat-1.jar"

Let's start JBoss...

# $JBOSS_HOME/bin/standalone.sh -c standalone-full-ha.xml

Let's start the Local CMR (repository)

# $INSPECTIT_HOME/CMR/startup.sh
(it will be possible to see that the service and 8182 port were initialized)

Now, local CMR is ready to receive informations about your application inside JBoss EAP Middleware, using inspectIT agents/sensors.

That's it!!
So, we need to manage the java application performance using inspectIT client...

# $INSPECTIT_HOME/inspectit/inspectIT



Thanks a lot and see you on the next post!

If you have any doubts or problems with java features, please let me know. I will do my best to help you.



Wednesday, May 4, 2016

Hibernate/JPA Entity Model

Hi All,

Thanks again to stay here reading my post.

I would like to help other developers to solve problems about Java and middleware implementations.

If you have any doubts or problems about Java/JEE or JBoss Midleware feel free to contact me (fredericocerqueiraalves@gmail.com). It will be a pleasure to help you.

Now, i would like to show you a model implementation to use hibernate entity beans.

These classes are implemented to support hibernate first or second level cache, audit with envers and native or named queries.

So, let's see...

- Base Model

package br.com.alvesfred.entity;
 

import java.io.Serializable;

/**
 * Base Model Interface
 *
 * @author alvesfred
 *
 */
public interface BaseModel<ID> extends Serializable {

    /**
     * Set ID definition
     *
     * @param id
     */
    void setId(ID id);
   
    /**
     * Get ID
     *
     * @return
     */
    ID getId();
}


- Entity Base Model

package br.com.alvesfred.entity;

import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.Parameter;

/**
 * Model for Entities
 *
 * @author alvesfred
 *
 * @param <ID>
 */
@MappedSuperclass
public abstract class BaseModelEntity<ID> implements BaseModel<ID> {

    /**
     * serial
     */
    private static final long serialVersionUID = 3958601406206249649L;

    //@Id
    //@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "default_id_generator")
    @Id
    @Column(name = "id")
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "hilo_sequence_generator")
    @GenericGenerator(
            name       = "hilo_sequence_generator",
            strategy   = "org.hibernate.id.enhanced.SequenceStyleGenerator",
            parameters = { // default parameters
                    @Parameter(name = "sequence_name",  value = "sq_id"),
                    @Parameter(name = "initial_value",  value = "1"),
                    @Parameter(name = "increment_size", value = "1"),
                    @Parameter(name = "optimizer",      value = "hilo")
    })
    protected ID id;

    public BaseModelEntity() {
    }

    public BaseModelEntity(ID id) {
        setId(id);
    }

    public ID getId() {
        return id;
    }

    public void setId(ID id) {
        this.id = id;
    }

    @Override
    public int hashCode() {
        HashCodeBuilder hcb = new HashCodeBuilder();
        hcb.append(this.id);

        return hcb.toHashCode();
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null || obj.getClass().isAssignableFrom(BaseModelEntity.class)) {
            return false;
        }

        @SuppressWarnings("unchecked")
        BaseModelEntity<ID> other = (BaseModelEntity<ID>) obj;

        EqualsBuilder eb = new EqualsBuilder();
        eb.append(this.getId(), other.getId());
       
        return eb.isEquals();
    }
   
}


- Version (Entity Base Model)

 package br.com.alvesfred.entity;

import javax.persistence.Column;
import javax.persistence.MappedSuperclass;
import javax.persistence.Version;

/**
 * Lock version entity - base model
 *
 * @author alvesfred
 *
 * @param <ID>
 * @param <VERSION>
 */
@MappedSuperclass
public abstract class BaseModelVersionEntity<ID> extends BaseModelEntity<ID> {

    /**
     * serial
     */
    private static final long serialVersionUID = 9205887194329558055L;

    //@Generated(GenerationTime.NEVER)
    @Version
    @Column(name = "versionId", nullable = false)
    private Long version;

    public Long getVersion() {
        return (this.version != null) ? this.version : this.hashCode();
    }

    public void setVersion(Long versao) {
        this.version = versao;
    }
}


- Finally Entity Class

 package br.com.alvesfred.entity;

import javax.persistence.AttributeOverride;
import javax.persistence.AttributeOverrides;
import javax.persistence.Column;
import javax.persistence.ColumnResult;
import javax.persistence.Entity;
import javax.persistence.Lob;
import javax.persistence.NamedNativeQueries;
import javax.persistence.NamedNativeQuery;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.QueryHint;
import javax.persistence.SequenceGenerator;
import javax.persistence.SqlResultSetMapping;
import javax.persistence.SqlResultSetMappings;
import javax.persistence.Table;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.envers.Audited;

@Cache(usage = CacheConcurrencyStrategy.TRANSACTIONAL) // using JTA by container
@Entity
@Audited
@Table(name="person")
@SequenceGenerator(name = "hilo_sequence_generator", sequenceName = "seq_id_person", allocationSize = 1)
@AttributeOverrides({
    @AttributeOverride(name="id", column=@Column(name = "person_id", unique = true, nullable = false, scale = 0)),
})

@NamedQueries({
    @NamedQuery(
            name  = PersonEntity.LIST_BY_ID_NAME,
            query = "select new PersonEntity(p.id) "
                  + "from PersonEntity p "
                  + "where p.name = :name",
            hints = {
                    @QueryHint (name="org.hibernate.cacheable", value="true"),
            }
    ),
})

@NamedNativeQueries(
        value = {
            @NamedNativeQuery(
                name  = PersonEntity.LIST_NATIVE_BY_NAME,
                query = PersonEntity.SQL_NATIVE_BY_NAME,
                resultSetMapping = PersonEntity.MAPPING_NATIVE_BY_NAME),
})

@SqlResultSetMappings(
        value = {
            @SqlResultSetMapping(      
                    name    = PersonEntity.MAPPING_NATIVE_BY_NAME,
                    columns = {
                        @ColumnResult(name="size")}),
        }
)
public class PersonEntity extends BaseModelVersionEntity<Long> {

    private static final long serialVersionUID = -1355055022303449688L;

    // Named Queries
    public static final String LIST_BY_ID_NAME = "PersonEntity.listByIdName";

    // Native Named Queries
    public static final String LIST_NATIVE_BY_NAME = "PersonEntity.nativeListByIdName";

    // SQL
    protected static final String SQL_NATIVE_BY_NAME =
            " select count(*) as size "
          + " from person "
          + " where name = :name ";

    // MAPPING
    public static final String MAPPING_NATIVE_BY_NAME = "PersonEntity.mappingListByIdName";

    @Lob
    @Column(name = "jsonInfo", nullable = false)
    private String jsonInfo; // a complete file text information, using json jackson parser
   
    @Column(name = "name", nullable = false)
    private String name;

    @Column(name = "age", nullable = true)
    private int age;

}
 

That's it!
Thanks a lot and feel free to contact me for any help.

See you soon...

Sunday, May 1, 2016

How to Analyse Java Performance using Red Hat Links

Hi All,

I would like to present links that I use to analyse java performance.
These links are important to know some details about JVM, but you need to subscribe on Red Hat Access Portal to see the details.

[IMHO] To subscribe on Red Hat Access Portal is a important action if you deploy applications over JBoss EAP. On this site, you can obtain a lot of concepts about JBoss and how to solve problems, especially for performance issues on your application running to JVM over JBoss EAP.

So, I enumerate some links to help you with Java and JBoss issues. These links help me a lot!

- Java Application High CPU

https://access.redhat.com/node/24830
https://access.redhat.com/solutions/46596


- Thread Dump

https://access.redhat.com/solutions/18178
https://access.redhat.com/solutions/317143

- Java Garbage Collection Performance

https://access.redhat.com/articles/1192773
https://access.redhat.com/node/19932
https://access.redhat.com/labs/garbagedog/
https://access.redhat.com/solutions/23735

- Application Unresponsive
https://access.redhat.com/solutions/18266
https://docs.jboss.org/author/display/AS7/High+Availability+Guide

- JVM Conf. Tool
https://access.redhat.com/labs/jvmconfig

If you have difficulties to know about java memory leak, I just suggest to read about jmap, jhat and MAT (Eclipse Memory Analyser). These tools are so important to detect a lot of issues about your application running on java virtual machine.

Thanks a lot for reading my post.
See you!!!


Wednesday, April 27, 2016

Let's Relax! Builder Pattern Example and Lombok API

Hi All,

Thanks again for reading my comments over internet.

I am trying to help java developers to know more about implementations and configurations using Java or JBoss Midleware technologies.

Now i would like to show a simple implementation pattern: Builder.
So, let's see the example:

1 - Simple Person class

/**
 * Person simple class information
 *
 * @author alvesfred
 *
 */
public class SimplePerson implements Serializable {

    /**
     * serial
     */
    private static final long serialVersionUID = -978731152684129660L;

    private String name;
    private int age;
    private boolean male;
    private boolean married;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public boolean isMale() {
        return male;
    }

    public void setMale(boolean male) {
        this.male = male;
    }
   
    public boolean isMarried() {
        return married;
    }
   
    public void setMarried(boolean married) {
        this.married = married;
    }
}


2 - Builder Pattern

/**
 * Builder pattern
 *
 * @author alvesfred
 *
 */
public abstract class Builder<T> {
    /**
     * Build
     *
     * @return
     */
    public abstract T build();
}
 

/**
 * Builder pattern class
 *
 * @author alvesfred
 *
 */
public class Person {

    private final String name;
    private final int age;
    private final boolean male;
    private final boolean married;

    public Person(PersonBuilder pfb) {
        this.name    = pfb.name;
        this.age     = pfb.age;
        this.male    = pfb.male;
        this.married = pfb.married;
    }

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

    public boolean isMale() {
        return male;
    }

    public boolean isMarried() {
        return married;
    }

    /**
     * Builder class sample
     *
     * @author alvesfred
     *
     */
    public static class PersonBuilder extends Builder<Person> {
        private final String name;
        private int age;
        private boolean male;
        private boolean married;

        public PersonBuilder(String name) {
            this.name = name;
        }

        public PersonBuilder defineAge(int age) {
            this.age = age;
            return this;
        }

        public PersonBuilder defineMale(boolean male) {
            this.male = male;
            return this;
        }

        public PersonBuilder defineMarried(boolean married) {
            this.married = married;
            return this;
        }

        public Person build() {
            return new Person(this);
        }
    }

    @Override
    public String toString() {
        return "[PERSON] "        + name
                + " - [Age] "     + age
                + " - [Male?] "   + male
                + " - [Married] " + married;
    }

    /**
     * Main
     *
     * @param args
     */
    public static void main(String[] args) {
        Person p = new PersonBuilder("Fred")
            .defineAge(38)
            .defineMale(true)
            .defineMarried(true)
            .build();

        System.out.println(p);
    }
}


A few months ago i see this important API, and i like it so much!
Lombok API is very interesting...have fun!

https://projectlombok.org/

[IMHO] for @Data annotation using lombok, I just recommend override equals and hashCode methods to use HashCodeBuilder and EqualsBuilder (Apache Commons).


Thanks a lot and see you next time!

"Move to Canada/Vancouver - it is my dream! Java/JEE/Middleware development is my work and i love it so much!"