Sunday, 14 February 2016

Configuring a JMS environment

In this post we will see how to create a JMS administered objects (connection factory and a destination) in a glassfish server.

The server must be running.



We can see in properties that the admin port is 4848. So the administration console can be opened in a browser with the URL : http://localhost:4848




To start configuring JMS we need to define a connection factory

The connection factory interface is an administered object, used to create the connection with the JMS provider.




So, select Resources/JMSResource/ConnectionFactories/ New:



In this new window complete:
- PoolName
- ResourceType
- Description
- Check the enabled status
- The pool settings can be left as by default.

Click OK




Now, that the connectionFactory has been created, it is necessary define a destination resource (it could be destination, queue or topic). In this example we will define a JMS point to point  so we will select queue:

Select Resources/JMSResource/DestinationResources/ New:




In this new window complete:
- JNDIName
- Physical Destination Name
- ResourceType : select javax.jms.Queue
- Check the enabled status
Click OK



Now JMS resources have been defined in the server, and we can use them to handle messaging in our application.

Saturday, 13 February 2016

Java Web Component Developer Certification OCEJWCD at a glance

OCEJWCD (SCWCD) - 1Z0-899 - Web Component Developer Certification

This post a is summary of info related Oracle JavaEE6 web component certification with all info available in this blog and other useful links:

Topics:


  • Exam:

    Further resources:

    • Books. I used HeadFirst Servlets & JSP (O'Reilly), but there are others like OCEJWCD Study Companion recommended in javaRanch.                    
    • Servlets 3.0 specification. You can download the JSR-000315 from here      
    • Java EE 6 API here                                                                                           
    • MockExams: 
      • Enthuware : there is trial version and full version.
      • Whizlabs : there is a free test and tests under purchase.



    Good luck!

    OCEJWCD (SCWCD) - 1Z0-899 - Web Component Developer Certification

    Friday, 5 February 2016

    Microservices

    Nowadays it is quite common to hear about the microservices architecture. What is the meaning behind the concept of microservices? Why is it changing the way of building software? I will try to summarize in this post the basic notions about microservices.

    A valid definition of Microservice can be the functionality that is deployable independently, for building a system in a modular way.

    Traditionally the projects in an organization are characterized by being monolithic. That means a project or group of projects with their dependences that are deployed as one entity. These projects share a database and are coded using the same language.



    On the other hand, microservices will have their own services and their own database. Also they do not need to use the same language, so the best technology can be used for each problem.

    Microservices architecture also solves a problem related with scalability. In a monolithic architecture as the project increases it needs more hardware resources (servers or containers).

    There is a very popular article about microservices in the Martin Fowler blog:
    Some main ideas about microservices:

    • Service oriented
    • Products instead of projects.
    • Smart endpoints and dump pipes communication pattern. Note :To know more about the meaning of this pattern you can read this explanation from stackoverflow. This pattern is the opposite to the use of enterprise service bus (ESB).
    • Microservices involves a team will be responsible of development, build, deployment, maintenance.
    • Decentralized data management: Microservices uses their own database. Managing distributed transactions can be a drawback, due to the need of synchronizing data.
    • Design for failure: it means that if one component fails it does not affect the others.
    • Infrastructure automation: Continous deployment, a change only requires deploying the microservice, not all the application as in monolithic architectures.

    Sunday, 31 January 2016

    OCEJBCD -6.Developing Java EE Applications Using Messaging

    OCEJBCD (SCBCD) - 1Z0-895 - Enterprise JavaBeans Developer Certification

    6.1. Review JMS technology

    JMS (Java Messaging Service) is an API to send/receive messages :

    1) Asynchronously:
    JMS works asynchronously because it sends messages to the client without a previous request, also it sends messages without having to wait for a reply.

    2) Loosely coupled: 
    Provides vendor-independent access to messaging systems.

    Messaging domain types:

    • Point to point :
      • Every message has a consumer
      • The messages are requested from the queue by the consumer.

    • Publish/Subscribe 
      • Every message has several consumers
      • The messages are pushed to the consumers without requesting.

      6.2. Describe the roles of the participants in the JMS API messaging system

    Components is a JMS system:
    • JMS Provider.- The messaging system
    • JMS Clients .- Any java component that is producer or consumer of messages
      • Producer
      • Consumer(Listener and selectors)
    • Messages .- the objects that contain the information.
    • Administered objects.- Destinations and connection factories.
    • Session.- single threaded context.
    • Connection.- connection with JMS provider.
    • Queue browsers.
    • Exception handling .


    6.3. Create a queue message producer
    MessageProducer producer = session.createProducer(queue);
    producer.send(message);
    See JMS message producer from the oracle java EE tutorial

    6.4. Create a synchronous message consumer
    MessageConsumer consumer = session.createConsumer(queue);
    connection.start();
    Message m = consumer.receive();
    See JMS message consumer from the oracle java EE tutorial

    The message can be consumed synchronously or asynchronously. 
    - synchronously : the consumer call the receive() method, that blocks until the message arrives
    - asynchronously : the consumer call the onMessage() message listener method.

    OCEJBCD (SCBCD) - 1Z0-895 - Enterprise JavaBeans Developer Certification

    Sunday, 24 January 2016

    Java 9 - The new things

    The new version Java9 is planned to be available next year.

    The main feature of Java9 is related with modularity (project Jigsaw). This feature is about having different modules for the JDK. This new feature will allow to install JDK in small devices (internet of things, cloud, mobile...), also will improve scalability and performance.


    Other new features that Java9 is likely to include are:

    • JSON API
    • HTTP2 API (WebSockets support)
    • JVM Optimization
    • Money and currency API

    This diagram represents how the language has been evolving from Java7:




    Monday, 18 January 2016

    OCEJBCD -5. Singleton Session Bean

    OCEJBCD (SCBCD) - 1Z0-895 - Enterprise JavaBeans Developer Certification

    5.1. Understand the advantages and disadvantages of using a singleton session bean

    A singleton is a stateless bean whose instance is accessed by all request in the application. Singletons have to be thread safe in order to manage the concurrently access.

    Advantages:
    - Only one instance. The bean will be created when the application is started and will exist during all application life.

    Disadvantages:
    - Could cause bottle-neck and performance, if it is not handle accurately.

    5.2. Create a singleton session bean
    javax.ejb.Singleton 
    @Singleton
    public class SingletonBean { ... }
    
    
    
    
    
    
    Other annotations that can be used with singleton are:
    
    
    
    
    @Startup (For eager initialization; it means the instance will be created at deployment time)
    
    
    @DependsOn (For adding dependencies)

    5.3. Describe the life cycle of a singleton session bean


    5.4. Implement singleton session bean life cycle methods

    For a full example using singleton see last section of this post.

    5.5. Describe singleton concurrency access

    Singleton beans are designed for concurrency access. There are two ways to manage concurrency.
    • Container-managed concurrency (by default)
    • Bean-managed concurrency
    Example:
    @ConcurrencyManagement(ConcurrencyManagementType.CONTAINER)
    @Singleton
    public class ExampleSingletonBean {...}

    For managing concurrrency, there are the following annotations to lock blocks of code:

    @Lock(LockType.READ)
    @Lock(LockType.WRITE)

    5.6. Implement a concurrency management strategy

    The following example describes the use of a singleton bean called by a servlet in order to get a random number in the page after clicking a button.

    The method that gets the random number can be accessed concurrently and the value will be consistent from different client accesses.

    Bean class:

    Servlet class:
    JSP:
    After running the application in the server, when the page is opened in a browser the first time:

    If we open another browser (emulating another client), the request accesses the same value of the counter. The counter value is going to be shared by all the requests:
    OCEJBCD (SCBCD) - 1Z0-895 - Enterprise JavaBeans Developer Certification

    Sunday, 13 December 2015

    OCEJBCD -4. Advanced Session Bean concepts

    OCEJBCD (SCBCD) - 1Z0-895 - Enterprise JavaBeans Developer Certification
    4.1. Understand the relationship between the EJB container and an EJB component.

    EJB components:
    • Remote business interface. exposes business logic to remote clients. 
    • Local business interface. exposes business logic to local clients.
    • Local no-interface. exposes public methods of the bean without using interface.
    • Bean class.
    * Note: message driven beans do not have remote or local interface.


    EJB Container
    Is the run-time container for beans deployed in the application server.
    The container manages:
    • Bean life cycle
    • Persistence
    • Transaction
    • Security
    • Concurrency

    4.2. Describe the life cycle for stateless and stateful session beans

    Stateless life cycle

    Stateful life cycle

    Note: when there is a timeout or a system exception the bean will be back to does not exist state.

    4.3. Implement session bean life cycle methods

    import javax.annotation.PostConstruct
    import javax.annotation.PreDestroy

    @PostConstruct and @PreDestroy are callbacks when bean instances are created or destroyed.

    import javax.ejb.PostActivate
    import javax.ejb.PrePassivate

    @PostActivate and @PrePassivate. To make the bean garbage collected in inactive periods, saving memory resources. The bean can be activated later and will maintain its conversational state.

    4.4. Use a session bean to perform asynchronous communication

    import javax.ejb.Asynchronous
    @Asynchronous 

    You can use asynchronous invocation on stateless session bean or singleton session beans.

    The return type of the method annotated with Asynchronous can be void or java.util.concurrent.Future<V> (java/util/concurrent/Future.html)

    Example of session asynchronous bean implementation:

    We need to define a stateless bean with an async method that is going to be invoked from a servlet. In the example we will have a page asking as for a user name and a button to submit. 

    After submission our servlet will get the request parameter "username", and will call the async method of the bean using the username as argument.

    The bean async method will receive the username, will wait 5 seconds and will return after that time a greeting message: Hello + username.

    Bean code: 

    Important:

    1. The bean is stateful session with an async method
    2. the async method returns a Future<T> object


    Servlet code:

    Important:

    1. servlet with a get method, and the bean is injected with annotation @EJB
    2. Observe that we will return the message back to the JSP when Future.isDone() that returns true when the task is completed.


    JSP code:


    The result when we executes the code above:
    http://localhost:8080/TestWeb/helloPage


    Remember:


    • TestWeb is the name of our web project(context)
    • helloPage is the name we have indicated in the annotation @WebServlet("/helloPage")



    When the button is clicked after 5 seconds the greeting message is displayed in the page:

    4.5. Have fine-grained control over packaging and deployment

    Enterprise beans can be deployed using ejb-jar or war modules.

    ejb-jar
    • root
      • classes
      • META-INF
        • ejb-jar.xml
        • manifest
    WAR
    In a WAR file we can include the classes under classes folder or include the JAR file under lib.
    • root
      • WEB-INF
        • ejb-jar.xml(optional)
        • classes (*)--> ejb classes included in this folder
        • lib
          • ejb-jar(*) --> JAR file


    OCEJBCD (SCBCD) - 1Z0-895 - Enterprise JavaBeans Developer Certification