Implementing EJBs

To use an asynchronous EJB method, we need to create a session bean and configure it to have asynchronous methods. In the following code, we have an example of the implementation of a session bean called PdfHandler, which is responsible for saving PDF files on a filesystem:

import javax.ejb.AsyncResult;
import javax.ejb.Asynchronous;
import javax.ejb.Stateless;
import java.io.IOException;
import java.util.Date;
import java.util.concurrent.Future;

@Stateless
public class PdfHandler {

@Asynchronous
public Future<String> handler (FileBean file) throws IOException {

return new AsyncResult(
FileSystemUtils.save(
file.getFile(),
"pdf",
"pdf_"+ new Date().getTime() + ".pdf" ));

}
}

In the preceding code block, we have the PdfHandler class, which contains a handler(FileBean file) method. This method is annotated with @Asynchronous to configure it as an asynchronous method. 

The following code demonstrates the configuration of the handle(FileBean file) method:

 @Asynchronous
public Future<String> handler (FileBean file) throws IOException {
//Business logic
}

This method needs to return Future<T>. In our example, we return AsyncResult, which is an implementation of the Future interface. In our example, the data returned with the Future object contains the information of the path to the file on the filesystem. In the following code, we have an example of a Future return:

return new AsyncResult(
FileSystemUtils.save(
file.getFile(),
"pdf",
"pdf_"+ new Date().getTime() + ".pdf" ));

In the following code block, we have an example of the implementation of a session bean called JpgHandler, which is responsible for saving JPG files on the filesystem:

import javax.ejb.AsyncResult;
import javax.ejb.Asynchronous;
import javax.ejb.Stateless;
import java.io.IOException;
import java.util.Date;
import java.util.concurrent.Future;

@Stateless
public class JpgHandler {

@Asynchronous
public Future<String> handler (FileBean file) throws IOException {

return new AsyncResult(
FileSystemUtils.save(
file.getFile(),
"jpg",
"jpg_"+ new Date().getTime() + ".jpg" ));

}
}

This method works as the PdfHandler method, but saves the file at another directory and with another filename pattern. In our example, the data returned with the Future object is the path to the file on the filesystem. 

In the following code, we have an example of an implementation of the session bean called JpgHandler, which is responsible for saving JPG files on the filesystem:

import javax.ejb.AsyncResult;
import javax.ejb.Asynchronous;
import javax.ejb.Stateless;
import java.io.IOException;
import java.util.Date;
import java.util.concurrent.Future;

@Stateless
public class ZipHandler {

@Asynchronous
public Future<String> handler (FileBean file) throws IOException {

return new AsyncResult(
FileSystemUtils.save(
file.getFile(),
"zip",
"zip_"+ new Date().getTime() + ".zip" ));

}
}

This method works as the PdfHandler and JpgHandler methods, but saves the file at another directory and with another filename pattern. In our example, the data returned with the Future object is the path to the file on the filesystem. 

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.59.173.242