0% found this document useful (0 votes)
43 views31 pages

Steps For Creating Session Bean: Creating The Java Class Library

The document provides steps for creating a session bean and message driven bean in Java EE. It describes creating a Java class library project to hold the remote interface, then an enterprise application project with an EJB module to contain the beans. It shows how to generate a session bean class using a wizard, add business methods, deploy the application, and access the session bean from a client program. Finally, it briefly mentions creating a message driven bean by right clicking the EJB module project.

Uploaded by

ktha_1
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views31 pages

Steps For Creating Session Bean: Creating The Java Class Library

The document provides steps for creating a session bean and message driven bean in Java EE. It describes creating a Java class library project to hold the remote interface, then an enterprise application project with an EJB module to contain the beans. It shows how to generate a session bean class using a wizard, add business methods, deploy the application, and access the session bean from a client program. Finally, it briefly mentions creating a message driven bean by right clicking the EJB module project.

Uploaded by

ktha_1
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 31

Steps for creating Session Bean

Creating the Java Class Library


In this section you will create a Java Class Library project that will contain the remote interface for the EJB. The remote interface behaves as an API for the EJB that is used by clients to communicate with the EJB. The library JAR is easy to distribute to any clients that may need to call the EJB. Clients that want to access the EJB only need to add the library JAR to the project classpath. The EJB implementation uses the same JAR to implement the interface. 1. Choose File > New Project (Ctrl-Shift-N; -Shift-N on Mac) and select Java Class Library in the Java category. Click Next. 2. Type SessionRemoteInterface for the Project Name. Click Finish.

When you click Finish, the IDE creates a Java Class Library project. In the next section you will create a Java EE enterprise application and an EJB module. You will then use a wizard to create a session bean and the remote interface for your session bean in the Class Library project. The application client will access the session bean via the interface in the class library.

Creating an EJB Module

In this section you will create an enterprise application and an EJB module. When you create an EJB, the EJB should be created as part of an enterprise application and packaged as an EAR archive and deployed in to the server.

Creating the Enterprise Application


In this section you will use the New Project wizard to create an enterprise application containing an EJB module. The wizard provides an option to create an EJB module when you create the application. 1. Choose File > New Project and select Enterprise Application in the Java EE category. Click Next. 2. Type SessionEntAppEJB for the Project Name. Click Next. 3. Select GlassFish Server 3 for the Server. 4. Confirm that Create EJB Module is selected and deselect Create Web Application Module. Click Finish.

When you click Finish, the IDE creates an enterprise application and an EJB module. In the Projects window, you can see that the EJB Module project is listed under the Java EE Modules node of the enterprise application project.

You can see that three types of projects are now listed in the Projects window: class library, enterprise application and EJB module.

Creating the Session Bean


In this exercise you will use a wizard to create a session bean in the EJB module project. In the wizard you will also create a remote interface for the session bean in the Class Library project. 1. 2. 3. 4. 5. 6. Right-click the EJB module project and choose New > Session Bean. Type MySession for the EJB Name. Type ejb for the Package. Select Stateless for the Session Type. Select the Remote option for Create Interface. Select the SessionEJBRemoteInterface project from the dropdown list. Click Finish.

When you click Finish, the IDE creates the session bean in the ejb package in the EJB module and opens the class in the editor. You can see that MySession implements the MySessionRemote interface and that the EJBRemoteInterface JAR was added as a library of the EJB module. The wizard also creates a remote interface named MySessionRemote in the ejb package of the EJBRemoteInterface project. The IDE automatically adds the Java EE 6 API Library that is required for the EJB interface.

Adding a Business Method


In this exercise you will create a simple business method in the session bean that returns a string. 1. Right-click in the editor of MySession and choose Insert Code (Alt-Insert; Ctrl-I on Mac) and select Add Business Method. 2. Type getResult for the Method Name and String for the Return Type. Click OK. 3. Make the following changes to modify the getResult method to return a String. The class should look like the following.
@Stateless public class MySession implements MySessionRemote { public String getResult() { return "Calculator"; } }

4. Save your changes. Add one more business method in the session bean that returns a integer.

5. Right-click in the editor of MySession and choose Insert Code (Alt-Insert; Ctrl-I on Mac) and select Add Business Method. 6. Type add for the Method Name and int for the Return Type. Click OK. 7. Make the following changes to modify the add method to return a int. 8. Add parameters ----Click on add button type num1 in name choose int from type 9. Click ok The class should look like the following.
@Stateless public class MySession implements MySessionRemote { public String getResult() { return "Calculator"; } public int add(int a,int b) { return a+b; } }

10. Save your changes.

You now have an enterprise application with a simple EJB that is exposed through a remote interface. You also have an independent class library that contains the EJB interface that can be distributed to other developers. Developers can add the library to their projects if they want to communicate with the EJB that is exposed by the remote interface and do not need to have the sources for the EJB. When you modify the code for the EJB, you only need to distribute a JAR of the updated class library if any of the interfaces change. When you use the Add Business Method dialog, the IDE automatically implements the method in the remote interface.

Deploying the Enterprise Application


You can now build and run the enterprise application. When you run the application, the IDE will deploy the EAR archive to the server. 1. Right-click the EntAppEJB enterprise application and choose Deploy. When you click Deploy, the IDE builds the enterprise application and deploys the EAR archive to the server. If you look in the Files window you can see that the EJBRemoteInterface JAR is deployed with the application.

In the Services window, if you expand the Applications node of GlassFish Server 3 you can see that EntAppEJB was deployed.

Creating the Application Client


In this section you will create an enterprise application client. When creating the application client, the project needs the EJBRemoteInterface Java class library as a library in order to reference the EJB. When you run the enterprise application, the IDE will package the application client and the Java class library JAR in the EAR archive. Library JARs must be packaged in an EAR with the application client if you want to access the JARs from the application client.

Creating the Enterprise Application Client


In this exercise you will use the New Project wizard to create an application client project. If you are using deploying to GlassFish 3.1 you can create and run an application client as a standalone project. The application client no longer needs to be deployed and run as part of an enterprise application. Note. If you are deploying to GlassFish 3.0.1, you need to create the application client as a module in an enterprise application project and run the enterprise application. 1. Choose File > New Project and select Enterprise Application Client in the Java EE category. Click Next. 2. Type EntAppClient for the Project Name. Click Next. 3. Select GlassFish Server 3.1 for the Server. Click Finish. Note that you do not need to add the project to an enterprise application.

When you click Finish, the IDE creates the application client project and opens Main.java in the editor.

Adding the Session Class to client


If the class library project is not open, you can add the class library to the project in the Projects window by right-clicking the Libraries node and locating the JAR of the EJBRemoteInterface project. 1. Expand the Source Packages node of the EntAppClient project and open Main.java in the editor. 2. Right-click in the source code and choose Insert Code (Alt-Insert; Ctrl-I on Mac) and select Call Enterprise Bean to open the Call Enterprise Bean dialog. 3. Expand the EntAppEJB project node and select MySession. Click OK.

The dialog automatically selects Remote as the interface type. When you click OK, the IDE adds the following annotation to Main.java.
@EJB private static MySessionRemote mySession;

The IDE also automatically adds EJBRemoteInterface as a project Library. 4. Modify the main method to retrieve the String of the getResult method via the MySessionRemote interface. Save your changes.
5. public static void main(String[] args) { 6. System.err.println("result = " + mySession.getResult()); }

Running the Application Client


You can now run the application client by building and deploying the EntAppClient project. 1. Right-click the main.java file in the Projects window and choose Run. Alternatively, you can expand source package and right-click the Main.java class and choose Run File. When you click Run, the IDE builds the application client project and deploys the JAR archive to the server. You can see the message from the application client in the Output window.

Steps for Creating Message Driven Bean

Creating an EJB Module


In this section you will create an enterprise application and an EJB module. When you create an EJB, the EJB should be created as part of an enterprise application and packaged as an EAR archive and deployed in to the server.

Creating the Enterprise Application


In this section you will use the New Project wizard to create an enterprise application containing an EJB module. The wizard provides an option to create an EJB module when you create the application. 1. Choose File > New Project and select Enterprise Application in the Java EE category. Click Next. 2. Type EntAppEJB for the Project Name. Click Next. 3. Select GlassFish Server 3 for the Server. 4. Confirm that Create EJB Module is selected and deselect Create Web Application Module. Click Finish.

When you click Finish, the IDE creates an enterprise application and an EJB module. In the Projects window, you can see that the EJB Module project is listed under the Java EE Modules node of the enterprise application project.

You can see that three types of projects are now listed in the Projects window: class library, enterprise application and EJB module.

Create Message Driven beanRight click on ejb file goto to new select Message driven bean

Add a Message Driven Bean Give the Bean a name and a package. Click next to destinations on the Add button to add a destination. Choose a name for your destination. Choose Queue

Name your bean Netbeans creates code for you and, except for the actual businesscode, your bean is ready. In this simple example, I just want to pass a name from my applicationclient to my bean and my bean has to echo Helllo name

Add bellow code in method onMessage(Message message)


try { System.out.println("Hello " + message.getStringProperty("name")); } catch (JMSException ex) { Logger.getLogger(HelloBean.class.getName()).log(Level.SEVERE, null, ex); }

If the file is showing error add required packages that are


import java.util.logging.Level; import java.util.logging.Logger; import javax.jms.JMSException;

The full code looks like this:

package hello; import import import import import import import java.util.logging.Level; java.util.logging.Logger; javax.ejb.ActivationConfigProperty; javax.ejb.MessageDriven; javax.jms.JMSException; javax.jms.Message; javax.jms.MessageListener;

@MessageDriven(mappedName = "jms/hello", activationConfig = { @ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge"), @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue") }) public class HelloBean implements MessageListener { public HelloBean() { } public void onMessage(Message message) { try { System.out.println("Hello " + message.getStringProperty("name")); } catch (JMSException ex) { Logger.getLogger(HelloBean.class.getName()).log(Level.SEVERE, null, ex); } } }

Creating the Application client


The MDB is created and is waiting for messages. Now its time to create the Application client. Just create a new project and choose for an Enterprise Applicaton Client from the list

Then right click on Mani.java file select insert code then select

As soon as u select Send JMS Message some code will be added as shown below

Add the below code that is highlighted in yellow color in main function The code for the applicaton client looks like this:
package mdbclient; import import import import import import import import javax.annotation.Resource; javax.jms.Connection; javax.jms.ConnectionFactory; javax.jms.JMSException; javax.jms.Message; javax.jms.MessageProducer; javax.jms.Queue; javax.jms.Session;

public class Main { @Resource(mappedName = "jms/helloFactory") private static ConnectionFactory helloFactory; @Resource(mappedName = "jms/hello") private static Queue hello; public static void main(String[] args) throws JMSException { Connection connection = helloFactory.createConnection(); Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); MessageProducer producer = session.createProducer(hello); Message message = session.createTextMessage(); message.setStringProperty("name", "World!!");

producer.send(message); System.exit(0); } }

If you deploy and run this application you should see in the server output the string Hello World!!

Steps for creating Entity Bean Create Database 1.Go to services under databases right click on link click on connect jdbc:derby://localhost:1527/sample [app on APP] 2.Then expand select APP & expand APP 3.Select tablesCreate tables

4.Give Table Name as EMP

5.Add columns name as id select type integer select primary key click ok 6.Add column name as name, select type as varchar, give size as 24, click ok 6.Insert 2 rows to the table Creating the Enterprise Application
In this section you will use the New Project wizard to create an enterprise application containing an EJB module. The wizard provides an option to create an EJB module when you create the application. 1. Choose File > New Project and select Enterprise Application in the Java EE category. Click Next. 2. Type EntityAppEJB for the Project Name. Click Next. 3. Select GlassFish Server 3 for the Server. 4. Confirm that Create EJB Module & Create Web Application is selected.Click Finish.

Right click on EJB file select New->Entity Class for Database

6.From Datasource drop down Select JDBC/sample You will get list of tables in Available tables add EMP table

Give package name as entityEjb->Rest leave default selection. 7.Click on Next & Finish 8.Right click on EJB file-select session bean

9.Type EJB Name as EntitySessionBean Give package name as Entityejb Select stateless , local & Remote both. In remote Drop down List select entityClassLib Click on finish

Add persistent object Right on class file select use Entity Manager as shown below

Add business Method 1.Right click on classselect add business methodgive method name as empAdd() 2.Leave return type as void. 3.Add 2 parametersBy clicking on add button 4.Type id in name select int in type do not select final 5.Type ename in name select String in type do not select final 6.In use in interface select both Then click ok 7. add below code in empAdd method Emp1 e1=new Emp1(); e1.setId(id); e1.setName(name); persist(e1);

Deploy the application by right clicking on EntityAppEJB

Create client for Entity Bean Goto EntityAppEJB war file select new add servlet file.

Refer Entity bean By right clickselect call enterprise beanselect remote as shown in below program

1.In try method add below code newSessionBean.addEmp(Integer.parseInt(request.getParamet er("id")),request.getParameter("name")); 2.In body tag add Below tag out.println("<h1>Database Updated</h1>");

Go to index page under web pages /Web INF-

Add below code in body tag as shown in the above figure


<form action="EntityDemo" method="get"> <h1> Servlet EJB Session Entity Bean </h1> EMP ID : <input type="text" size="3" name="id" > <br> EMP NAME: <input type="text" size="15" name="name"> <br> <input type="submit" name="post" value="post"> <input type="reset" name="clear" value="clear"> </form>

Run the application Right click on applicationin select run as shown in the below figure

In Browser enter input for id & name

Click on post button You will see Database updated message in the browser Go to services tab Unser database Go to Then expand select APP & expand APP 3.Select table right view table Can see one row is added to the table as shown in the below fig.

You might also like