Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Java EE 8 Design Patterns and Best Practices

You're reading from   Java EE 8 Design Patterns and Best Practices Build enterprise-ready scalable applications with architectural design patterns

Arrow left icon
Product type Paperback
Published in Aug 2018
Publisher Packt
ISBN-13 9781788830621
Length 314 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Authors (3):
Arrow left icon
Rhuan Rocha Rhuan Rocha
Author Profile Icon Rhuan Rocha
Rhuan Rocha
Paulo Alberto Simoes Paulo Alberto Simoes
Author Profile Icon Paulo Alberto Simoes
Paulo Alberto Simoes
Joao Carlos Purificação Joao Carlos Purificação
Author Profile Icon Joao Carlos Purificação
Joao Carlos Purificação
Arrow right icon
View More author details
Toc

Table of Contents (14) Chapters Close

Preface 1. Introduction to Design Patterns 2. Presentation Patterns FREE CHAPTER 3. Business Patterns 4. Integration Patterns 5. Aspect-Oriented Programming and Design Patterns 6. Reactive Patterns 7. Microservice Patterns 8. Cloud-Native Application Patterns 9. Security Patterns 10. Deployment Patterns 11. Operational Patterns 12. MicroProfile 13. Other Books You May Enjoy

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...

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at ₹800/month. Cancel anytime