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