Free Online Courses for Software Developers - MrBool
× Please, log in to give us a feedback. Click here to login
×

You must be logged to download. Click here to login

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

How to create a Session Facade

In this article we will see the process of creating a session facade. Many modern-day web-oriented and enterprise-oriented applications are built using tiered and layered architectures.

Tiered architectures typically break the application into pieces, and then have the pieces run on separate physical machines. You are probably familiar with tiered solutions either as a client-server solution or a three-tiered architecture. Layering is commonly used within EJB applications. In many cases, the one layer (an EJB) acts as a façade to another layer (another EJB), simplifying and encapsulating the interactions. In fact, the practice of layering is so common in EJB development, there is a design pattern for it, the Session Façade pattern.

Session Facade

Figure 1: Session Facade

There are two ways to implement the Session Façade pattern: dependency injection or programmatic lookup of an EJB from the naming system.

Dependency Injection of a Session Bean

Creating a session façade through dependency injection is simple -you can have a reference to the EJB directly injected. Dependency injection of an EJB is achieved by declaring a reference to the EJB (either its business interface or the no-interface class) and annotating it with the @EJB annotation. In order for the application server to determine which EJB to inject, it first looks for an EJB based on the variable name. If the variable name matches a bean name (found in the Global JNDI namespace), it injects the reference. Otherwise, if the application server can’t find an EJB in the namespace based on the variable name, it then relies on the reference type. Alternatively, you can explicitly tell the injection mechanism which EJB component to use by specifying the mappedNamevalue as part of the annotation.

Context Lookup of a Session Bean

Java EE EJB components typically have a reference to an EJBContextobject that provides access to environmental data and manipulation, such as security information, transaction control, and resource lookup. Because an EJBContextobject is used to provide several critical pieces of information, one is usually already present in an EJB component. The EJBContextobject works in a similar way to a JNDI Contextand can be used in place of a JNDI Contextif desired. The EJBContextobject performs JNDI lookups within the java:comp/envcontext and can be used to locate EJB references, data sources, and any other resource reference that can be stored in JNDI. Using an EJBContextremoves the need to repeatedly call a new InitialContextmethod and simplifies exception handling by only causing runtime exceptions.

In this exercise, you will transform the HelloWorldBeaninto a session façade. The session façade will delegate the sayHellofunctionality to the local stateless session bean. This exercise contains the following sections:

  • “Task 1 -Create TimeBasedHelloWorldBean”
  • “Task 2 -Modify HelloWorldBean”
  • “Task 3 -Build and Deploy the EJB31 Application”
  • “Task 4 -Test the Session Façade”

This exercise assumes that the application server is installed and running. As a review, the steps to create a session bean are as follows: 1.Declare a business interface for the session bean or use a no-interface implementation. 2.Annotate the business interface with the desired client access mode. 3.Create a class that implements the business interface. 4.Annotate the class with the desired session bean type. 5.Package the session bean. You will be using the EJB creation wizard provided by the NetBeans IDE.

Task 1 - Create TimeBasedHelloWorldBean

In this task, you create and implement a local stateless session bean named TimeBasedHelloWorldBean. TimeBasedHelloWorldBeangenerates a greeting based on the period of day. TimeBasedHelloWorldBeanis accessed by HelloWorldBean:

Right-click on the EJB31-ejbmodule in the Projects tab and choose: New > Session Bean 2.Fill in the EJB information in the wizard to create a no-interface stateless session bean:

  • Specify TimeBasedHelloWorldBeanas the EJB Name.
  • Define the package as helloworld.beans.
  • Leave Stateless selected for the Session Type.
  • Click the Finish button.

Verify the EJB was created properly by examining the Source Packages node of the EJB31-ejbmodule in the Projects tab:

  • helloworldbeans.TimeBasedHe lloWorldBean-Represents the business interface and the implementation of the EJB.
  • @LocalBean(within the TimeBasedHelloWorldBean.jav a)-Denotes the EJB is a no-interface, local bean.

Add an import statement for java.util.Calendar. import java.util.Calendar;

Implement the sayHellomethod in the TimeBasedHelloWorldBean:

  • Right-click in the editor and choose Insert Code > Add Business Method
  • Specify the method information as follows:
    • Name: sayHello
    • Return Type: String
    • Click the OK button.

Listing 1: Modify the sayHellomethod created in the source to return a time-sensitive greeting, as follows:

public String sayHello() { 

	String greeting = "Zzzzzzz"; 
	Calendar cal = Calendar.getInstance(); 
	int hourOfDay = cal.get (Calendar.HOUR_OF_DAY); 
	
		if (hourOfDay > 6 && hourOfDay < 12) { 
			greeting = "Good Morning"; 
		} else if (hourOfDay >= 12 && hourOfDay < 16) {
			greeting = "Good Afternoon"; 
		} else if (hourOfDay >= 16 && hourOfDay < 19) {
			greeting = "Good Evening"; 
		} else if (hourOfDay >= 19 && hourOfDay < 22) {
		 	greeting = "Good Night"; } return greeting; 
		}

Task 2 - Modify HelloWorldBean

In this task, you will transform the HelloWorldBean into a session façade. Complete the following steps:

  • Open the HelloWorldBean.javafile in the source code editor window.
  • Add a reference to TimeBasedHelloWorldBean:
    • Right-click in the editor and choose Insert Code > Call Enterprise Bean.
    • Select TimeBasedHelloWorldBeanfrom the list.
    • Click the OK button. d. Review the @EJBdeclaration that was inserted. @EJB private TimeBasedHelloWorldBean timeBasedHelloWorldBean; Modify the sayHellomethod to invoke the sayHellomethod of theTimeBasedHelloWorldand return the result. public String sayHello() { return timeBasedHelloWorldBean.sayHello ();}

Task 3 - Build and Deploy the EJB31Application

In this task, you build, verify, and deploy the EJB31 EAR file on a Java EE application server. Complete the following steps:

  • Build the EJB31application: a. Right-click on the EJB31application and select Build. b.Verify there were no errors during the build of the EJB31application by reviewing the output in the EJB31 (dist) tab.
  • Verify that a distribution of the application was created by using the Files tab of the IDE. Check that the following file has been created:
    • EJB31/dist/EJB31.ear-the application
    • EJB31-app-client/dist/ EJB31-app-client.jar-the application client
    • EJB31-ejb/dist/EJB31-ejb.jar-the EJB module (containing HelloWorldBean)

Note If the EJB31.earfile does not exist, try rebuilding the entire EJB31 project.

Deploy the EJB31application.IDE Projectstab > Right-click EJB31 > Deploy Verify the deployment of the EJB31 application worked: a. Review the output in the EJB31 (run-deploy) tab, looking for any unexpected errors. b.Review the deployed Applications of the GlassFish server:

  • In the Services tab, expand the Servers node.
  • In the Servers node, expand the GlassFish v3 Domain node. In the GlassFish v3 Domain node, expand the Applications node. You should see EJB31as a deployed application.

Task 4 -Test the Session Façade

There are two applications that can be used to test the execution of the session façade: standaloneapp.Mainand ejb31.Main. You will use both applications to test the façade functionality. They should yield the same results. You do not need to make any modifications to either application to test the newly created TimeBasedHelloWorldBean.To execute the ejb31.Mainapplication in NetBeans, complete the following steps:

  • Verify that the EJB31application is deployed and running in GlassFish.
  • In the Projects tab, right-click on the EJB31 project:
    • Select Run from the pop-up menu.
    • Examine the output in the EJB31 (run) tab. You should observe the output similar to the following: Main.main: calling HelloWorldBean main: HelloWorldBean said: Good Afternoon To execute the standaloneapp.Mainapplication in NetBeans, complete the following steps:
    • Verify the EJB31application is deployed and running in GlassFish
    • In the Projects tab, right-click on the StandAloneAppproject:
      • Select Runfrom the pop-up Menu
      • Examine the output in the StandAloneApp (run)tab You should observe the output similar to the following: standaloneapp.Main.main: looking up bean at:
  • java:global/EJB31/ EJB31- ejb/HelloWorldBean Main.main: found HelloWorldBean: helloworld.beans._HelloWorldRemot e_Wrapper@2c9b19df standaloneapp.Main.main: calling sayHello standaloneapp.Main.main: HelloWorldBean said: Good Afternoon

Note : The message may differ based on the time of day.

Conclusion

In Java EE a three-tiered architecture could have a web tier, an EJB tier, and a “legacy” tier. Layering in a software architecture describes the concept of organizing functional components on top of each other. Proper layering creates abstraction between components, lowering the dependencies within the system.



Have good knowledge on Java, HTML, CSS and Android platform and is pursuing Masters in Computer Applications.

What did you think of this post?
Services
[Close]
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually – in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login