Injecting JBoss EJB3 beans into Tomcat6

Injecting EJB's into a managed bean (a bean spawned by appserver, of a JSF managed bean) inside an application server is simple:
public class AppManagedBean
{
   @EJB(mappedName="ejb/myBean")
   private MyBeanRemote session;
The container looks up the mapped bean and injects it. Easy.

However in tomcat it gets a little messy. Tomcat doesn't document how to inject them. It assumes something annotated with @EJB is in the context somewhere and tries to look it up.
It has documented how to lookup ejb's by defining entries in your context.xml or server.xml. But there isn't anything documented for defining EJB's. Looking at the soure it has support for <Ejb/> references but no real clue on how to configure it. So after stepping through the code I came up with the following soloution:

My environment is JBoss 5 running remotely (on localhost). A seperate instance of Tomcat 6. In it I have defined a session bean:
@Stateful(mappedName="userSession")
public class UserSessionBean implements UserSessionRemote 
This bean is used by a JSF backing bean (i.e. a managed bean):
public class TomcatManagedBean
{
   @EJB(name="ejb/myBean")
   private MyBeanRemote session;
In order for this bean to bound to the context, you need to configure Tomcat. There are 2 ways: in the server.xml or in the context.xml
I find that putting it in META-INF/context.xml is the easiest, as it gets reloaded nicely in Eclipse. The name="ejb/myBean" corresponds to the name attribute in the <Ejb/> below. In the URL is the appserver name, port, then the mapped name (it could be myProject/ejb/bean or whatever you feel like) - as long as it matched the mappedName on the EJB.
<Context>
   <!-- Makes Tomcat manage the ejb for you. -->
   <Ejb name="ejb/myBean" type="Session" factory="org.jnp.interfaces.NamingContextFactory" URL="jnp://localhost:1099/userSession" />
</Context>
Update 2010/02/02: Correct the mappedName in AppManagedBean

Comments

  1. Hi, I am currently trying to do the same thing like you. I have a Webapp runing on Tomcat 6.0.20 and an EJB 3.0 on JBoss 4.3.
    But it is absolutly not workling. I am trying to inject my EJB in Tomcat but it doesn't work.

    If you like to help me, and send me your email-adress to gfemajor2[at]gmx.de i can send you my 2 test projects. (I will not post my hole code here, because i think it is to mutch for a comment ;-)). So that you can have a look at it and tell me what am i doing wrong.



    Thank you so far

    Christian

    ReplyDelete
  2. I just noticed I had a mapped name slightly wrong above which I've corrected - sorry if that caused some confusion.

    I've also got an alternate way of mapping it (in which I've had to edit the tomcat source). Tomcat - load & bind an external naming context

    I'll drop you an email.

    ReplyDelete
  3. Hi Chris,
    thank you for your article, it contains the information I am looking for, but I have a problem left.
    As soon as I am accessing the injected EJB I get an NullPointerException, I think it is because I don't import the right libraries.
    This are the libraries I import, it would be nice, if you could take a look, if they are complete:
    groupId: javax.ejb
    artefactId: ejb-api
    version: 3.0
    and
    groupId:javax.persistence
    artefactId: persistence-api
    version: 1.0

    All the best,
    Sebastian

    ReplyDelete
  4. I've just added a new post describing how I build my client jars. I'm assuming your using it in a webapp (like tomcat) outside of the app server (e.g. jboss)?

    If your inside the appserver you don't need to include the persistence-api or ejb-api.
    And for outside the appserver but inside a web server you would have all those jars inside the server's shared directory (e.g. tomcat/lib). Just make the dependencies <scope>provided</scope>

    ReplyDelete
  5. Hi Chris,
    thank you for your help :)
    your right the frontend is running in a ServletContainer (Tomcat 6.0.18) and the backend in an ApplicationServer (JBoss 5.1.0.GA).
    I changed the libraries accoding to your description (I put the client jars into the lib directory of the Tomcat and I made the dependencies provided), but it won't work.
    Now I am thinking, that there might be something missing in my web.xml? It just contains the follwoing part:
    <servlet>
    <display-name>FacesServlet</display-name>
    <servlet-name>FacesServlet</servlet-name>
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
    <servlet-name>FacesServlet</servlet-name>
    <url-pattern>/faces/*</url-pattern>
    </servlet-mapping>
    Do you know if I need something more?

    ReplyDelete
  6. No, I was wrong it truly worked :) *Thank you very much* :)
    But I have one Exception left:
    Caused by: java.lang.IllegalArgumentException: Can not set com.backend.service.UserServiceRemote field com.frontend.action.LoginBean.userService to org.jnp.interfaces.NamingContext
    at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:146)
    at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:150)
    at sun.reflect.UnsafeObjectFieldAccessorImpl.set(UnsafeObjectFieldAccessorImpl.java:63)
    at java.lang.reflect.Field.set(Field.java:657)
    at org.apache.catalina.util.DefaultAnnotationProcessor.lookupFieldResource(DefaultAnnotationProcessor.java:213)
    at org.apache.catalina.util.DefaultAnnotationProcessor.processAnnotations(DefaultAnnotationProcessor.java:142)
    at com.sun.faces.vendor.Tomcat6InjectionProvider.inject(Tomcat6InjectionProvider.java:82)
    ... 38 more

    Do you have a clue?

    ReplyDelete
  7. Hi Chris :)

    I could fix the last problem :) Now it is working! (I fixed it by removing the attribute "mappedName" in the SessionBean) Thanks again.

    All the best,
    Sebastian

    ReplyDelete
  8. Hi Sebastian,

    No worries.
    I was going to say that the problem you having was probably related to what it was trying to resolve actually resolved to a directory not the bean.

    Looking at my example its slightly wrong:
    It should be @EJB(mappedName="ejb/myBean")
    That matches the line in context.xml name="ejb/myBean". But removing the mapped name causes it to look up by class interface which is why it works.

    -Chris

    ReplyDelete
  9. I just want these notifications to stop for myself.
    บาคาร่า
    จีคลับ
    gclub

    ReplyDelete
  10. เกมส์ pussy888ทุกแบบ สำหรับทุกคน
    เล่นเกมส์การพนันออนไลน์ แหล่งสะสมเกมส์การพนันคาสิโนสล็อต สำหรับทุกคนที่ชอบด้านการเล่นเกมส์การพนันออนไลน์ ได้เข้ามาร่วมบันเทิงใจกับเกมการพนัน แล้วก็ทำเงินจากการเล่นเกมส์การพนันกับเว็บของพวกเราได้อย่างง่ายๆ pussy888 เว็บที่สะสมเกมส์การพนันออนไลน์ทุกแบบอย่างให้กับทุกคนที่ชื่นชอบในด้านการเล่นเกมส์การพนัน เว็บ pussy888ที่เก็บรวบรวมเกมส์การพนัน pussy888ที่มีคุณภาพ เล่นแล้วได้เงินจริง พร้อมบริการกับทุกคนที่เข้ามาร่วมเล่นเกมส์การพนันกับเว็บของพวกเรา บันเทิงใจกับเกมส์การพนันได้อย่างไม่ยากเย็นทำเงินจากการเล่นเกมส์การพนัน pussy888
    ร่วมเล่นเกมส์การพนันกับเว็บของพวกเราได้เลยเดี๋ยวนี้ pussy888
    ร่วมเล่นเกมส์การพนันกับเว็บของพวกเรา เล่นเกมส์การพนันกับผู้ให้บริการเกมการพนันที่เหมาะสมที่สุด เล่นเกมส์การพนันกับเว็บคาสิโนออนไลน์ที่มีคุณภาพ pussy888 ลือชื่ออยู่แล้วในเรื่องเกี่ยวกับการให้บริการเกมส์การพนันคาสิโนออนไลน์ ขึ้นชื่อลือชาอยู่แล้วในเรื่องเกี่ยวกับการให้บริการเกมการพนันที่มีคุณภาพ มีเกมส์การพนันคาสิโนออนไลน์ทุกต้นแบบ ที่พร้อมจะบริการให้กับทุกคนที่เข้ามาร่วมเล่นเกมส์การพนัน
    สรวงสวรรค์ของผู้ที่ชอบใจการเล่นเกมส์การพนัน pussy888 พวกเราเก็บเกมส์การพนัน pussy888ทุกต้นแบบมาเปิดให้บริการกับลูกค้าทุกคนที่เข้ามาร่วมเล่นเกมส์การพนันกับเว็บของพวกเราทั้งผองแล้ว ด้วยเหตุนั้น สมัครร่วมเล่นเกมส์การพนันกับเว็บของพวกเรา เล่นเกมส์การพนันกับผู้ให้บริการเกมการพนันที่เหมาะสมที่สุดในช่วงเวลานี้ เล่นเกมส์การพนันกับเว็บคาสิโนออนไลน์ที่มีคุณภาพ pussy888 พร้อมบริการลูกค้าทุกคนที่เข้ามาร่วมเล่นเกมส์การพนัน
    เกมส์การพนันคาสิโนออนไลน์เล่นง่าย pussy888
    พุซซี่ 888 พวกเราลือชื่ออยู่แล้วนะเรื่องของเทคโนโลยีเกมส์การพนันคาสิโนออนไลน์ รวมทั้งพวกเราขึ้นชื่อลือชาอยู่แล้วในเรื่องของเว็บที่ถูกดีไซน์มาให้ใช้งานได้ง่าย ทุกคนที่เข้ามาร่วมเล่นเกมส์การพนันกับเว็บของพวกเรา จะได้เล่นเกมส์การพนันคาสิโนออนไลน์มาก ทุกคนที่เข้ามาร่วมเล่นเกมส์การพนันกับเว็บของพวกเรา จะสามารถทำเงินจากการเล่นเกมส์การพนันคาสิโนออนไลน์ pussy888 ศูนย์สะสมเกมส์การพนัน pussy888ทุกแบบอย่าง แหล่งสะสมเกมส์การพนันสำหรับทุกคนที่พอใจต้องการจะเข้ามาร่วมเล่นเกมส์การพนันได้เข้ามาร่วมเล่นเกมส์การพนันกับเว็บของพวกเรา
    พวกเราพร้อมบริการลูกค้าทุกคนที่เข้ามาร่วมเล่นเกมส์การพนัน พวกเราพร้อมมอบประสบการณ์ที่เยี่ยมที่สุดสำหรับทุกคนที่ต้องการจะสร้างอาชีพเสริม กำลังมองหาหนทางวิธีการทำเงินให้กับตนเอง เข้ามาร่วมเล่นเกมส์การพนันกับเว็บของพวกเราได้เลยในช่วงเวลานี้ เกมส์การพนันคาสิโนออนไลน์ที่เยี่ยมที่สุด เกมส์การพนันคาสิโนออนไลน์ ศูนย์เก็บเกมส์การพนัน ซึ่งสามารถร่วมเล่นเกมส์การพนันผ่านโทรศัพท์เคลื่อนที่ได้
    pussy888

    ReplyDelete
  11. สล็อต pussy888 เกมคาสิโนออนไลน์ที่คุณไม่สมควรละเลย
    การเล่นเกมคาสิโนออนไลน์ในทุกวันนี้มีให้ผู้เล่นเลือกเล่นมากมายแบบเกมทั้งยังเกมบาคาร่า รูเล็ต เกมไพ่ เกมยิงปปลา หรือเกมสล็อตออนไลน์ ซึ่งนับว่าเป็นเกมคาสิโนออนไลน์ยอดนิยม และก็เป็นที่กล่าวถึงของเหล่าคนถูกใจเล่นเกมค้างสินออนไลน์เป็นอย่างยิ่งโดยในวันนี้พวกเราจะมาเอ๋ยถึงการเล่นเกมสล็อต จากค่ายเกมpussy888 ที่นับว่าเป็นค่ายเกมในดวงใจของคนอีกจำนวนไม่น้อยอย่างยิ่งจริงๆ เนื่องจากว่าด้วยต้นแบบเกมในค่ายที่มีความน่าดึงดูดใจ น่าเล่นอย่าเกมสล็อต เป็นเกมที่ซึ่งพูดได้ว่า ได้รับความนิยมติดลมบน จนถึงคนใดที่ไม่เคยเล่นเกมนี้จัดว่าพลาดมากมายทีเดียว ตกลงว่าแม้คุณพอใจ รวมทั้งอยากมากระเป๋านส่วนหหนึ่งในการเล่นเกมคาสิโนสล็อตpussy888 วันนี้พวกเราจะพาทุกคนไปดูกันว่า เกมคาสิโนออนไลน์นี้ ดีมากยิ่งกว่าดเกมคาสิโนแบบอื่นๆเช่นไร
    เกมคาสิโนออนไลน์ pussy888
    เกมคาสิโนออนไลน์ pussy888 แนเกมยอดนิยม รวมทั้งเป็นที่เอ่ยถึงเป็นอย่างมากในปี 2020 นี้ ด้วยแบบการเล่นเกมเอง แบบอย่างเกม ความสนุกสนานร่าเริง แสงสว่าง สีเสียงด้านในเกมที่ช่วยทำให้ผู้เล่นได้รับความสนุกสนาน ความระทึกใจไปกับทุกๆการพนัน ก็เลยมีบุคคลที่เข้ามาเล่นเกมสล็อตpussy888 กันอ่างตลอด ทั้งเกมสล็อตpussy888นี้ยังเป็นเกมที่เล่นง่าย ผู้เล่นสามารถทำเงินจากเกมคาสิโนออนไลน์ลักษณะนี้ได้เร็วกว่ากมคาสิโนอื่นๆก็เลยไม่ต้องฉงนใจเลยว่า เพราะอะไรขณะนี้การเล่นเกมสล็อต pussy888 ถึงแปลงเป็นที่ชอบใจของเหล่านักเล่นเกมคาสิโนออนไลน์กันเพิ่มขึ้นเรื่อยๆวันแล้ววันเล่า
    รวมทั้งในวันนี้แม้คุณพึงพอใจที่จะทดลองเข้ามาร่วมสนุกสนาน แล้วก็ตื่นเต้น ไปกับการเล่นเกมคาสิโนสล็อตpussy888 พวกเรามีวิธีการการเล่นเกมสล็อต ที่สามารถช่วยให้ผู้เล่นได้โอกาสสำหรับเพื่อการทำเงินจากเกมคาสิโนนี้ได้ง่ายมากยิ่งขึ้นกว่าเดิมมาฝาก
    เล่นเกมคาสิโนสล็อตpussy888 อย่างมีสติสัมปชัญญะ รวมทั้งคิดพินิจพิจารณาให้ดีทุกคราวก่อนจะเข้ามาสมัครเล่นเกมคาสิโนออนไลน์ เท่านั้นไม่พอ การเล่นเกมติดอยู่สิดนออนไลน์ของคุณจำเป็นต้องแตกต่าจากผูอื่นเพื่อเป็นการเพิ่มช่องทางสำหรับในการทำเงิน ยกตัวอย่างเช่น พนันคูณ2 เพื่อเพิ่มช่องทางสำหรับในการทำเงินเพิ่มขึ้นเรื่อยๆจากการชนะเกมคาสิโนที่เสียไป
    ไม่ยืม กู้ หรือนำเงินของผู้อื่นมาลงทุนเพื่อเล่นเกมคาสิโนออนไลน์ ด้วยเหตุว่ามันอาจทำให้ผู้เล่นมีปัญหาด้านการเงินตามมาได้ เมื่อเล่นเกมแพ้
    วิธีการเล่นเกมสล็อต
    pussy888 นี้ยังมีอีกหลายเคล็ดลับร่วมกัน ซึ่งถ้าหากผู้เล่นพอใจ ก้สามารถข้ามาเรียนรู้วิธีการเล่นเกมสล็อตพอดีเว็บไซต์ของพวกเราตลอด 24 ชั่วโมง ที่สำคัญ เว็บไซต์พวกเรามีโปรโมชั่นเด็ดรอคอยคุณอยู่ด้วย

    ReplyDelete
  12. Very Helpful Articles. lady hawk It might help you. lady hawk Thanks For Sharing
    lady hawk Thank you very much.

    ReplyDelete
  13. This Is Really Useful And Nices Information. lady hawk
    This are such great articles. lady hawk This articles can help you to make some new ideas.
    https://soccersurfer98.hatenablog.com/entry/2020/09/02/132139?_ga=2.193217001.552343305.1598844608-1286484823.1596077192 I appreciate for reading my blogs.

    ReplyDelete

Post a Comment