{"id":84,"date":"2009-11-30T17:28:35","date_gmt":"2009-11-30T17:28:35","guid":{"rendered":"http:\/\/salientsoft.co.uk\/?p=84"},"modified":"2009-12-03T10:55:20","modified_gmt":"2009-12-03T10:55:20","slug":"accessing-local-and-remote-ejbs-in-glassfish","status":"publish","type":"post","link":"https:\/\/salientsoft.co.uk\/?p=84","title":{"rendered":"Accessing Local and Remote EJBs in Glassfish"},"content":{"rendered":"<p><a title=\"https:\/\/glassfish.dev.java.net\/javaee5\/ejb\/EJB_FAQ.html\" href=\"https:\/\/glassfish.dev.java.net\/javaee5\/ejb\/EJB_FAQ.html\">This Glassfish article<\/a> \u00a0is a must read reference on the naming and JNDI minefield when looking up local and remote EJBs.<br \/>\nThere\u00a0is also a very\u00a0 informative <a title=\"http:\/\/www.scribd.com\/doc\/7471016\/Refcardz-06-Dependency-Injection-in-EJB-3\" href=\"http:\/\/www.scribd.com\/doc\/7471016\/Refcardz-06-Dependency-Injection-in-EJB-3\">Refcardz card<\/a>\u00a0on this topic.\u00a0Refcardz can be downloaded as PDFs if\u00a0\u00a0you register<strong>.<br \/>\n<\/strong>In my experience, there is a lot of misinformation around the internet on this which can be time consuming to weed out.<\/p>\n<p>I&#8217;ve posted some examples of local and remote EJB access via both injection and JNDI <strong><a title=\"http:\/\/salientsoft.co.uk\/?p=123\" href=\"http:\/\/salientsoft.co.uk\/?p=123\">here<\/a><\/strong>.<\/p>\n<p>Here are some key points I worked through and learned :-<\/p>\n<ol>\n<li>Despite a number of blog posts to the contrary, EJBs can be injected via @EJB\u00a0into JSF managed beans, POJOs called from managed beans, and servlets\u00a0provided the JSF\/servlets are running in the\u00a0EJB container, e.g. if Glassfish is used both as servlet and EJB container.<\/li>\n<li>When injecting local EJBs, using the defaults on @EJB and matching defaults e.g. on @Stateless will work and the bean will inject.<\/li>\n<li>When injecting remote EJBs via @EJB, use the <strong>mappedName<\/strong> argument to specify the JNDI name of the remote bean. It is also essential to specify the class of the remote EJB interface in the <strong>beanInterface<\/strong> argument to @EJB. Failure to do this will result in NamingExceptions, which can give the misleading impression that it is the JNDI name that is wrong.<\/li>\n<li>If @EJB is not used, then &lt;ejb-local-ref&gt; may be used in web.xml to define a reference to the bean. If @EJB is used then this is handled automatically. The above Glassfish article details how the various fields in @EJB and &lt;ejb-local-ref&gt; are related.<\/li>\n<li>In addition to @EJB and direct JNDI lookups, EJBs can be injected via JSF using a custom EL resolver which performs the JNDI lookups and injections performed in faces-config.xml.\u00a0 An interesting example of a custom EL resolver to do this is <a title=\"http:\/\/www.manorrock.com\/products\/jsf\/usersguide\/jndi.html\" href=\"http:\/\/www.manorrock.com\/products\/jsf\/usersguide\/jndi.html\" target=\"_blank\">here<\/a>.\u00a0However, for injecting EJBs, \u00a0@EJB requires no extra code.\u00a0It is also possible to inject EJBs\u00a0via Spring, into beans managed by a Spring container. However these have not been pursued as @EJB is the easiest way. It would be overkill to use\u00a0a Spring container purely for EJB injection\/service location.<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>This Glassfish article \u00a0is a must read reference on the naming and JNDI minefield when looking up local and remote EJBs. There\u00a0is also a very\u00a0 informative Refcardz card\u00a0on this topic.\u00a0Refcardz can be downloaded as PDFs if\u00a0\u00a0you register. In my experience, there is a lot of misinformation around the internet on this which can be time [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[17],"tags":[18,10,19,16],"_links":{"self":[{"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/84"}],"collection":[{"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=84"}],"version-history":[{"count":10,"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/84\/revisions"}],"predecessor-version":[{"id":94,"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/84\/revisions\/94"}],"wp:attachment":[{"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=84"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=84"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=84"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}