Arch2Arch Tab BEA.com
Syndicate this blog (XML)

Reminder on how to use EJB3 with Weblogic 10

Bookmark Blog Post

del.icio.us del.icio.us
Digg Digg
DZone DZone
Furl Furl
Reddit Reddit

Maxence Button's Blog | April 3, 2008   2:18 AM | Comments (18)


First of all, I'd like to point out the excellent tutorial by Greg Mally on using EJB3 within Workshop :

http://dev2dev.bea.com/blog/gmally/archive/2007/11/using_workshop.html

 

How to define an EJB3 ?

Well, it's quite easy. According to the specification, you only have to use annotations to define your EJB.

For instance, let's take the following EJB :

@Stateless(
	name = "PdfRetriever", 
	mappedName = "ejb/stateless/PdfRetriever", 
	description = "Gets a PDF file from the server")
@Local ({PdfRetriever.class})
@Remote ({PdfRetrieverRemote.class})
public class PdfRetrieverBean implements 
			PdfRetriever, PdfRetrieverRemote {
[...]
}

For specifying the interfaces (local or remote), you can choose to use only the annotations or the implementation declarations.

The use of annotations is maybe a bit quicker to read, but the implement declaration prevents you from missing an interface method. I wrote both so that you can make your choice.

As for the deployment descriptor, the ejb-jar.xml must have the correct declaration, that is to say :

<ejb-jar xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/ejb-jar_3_0.xsd"
version="3.0">

 

How to call it with remote interface ?

 

 image

Here's the code to call the EJB :

Properties props = new Properties();
props.put(Context.INITIAL_CONTEXT_FACTORY,
	"weblogic.jndi.WLInitialContextFactory");
props.put(Context.PROVIDER_URL, 
	"t3://localhost:8001");
InitialContext ctx = new InitialContext(props);
PdfRetrieverRemote ejb = (PdfRetrieverRemote) 
	ctx.lookup("ejb.stateless.PdfRetriever#sample.ejb.PdfRetrieverRemote");
ejb.getPdfFileFromServer("myFile.pdf");

Be aware that the JNDI name is not exactly the same as entered in the EJB definition ("ejb/stateless/PdfRetriever").

It has been postfixed with a # and the full-qualified name of the Remote interface.

 

How to call it with local interface ?

Note : "Local" doesn't refer to the same JVM but the same EAR.

The easiest way to call your EJB is through a Servlet thanks to an annotation.

As explained above, your servlet must reside in the same EAR than the EJB3 module.

 

image

You don't have to use JNDI to reach your EJB, just reference it that way :

@EJB
private PdfRetriever pdfRetriever;

public void myMethod () {
    byte[] myPdfFile = pdfRetriever.getPdfFileFromServer("myFile.pdf");
}

Note : Due to lots of messages asking me how to call an EJB3 from a pojo, I will write some details about that, but know that this is not the way it is sensed to be used.

According to the specification, an EJB3 is to be called via an annotation in a Servlet or another EJB.

This solution lies on the "java:comp/env" mechanism.

EJB within a web application

First part : declaring the EJB

Taking the example quoted above, you will have to add an annotation to at least one servlet.

Example :

@EJB(beanInterface=sample.ejb.PdfRetriever.class, name = "ejb/PdfRetriever")
  public class EjbInvoker extends javax.servlet.http.HttpServlet implements         javax.servlet.Servlet {

This annotation replaces the EJB declaration in the web.xml file. It makes the EJB available for all the War context.

Else you can declare your EJB in the web.xml :

<ejb-local-ref>
  <ejb-ref-name>ejb/PdfRetriever</ejb-ref-name>
  <local>sample.ejb.PdfRetriever</local>
</ejb-local-ref>

http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd

As you can see, the attributes are different :

Deployment descriptor Annotation
local beanInterface
ejb-ref-name name

Note : With the annotation, the beanInterface attribute expects a class.

Second part : calling the EJB from the POJO

Now the EJB has been made visible within the whole web application, the code to call the EJB from the POJO is quite simple :

InitialContext ctx = new InitialContext(); 
PdfRetriever ejb = (PdfRetriever) ctx.lookup("java:comp/env/ejb/PdfRetriever"); 

Note : The lookup has been made on the name specified in "name" attribute for the annotation and "ejb-ref-name" for the DD, that is to say "ejb/PdfRetriever"

You can then manipulate your local EJB.

EJB within an EJB module

The scenario is the same than for the web application, except that the element you've got to add the EJB declaration to is now an EJB and the DD to modify is not the web.xml but the ejb-jar.xml

 


Comments

Comments are listed in date ascending order (oldest first) | Post Comment

  • Hi Thomas,

    As I wrote a few days earlier, if you're looking for some precise classes, use JarClassFinder, : it's an excellent tool provided by IBM for Eclipse-based IDEs.

    It will help you to find all the interfaces and implementation classes you're looking for.

    Regards.

    Posted by: mbutton on May 11, 2008 at 3:04 PM

  • Hi, I am going to build projects of EJB3 qand run it on Weblogic. But where can I get libraries (such as javax.persistence.Entity) of EJB3 for Weblogic?

    Posted by: thomas2004ch on May 11, 2008 at 1:00 PM

  • Hi Kakoni,

    If you want to use your EJB3 within a POJO, you should use :

    • either the annotation (which will play in the end the same role as a definition in DD)
    • or directly the declaration in the DD.
  • Since you're not mixing both methods, it shouldn't be confusing.

    Hope it helps. Regards.

    Posted by: mbutton on May 6, 2008 at 8:40 AM

  • Hi! Scenario; I have a pojo inside ejb module/package. So I use that in my ejb-jar.xml to define properties for the ejb3 to be called? How would this work? I mean using both annotations and ejb-jar.xml seems kinda confusing?

    Posted by: kakoni on May 6, 2008 at 7:10 AM

  • Hi thielj, First of all, what I saw in your example is that you're trying to reach the remote interface from a local context. It should work but it's cleaner if you make a clear reference to your local interface. (try to think about the day you'll have different methods exposed !) Also note that the "mappedName" atribute is only to be used for defining the JNDI mapping. Since you want to use the "java:comp/env" mechanism, there's no need for it and it will make WLS confused. Then, in a word, for your use case to work, remove the 'mappedName' attribute and if you still have got some errors, double check your attributes. Don't hesitate to change them to be clearer. Should work, let me know. Regards.

    Posted by: mbutton on May 6, 2008 at 1:29 AM

  • Goodness, sorry about the unformatted post. I admit, my last post was vague. Here is what I'm doing. WLS 10. Have Struts 1.1.

    I created an EJB w/ signature:
    @Session(maxBeansInFreePool = "100", initialBeansInFreePool = "0", transTimeoutSeconds = "3600", type = Session.SessionType.STATELESS, defaultTransaction = Constants.TransactionAttribute.REQUIRED, ejbName = "ejb/CommentManager", enableCallByReference = Constants.Bool.TRUE)

    @Stateless(name = "ejb/CommentManager", mappedName = "ejb.CommentManager")

    public class CommentManagerBean implements CommentManagerInterface {

    I created EJB Interface w/ signature:

    @Remote public interface CommentManagerInterface {

    I added this to a struts action servlet:

    public class CCActionServlet extends ActionServlet {
    @EJB(beanInterface = CommentManagerInterface.class, mappedName = "CommentManager", name = "ejb.CommentManager")
    protected CommentManagerInterface commentManager;

    I added this to a struts action to obtain the bean:

    final InitialContext ctx = new InitialContext();
    ejb = (CommentManagerInterface) ctx.lookup("java:comp/env/CommentManager");

    Here is the exception that i get: javax.naming.NameNotFoundException: While trying to look up comp/env/ejb/CommentManager in /app/webapp/eng/9532759.; remaining name 'comp/env/ejb/CommentManager'

    Using debugger, i examined the context and have found that there doesn't appear to bea and "ejb" entry in it. there is a jmx for something else, but no ejb. I wonder if the injection on the struts servlet is working? Am I missing something? I'm assuming that injecting the bean into the struts action servlet will allow it to be accessed in the struts action. Is that erroneous thinking?

    We are replacing legacy ejb2 w/ ejb3. should this work along side the old code? Some are suggesting that we need to wholesale replace all ejb2 w/ ejb3 in order to get the first ejb3 working. Not sure I buy that.

    Thanks in advance for any advice!

    Posted by: thielj on May 5, 2008 at 6:46 AM

  • I admit, my last post was vague. Here is what I'm doing. WLS 10. Have Struts 1.1. I created an EJB w/ signature: @Session(maxBeansInFreePool = "100", initialBeansInFreePool = "0", transTimeoutSeconds = "3600", type = Session.SessionType.STATELESS, defaultTransaction = Constants.TransactionAttribute.REQUIRED, ejbName = "ejb/CommentManager", enableCallByReference = Constants.Bool.TRUE) @Stateless(name = "ejb/CommentManager", mappedName = "ejb.CommentManager") public class CommentManagerBean implements CommentManagerInterface { I created EJB Interface w/ signature: @Remote public interface CommentManagerInterface { I added this to a struts action servlet: public class CCActionServlet extends ActionServlet { @EJB(beanInterface = CommentManagerInterface.class, mappedName = "CommentManager", name = "ejb.CommentManager") protected CommentManagerInterface commentManager; I added this to a struts action to obtain the bean: final InitialContext ctx = new InitialContext(); ejb = (CommentManagerInterface) ctx.lookup("java:comp/env/CommentManager"); Here is the exception that i get: javax.naming.NameNotFoundException: While trying to look up comp/env/ejb/CommentManager in /app/webapp/eng/9532759.; remaining name 'comp/env/ejb/CommentManager' Using debugger, i examined the context and have found that there doesn't appear to bea and "ejb" entry in it. there is a jmx for something else, but no ejb. I wonder if the injection on the struts servlet is working? Am I missing something? I'm assuming that injecting the bean into the struts action servlet will allow it to be accessed in the struts action. Is that erroneous thinking? We are replacing legacy ejb2 w/ ejb3. should this work along side the old code? Some are suggesting that we need to wholesale replace all ejb2 w/ ejb3 in order to get the first ejb3 working. Not sure I buy that. Thanks in advance for any advice!

    Posted by: thielj on May 5, 2008 at 6:42 AM

  • Has anybody used EJB 3 with a Struts 1.1 client? Any known examples?

    Posted by: thielj on May 2, 2008 at 1:07 PM

  • Hi Jibin, The class "weblogic/descriptor/DescriptorBean" you're looking for is located in two jars :
    • "BEA_HOME/wlserver_10.0/server/lib/wls-api.jar"
    • "BEA_HOME/modules/com.bea.core.descriptor_1.0.1.0.jar"
    Anyway, as it's better to learn to a man how to fish rather than giving him fish, I guess you should try JarClassFinder, which is an excellent tool provided by IBM for Eclipse-based IDEs. Hope it helps. Regards.

    Posted by: mbutton on April 24, 2008 at 1:22 AM

  • Hi, Can some one tell me which jar file in weblogic 10 supports the jwsc task in Ant. I am getting the following error while executing the build.xml java.lang.NoClassDefFoundError: weblogic/descriptor/DescriptorBean and am not able to find out which jar file it refers to... I have added weblogic.jar, wseeclient.jar and webservices.jar (belongs to weblogic 10 version) in my classpath. This is not resolving my issue. I dont have my server deployed on my machine. Any help will be appreciated.. Thanks in Advance Jibin

    Posted by: jgeo on April 23, 2008 at 2:07 PM

  • Dchakraborty, I added some content for calling an EJB3 from a POJO. That should help you get rid of that annoying NPE ... Hope it helps ! Regards.

    Posted by: mbutton on April 9, 2008 at 8:30 AM

  • Hey, Thanks for the quick reply. Here is what I am trying to do, I have all my EJBs in 1 jar and I have the invoking Delegate in the other. As per example I have modified the ejb-jar.xml to the correct ejb3 entries as show in the example. and I am using @EJB annotation in the delegate to look up the EJB. But I keep getting null pointer exception. But in weblogic console I do see the EJBs getting deployed. My delegate and EJBs are in separate jar files inside same ear file. Is there any thing I am missing?

    Posted by: dchakraborty on April 7, 2008 at 9:35 AM

  • Dchakraborty, the example above does not need any definition in the deployment descriptors : you just have to define that the version of the EJB specification you're using is the "3.0" else, annotations will be processed by the container and do the job. Harikrishnan, if I got it well, you're trying to call a local EJB from a POJO and you're telling me that the dependency injection works fine. Then, why do you want to perform a JNDI lookup ? Anyway, have you tried to use the "java:comp/env" mechanism ? Regards.

    Posted by: mbutton on April 7, 2008 at 9:20 AM

  • Hi, Can someone tell me how we can deploy EJB3.0 in weblogic 10 without using the deployment descriptors as mentioned in the EJb specs? Can I deploy the same example listed above without deployment descriptors? If so is there any added configuration that I need to do?

    Posted by: dchakraborty on April 7, 2008 at 7:33 AM

  • XML for EJB3.0 remote bean JPAServiceBean com.infosys.ua.ResourceManageMent.model.jpa.ejb.JPAService JPAService true

    Posted by: harikrishnan_pillai@infosys.com on April 6, 2008 at 10:54 PM

  • Hi Thanks for the help. But my problem is the following i have a local bean(EJB3.0) i have to look up the bean from a POJO class inside the container(same EAR)by using JNDI lookup.The Depedency injection is working but it is limited to EJB's only. so i need a sample XSD for configuring a LOCAL ejb 3.0 bean in weblogic-ejb-jar.xml . The one i have used for remote beans is as follows JPAServiceBean com.infosys.ua.ResourceManageMent.model.jpa.ejb.JPAService JPAService true But i am unbale to find the full structure for the above xml. Can you plese help me on this?

    Posted by: harikrishnan_pillai@infosys.com on April 6, 2008 at 10:52 PM

  • Hi, i am seraching for an XSD reference for weblogic 10 for ejb related configurations. Can any one post it on the group.

    Posted by: harikrishnan_pillai@infosys.com on April 3, 2008 at 11:58 PM



Only logged in users may post comments. Login Here.

Powered by
Movable Type 3.31