Steps For Creating Session Bean: Creating The Java Class Library
Steps For Creating Session Bean: Creating The Java Class Library
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.
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.
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.
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.
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; } }
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.
In the Services window, if you expand the Applications node of GlassFish Server 3 you can see that EntAppEJB was deployed.
When you click Finish, the IDE creates the application client project and opens Main.java in the editor.
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()); }
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
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); } } }
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
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.
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);
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>");
Run the application Right click on applicationin select run as shown in the below figure
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.