Saturday 26 March 2016

OCEJBCD -8.Using Timer Services

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

8.1. Describe timer services

Timer services is a service of scheduling notifications in the bean container.
(Available for all bean types except from stateful session beans).

Timers services can be created 
-Programmatically, using TimerService interface
-By annotations @Schedule

Functionality that a bean with timer services can perform:
  • Create a timer notification callback
  • Process a timer notification callback
  • Manage timer objects (list notification, cancel timer etc...)




The timers can be classified as:
  • Single notifications
  • Interval (multiple notifications at specified interval)
  • Calendar based schedule 
    • second, minute, hour 
    • dayOfWeek, dayOfMonth
    • month
    • year
    • Expressions : wildcard (*), list(,), range(-),intervals(/)

8.2. Create a timer notification callback
  • By timerService interface
    • Single:
          Timer timer = timerService.createSingleActionTimer(data, new             TimerConfig());
    • Calendar based: using javax.ejb.ScheduleExpression
          ScheduleExpression schedule = new ScheduleExpression();
          schedule.dayOfMonth("1,Last");//first and last day of the month
          Timer timer = timerService.createCalendarTimer(schedule);

       When a timer expires the container calls the bean's method annotated              with @Timeout (*Note: this method must return void and the argument is optional).

  • By annotation
       Example : @Schedule(dayOfWeek="Mon", hour="0")

8.3. Process a timer notification callback 

Example with timerService interface

To execute this bean we need to make a call from a client (for example a servlet class) to the method setTimer that indicates the milliseconds after the timeout will happen: scheduleBean.setTimer(30000);

Results in server console:
2016-03-26T11:58:33.313+0000|Info: TestBean was successfully deployed in 362 milliseconds.
2016-03-26T12:00:26.816+0000|Info: the next timeout is programed Sat Mar 26 12:00:56 GMT 2016 happening in 29968 milliseconds

2016-03-26T12:00:56.787+0000|Info: Timeout occurred

Example with schedule annotation
The following example is a stateless session bean, with a timer scheduled every 1 min.
When it is deployed in the server, after 1 minute the timeout method is called:

Results in server console:
2016-03-26T10:03:00.001+0000|Info: Automatic timeout occured
2016-03-26T10:04:00.002+0000|Info: Automatic timeout occured
2016-03-26T10:05:00.001+0000|Info: Automatic timeout occured
2016-03-26T10:06:00.002+0000|Info: Automatic timeout occured
2016-03-26T10:07:00.002+0000|Info: Automatic timeout occured

8.4. Manage timer objects

-Cancel() - cancel a timer
-getHandle() - obtain a serializable object
-getTimeRemaining()
-getNextTimeout()
-getInfo()

When using container-managed transactions @Timeout can use attributes: Required or RequiredNew to preserve transaction integrity, which means that in case of rollback the timeout will be reset.

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

Sunday 13 March 2016

How to upload files using Servlet technology

This time, the aim of this post is to describe how to upload files to a server using servlet technology.

Upload files with Java technology is easy, we only need to create a JSP page and a servlet class, but we have to take into account this form will be a bit different than usual:
  • Method POST 
  • multipart/form-data 

Form declaration in the JSP:
<form action="upload" method="post" enctype="multipart/form-data">

Annotations in the servlet declaration:
@WebServlet("/UploadServlet")
@MultipartConfig
public class UploadServlet extends HttpServlet {

JSP code:


Servlet code:
* This example project is using servlet library version 3.0 or above.



Method post - summary:

1) Getting the file attribute from the request object. file attribute corresponds
with the name of the input type ="file" in the JSP. The object obtained is Part type.

2) Obtain the name of the file uploaded:
using filePart.getHeader to obtain the value of the property filename.

3) Defining an object OutputStream with the destination path, plus the file name).

4) Read the content of the object filepart obtained in point 1, and writing the bytes into the ouputStream (in the destination).


Running the application:  
http://localhost:8080/UploadWeb/

 Select a file from the local disk and click the button to submit the request:

The server logs will display this message:
INFO: Reloading Context with name [/UploadWeb] is completed

Checking the execution result (the path selected in the example is also a local directory C:/uploads), we can verify the file has been uploaded successfully to the selected destination.