Saturday, July 27, 2013

Creating a Simple EJB SessionBean Using NetBeans & GlassFish Server


Hi , Today we are going to see how to create a simple EJB Stateless Session Bean using NetBeans and then we are going to deploy it in GlassFish Server and finally we will test it.

We are not going to discuss what is EJB what is Session Bean and how we have to use it etc.

So we require the following :

1.NetBeans ID
2. GlassFish Server.

Generally NetBeans comes along with GlassFish Server .While downloading NetBeans you can select this Option so that NetBeans Installer will download along with GlassFish Server.

Install the NetBeans with GlassFish Server.Once it is done we are ready to Go.

In this tutorial i am using the NetBeans 7.3 Version along with GlassFish Server 3.1.2 Version.

Now open NetBeans ID.

Step 1:

- Select : File->New Project ->Java EE - > EJB Module and click the "Next" Button.

- Give Project a Suitable Name Here i am giving "MyApp" click the "Next" Button.

- Select the Server as "GlassFish" with the version You have installed . Here i am selecting "GlassFish Server 3.1.2"

-  Select the J2EE Version what ever you have installed. Currently it is J2EE 6. and click the "Finish" Button.

You will have the Project Directory Structure something like this:















Step 2:

- Create a New Package called test under the Source Package Folder.

-  Create a new interface called "Home".under test package and add the following code.

 - This is a Remote Interface this can access from anywhere,once it is deployed in the server.

package test;
import javax.ejb.Remote;

@Remote
public interface Home {
public String sayHello(String name);
   }

Step 3:

Create a SessionBean class which implements this Interface .

RightClick on test and create New --> JavaClass--> HomeImpl

package test;

import javax.ejb.Stateless;
@Stateless
public class HomeImpl implements Home{

@Override
public String sayHello(String name) {
return "Welcome to EJB"+" "+name;
}

}

Step 4:

Create a GlassFish Descriptor File by

Righ Click on the Project "MyApp" and then choose New -->Other-->GlassFish-->GlassFish Descriptor and then click the "Next" button and click the "Finish " Button.

 glassfish-ejb-jar.xml file will be created under the Configuration Files Folder.

Set the JNDI Name for the HomeImpl Bean by editing the file or by clicking the TAB "EJB" and giving jndi name as "abc". 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE glassfish-ejb-jar PUBLIC "-//GlassFish.org//DTD GlassFish Application Server 3.1 EJB 3.1//EN" "http://glassfish.org/dtds/glassfish-ejb-jar_3_1-1.dtd">
<glassfish-ejb-jar>
<enterprise-beans>
<ejb>
<ejb-name>HomeImpl</ejb-name>
<jndi-name>abc</jndi-name>
<pass-by-reference>true</pass-by-reference>
</ejb>
</enterprise-beans>
</glassfish-ejb-jar>

Save the XML File.

Note : There can be only one glassfish-ejb-jar.xml file per EJB Module.

Step 5:

- Right click on the Project Node(MyApp)and select clean and Build.

- After clean and build again right click on the project node and select "Deploy".

For Both this Actions "BUILD SUCCESSFUL" Message will be displayed in the console output.Then every thing is fine and our EJB Session Bean  HomeImpl with JNDI Name "abc" is successfully Deployed.

Step 6:

RightClick on the Project Node (MyApp) and create new Class called "Main"

package test;

import java.util.Properties;
import javax.naming.InitialContext;
import javax.rmi.PortableRemoteObject;


public class Main {

  public static void main(String arg[])throws Exception
{
Properties p=new Properties();
InitialContext c=new InitialContext();
Object l=c.lookup("abc");
Home r=(Home)l;
System.out.println(r.sayHello("Ayaz"));
}

}

Run this class to check our EJB is deployed Sucessfully . It will give the following result in the output console.
run:
Welcome to EJB Ayaz
BUILD SUCCESSFUL (total time: 14 seconds)


So far we have created a Stateless EJB Session Bean and deployed it in the Server and checked it through the same EJB Module , this will work since we are accessing from EJB Container locally from our EJB Module .

To access this bean remotely from another Java Component such as Web Application or Java Swing Application or Java Webservice we require the Jar files created from the "HomeImpl" Session Bean just now we created  & Server specific jars to load the Bean.

In my next Post on EJB we will see how to do this...


         Next >>                                          Index

--------------------------------------------------------------------------------------------------------



Technology Blogs
blogs

54 comments:

  1. Easy to understand,,

    ReplyDelete
  2. really nice and easy to implement the bean Thank you

    ReplyDelete
  3. i can't able deploy the bean in server...
    i get this error msg....
    "
    deploy?DEFAULT=/home/jec/NetBeansProjects/ejb_simulation/build/classes&name=myapp&force=true failed on GlassFish Server 4.0
    Error occurred during deployment: Exception while deploying the app [myapp] : Referencing error: this bundle has no bean of name: Homeimpl. Please see server.log for more details.
    /home/jec/NetBeansProjects/ejb_simulation/nbproject/build-impl.xml:919: The module has not been deployed.
    See the server log for details.
    BUILD FAILED (total time: 0 seconds)

    ReplyDelete
  4. I get the answer after a long tragedy...............!

    ReplyDelete
  5. Thank you. Tutorial was very clear :) No errors encountered :D thanks man

    ReplyDelete
  6. Thanks Bro.. Keep It up this way with new post :)

    ReplyDelete
  7. Hi, I do think this is a great web site. I stumbledupon it ;) I will return yet
    again since I book marked it. Money and freedom is the best way to change, may you be rich and continue to help other people.


    Here is my web-site Dre Beats headphones

    ReplyDelete
  8. It's in point of fact a nice and helpful piece of info.
    I'm happy that you shared this helpful info with
    us. Please stay us informed like this. Thanks for sharing.


    Visit my page :: Dre Beats Headphones

    ReplyDelete
  9. Definitely believe that which you stated.
    Your favorite justification seemed to be on the web the
    simplest thing to be aware of. I say to you, I certainly get annoyed while people think about
    worries that they just do not know about.
    You managed to hit the nail upon the top and also defined out the whole thing without having side-effects , people can take a signal.
    Will likely be back to get more. Thanks

    Here is my page Beats By Dre UK

    ReplyDelete
  10. Definitely imagine that which you stated. Your favourite justification seemed to be at the web the
    simplest factor to be mindful of. I say to you, I certainly
    get irked even as folks consider concerns that they plainly do not realize about.
    You managed to hit the nail upon the highest and defined out the entire
    thing with no need side effect , people can take a signal.

    Will likely be back to get more. Thank you

    Also visit my blog post - Dr Dre Beats Headphones

    ReplyDelete
  11. Howdy just wanted to give you a quick heads up.
    The text in your content seem to be running off the screen in Opera.
    I'm not sure if this is a formatting issue or something to do
    with web browser compatibility but I thought I'd post
    to let you know. The design and style look great though!

    Hope you get the problem fixed soon. Cheers

    Also visit my web-site Beats By Dre Solo

    ReplyDelete
  12. Simply desire to say your article is as surprising.
    The clarity in your post is simply nice and i could assume
    you are an expert on this subject. Well with your permission allow me to grab your RSS feed to keep up to date with forthcoming post.
    Thanks a million and please carry on the gratifying work.

    Take a look at my website: dre beats headphones

    ReplyDelete
  13. Fine way of telling, and pleasant post to
    obtain information regarding my presentation focus, which i am going to deliver
    in university.

    Here is my web-site beats dre (Http://Sunplanet.bplaced.net)

    ReplyDelete
  14. I'll immediately seize your rss feed as I can not in finding your
    email subscription link or e-newsletter service.
    Do you have any? Please let me know so that I could subscribe.
    Thanks.

    Here is my site ... Cheap Dr Dre Beats

    ReplyDelete
  15. Wow, awesome blog layout! How long have you been blogging for?
    you make blogging look easy. The overall look of your web site is excellent, as well as the content!


    my weblog ... Dr Dre Beats

    ReplyDelete
  16. Oh my goodness! Impressive article dude! Thank you so much, However I am encountering difficulties
    with your RSS. I don't know why I am unable to join it.

    Is there anyone else getting the same RSS issues?
    Anybody who knows the solution will you kindly respond?
    Thanks!!

    Here is my webpage; Beats By Dre UK

    ReplyDelete
  17. I hardly write responses, however after browsing through a
    lot of remarks here "Creating a Simple EJB SessionBean Using NetBeans & GlassFish Server".

    I actually do have a couple of questions for you if it's allright.

    Is it only me or do a few of the comments appear like
    they are written by brain dead visitors? :-P And, if you are writing at additional sites, I'd like to keep up with everything fresh you have to post.
    Would you list of every one of all your shared pages like your twitter feed, Facebook page or linkedin profile?


    Here is my web blog: Christian Louboutin

    ReplyDelete
  18. This piece of writing will assist the internet viewers
    for creating new blog or even a weblog from start
    to end.

    Feel free to visit my web page Fake Louis Vuitton Bags

    ReplyDelete
  19. Yesterday, while I was at work, my sister stole my apple ipad and tested to see if it
    can survive a forty foot drop, just so she can be a youtube sensation. My apple ipad is now broken and she has 83 views.
    I know this is entirely off topic but I had to share it with someone!


    Also visit my homepage :: Dr Dre Beats USA

    ReplyDelete
  20. The other day, while I was at work, my cousin stole my
    iphone and tested to see if it can survive
    a forty foot drop, just so she can be a youtube
    sensation. My apple ipad is now destroyed and she has 83 views.
    I know this is entirely off topic but I had to share
    it with someone!

    Also visit my web blog; Beats By Dre Solo

    ReplyDelete
  21. An intriguing discussion is worth comment. I believe that you should publish more about this
    subject, it may not be a taboo matter but generally people do not discuss such topics.
    To the next! All the best!!

    Feel free to visit my site ... Dr Dre Beats

    ReplyDelete
  22. I like the valuable info you provide in your articles. I will bookmark your weblog and check again here frequently.

    I am quite certain I'll learn many new stuff right here!

    Best of luck for the next!

    my web blog - Beats By Dre Studio (freiheitundliebe.com)

    ReplyDelete
  23. Hello There. I discovered your weblog the use of msn. This is a really well written article.
    I'll make sure to bookmark it and return to learn extra of your useful info.
    Thank you for the post. I'll definitely comeback.

    Here is my blog ... Beats Headphones

    ReplyDelete
  24. Hi there just wanted to give you a quick heads up and let you know a few of the pictures
    aren't loading properly. I'm not sure why but I think its
    a linking issue. I've tried it in two different browsers and both show the same results.


    My website - Beats By Dre Cheap

    ReplyDelete
  25. Good day very nice blog!! Man .. Excellent .. Wonderful ..
    I'll bookmark your blog and take the feeds additionally?

    I'm happy to find a lot of helpful information here in the publish,
    we want develop more techniques in this regard, thanks for sharing.
    . . . . .

    Also visit my site: New Balance Discount

    ReplyDelete
  26. Hey I am so excited I found your blog, I really found you by
    accident, while I was searching on Bing for something
    else, Anyways I am here now and would just like to say thanks for a marvelous post and a all round exciting
    blog (I also love the theme/design), I don’t have time to go through it
    all at the minute but I have saved it and also included your RSS feeds, so when I have time I will be back to read a
    lot more, Please do keep up the great work.

    My web page: New Balance Shoes

    ReplyDelete
  27. Why viewers still use to read news papers when in this technological globe
    the whole thing is presented on web?

    Feel free to surf to my webpage - New Balance Shoe

    ReplyDelete
  28. Hey there! I've been following your website for a
    long time now and finally got the bravery to go ahead and give
    you a shout out from Lubbock Texas! Just wanted to
    mention keep up the good work!

    Take a look at my blog: Replica New Balance

    ReplyDelete
  29. Peculiar article, just what I needed.

    my blog; Beats By Dre USA

    ReplyDelete
  30. It's great that you are getting ideas from this post as well as from
    our discussion made here.

    Here is my web page - New Balance Online

    ReplyDelete
  31. I'm no longer certain where you are getting your info, but good topic.
    I must spend a while learning much more or figuring out more.
    Thank you for excellent information I used to be on the lookout for
    this information for my mission.

    my web blog - christian louboutin Canada

    ReplyDelete
  32. Hey there, You have done an incredible job. I'll definitely digg it and personally recommend to my friends.

    I am sure they will be benefited from this website.


    Here is my web blog Beats By Dre Solo (http://beats-australia.rr32.com)

    ReplyDelete
  33. Hello it's me, I am also visiting this web site daily, this website is actually
    fastidious and the viewers are in fact sharing fastidious
    thoughts.

    Here is my page; Cheap Louis Vuitton Bags

    ReplyDelete
  34. I loved as much as you will receive carried out right
    here. The sketch is attractive, your authored subject matter stylish.
    nonetheless, you command get got an nervousness over that you wish be delivering the following.
    unwell unquestionably come more formerly again as exactly
    the same nearly a lot often inside case you shield this increase.


    My blog post ... Christian Louboutin Sale

    ReplyDelete
  35. Hey there fantastic blog! Does running a blog such as this require
    a lot of work? I've virtually no understanding of
    coding but I had been hoping to start my own blog in the
    near future. Anyways, should you have any recommendations or tips for
    new blog owners please share. I understand this is off subject but I simply needed
    to ask. Kudos!

    Here is my website :: lacrim corleone album telecharger

    ReplyDelete
  36. always i used to read smaller articles or reviews which also clear
    their motive, and that is also happening with this post which I am reading here.


    Check out my web-site: New Balance Shoes

    ReplyDelete
  37. For newest information you have to go to see web and on web I found this
    website as a finest website for newest updates.


    Take a look at my weblog - lacrim corleone album telecharger

    ReplyDelete
  38. Hello There. I found your blog the usage of msn. This is
    a really smartly written article. I'll be sure to bookmark
    it and return to learn more of your useful information. Thank you for the post.
    I'll certainly return.

    Feel free to visit my web page :: Christian Louboutin Cheap

    ReplyDelete
  39. Hi there, just wanted to say, I loved this post.
    It was funny. Keep on posting!

    Have a look at my homepage; christian louboutin sale

    ReplyDelete
  40. Excellent post. Keep writing such kind of info on your site.
    Im really impressed by it.
    Hey there, You've done a great job. I'll definitely digg it and individually recommend to my friends.
    I'm sure they'll be benefited from this web site.

    My weblog ... New Balance

    ReplyDelete
  41. I think this is one of the most vital info for me. And i am glad
    reading your article. But should remark on few general things, The
    site style is great, the articles is really great : D. Good job, cheers

    Check out my webpage :: New Balance Outlet

    ReplyDelete
  42. When someone writes an article he/she maintains the thought of a user in his/her mind that how a user
    can understand it. So that's why this article is perfect.
    Thanks!

    Also visit my blog post: Dr Dre Beats Studio

    ReplyDelete
  43. If you are going for finest contents like me, only visit this web
    site daily as it provides quality contents, thanks

    Also visit my web-site :: Louis Vuitton Handbags

    ReplyDelete
  44. For the reason that the admin of this website is working, no
    hesitation very quickly it will be renowned, due to its quality contents.


    Here is my web-site - Christian Louboutin Canada

    ReplyDelete
  45. May I simply say what a relief to find an individual who truly knows what they are discussing over the internet.
    You definitely understand how to bring an issue to light
    and make it important. More and more people really need to
    check this out and understand this side of the story. I
    was surprised you are not more popular since you definitely have the gift.


    Also visit my weblog ... Christian Louboutin Shoes

    ReplyDelete
  46. Heya i'm for the first time here. I found this board and I to find It
    really useful & it helped me out a lot. I am hoping to give one thing back and aid
    others like you aided me.

    Also visit my blog; New Balance 574

    ReplyDelete
  47. Wonderful blog! Do you have any recommendations
    for aspiring writers? I'm planning to start my own site soon but I'm a little lost on everything.

    Would you advise starting with a free platform like Wordpress or go for
    a paid option? There are so many choices out there that I'm totally overwhelmed ..
    Any ideas? Cheers!

    my webpage :: Christian Louboutin Sale

    ReplyDelete
  48. Wow, marvelous weblog format! How long have you been blogging for?
    you made blogging glance easy. The whole glance of your
    site is wonderful, let alone the content material!


    Take a look at my site ... New Balance Outlet

    ReplyDelete
  49. WOW just what I was searching for. Came here by searching for restriction

    Also visit my blog Replica New Balance

    ReplyDelete
  50. Magnificent beat ! I would like to apprentice whilst you amend your web site, how can i subscribe for a weblog web site?
    The account helped me a appropriate deal. I had been tiny bit acquainted of this your broadcast provided shiny
    clear concept

    my web page ... Cheap Louis Vuitton Bags

    ReplyDelete
  51. Do you mind if I quote a few of your articles as long as I
    provide credit and sources back to your weblog?
    My website is in the exact same area of interest as yours and my visitors would genuinely benefit from a lot of the information you present here.
    Please let me know if this alright with you. Cheers!

    My web-site ... Louis Vuitton Discount

    ReplyDelete