Modular EJBs in OSGi <ul>Tim Ward IBM 21 Sep 2011 </ul><ul>OSGi Alliance Marketing © 2008-2010 . All Rights Reserved  </ul...
Legal <ul><ul><li>Java and all Java-based trademarks and logos are trademarks or registered trademarks of Oracle and/or it...
OSGi and the OSGi logo are trademarks or registered trademarks of the OSGi Alliance
Other product and service names might be trademarks of IBM or other companies. A current list of IBM trademarks is availab...
Agenda <ul><li>A Quick EJB refresher
How EJBs might fit into OSGi
A case study in implementing Modular EJB
Proof that it really works </li></ul>
Terminology <ul><li>EJB  –  Enterprise Java Bean
EJB JAR  –  A JAR packaging EJBs
Modular EJB  –  An EJB running in OSGi
EJB Bundle –  An OSGi bundle packaging  Modular EJBs </li></ul>
EJBs – the basics <ul><li>EJBs are managed objects, the container injects their dependencies </li></ul><ul><li>Session EJB...
The same view object may delegate to the same, or a different, EJB for successive calls
Different EJB types have different delegation styles </li></ul></ul><ul><li>The EJB runtime adds declarative transactions,...
EJB Views and OSGi services EJB View OSGi Service Cardinality May proxy one or many EJB objects One per registration or on...
Integrating EJBs with OSGi <ul><li>The service registry is  the  integration point in OSGi </li></ul><ul><ul><li>Expose mo...
Singleton is like a normal service
Stateful EJBs are “one per lookup” </li></ul></ul>
Identifying EJB Bundles in OSGi  <ul>Requirement </ul><ul><li>EJB Bundles should be able to be valid EJB JARs
Fit with existing OSGi module types (e.g. WABs) </li></ul><ul>Proposal <li>Add a new header “Export-EJB:” </li></ul><ul><u...
Defines which EJBs are exposed as OSGi services
Pioneered by Glassfish Application Server </li><ul><li>Known to work –  good basis for a standard? </li></ul></ul></ul>
Running Modular EJBs in Aries  <ul><li>Apache Aries provides pieces of an OSGi container </li></ul><ul><ul><li>Integrate w...
Issues Running EJBs in OSGi  <ul><li>Locating EJBs in OSGi bundles
OSGi class loading semantics
Building EJB proxy stubs
Transaction Manager integration
JPA integration
Security integration
Miscellaneous </li></ul>
Working with EJB Bundles  – Finding the EJBs <ul><li>EJBs are defined in one of two ways </li></ul><ul><ul><li>via annotat...
via XML  <session>... </li></ul></ul><ul><li>Requirement 1 says that EJB Bundles should put  XML in  META-INF/ejb-jar.xml ...
Finding the XML is non-trivial for OpenEJB in OSGi </li></ul><ul><ul><li>Use the extender pattern to help OpenEJB out
Recognise bundles using Export-EJB header </li></ul></ul>
Working with EJB Bundles  – Finding the EJBs (2) <ul><li>Finding Annotated EJBs much harder than for XML </li></ul><ul><ul...
Upcoming SlideShare
Loading in...5
×

Java EE | Modular EJBs for Enterprise OSGi | Tim Ward

2,064

Published on

2011-11-01 | 05:20 PM - 06:10 PM
Enterprise OSGi is all about enabling Java EE technologies in an OSGi environment. Modular EJB provides support for Enterprise Java Beans running inside OSGi, taking advantage of the framework's modularity. See how to: * Package EJBs for use in an OSGi environment * Make use of EJBs from other OSGi bundles * Consume OSGi services directly within your EJBs * Flow transactions between EJBs and OSGi

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
2,064
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
49
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Java EE | Modular EJBs for Enterprise OSGi | Tim Ward

  1. 1. Modular EJBs in OSGi <ul>Tim Ward IBM 21 Sep 2011 </ul><ul>OSGi Alliance Marketing © 2008-2010 . All Rights Reserved </ul><ul>Page </ul>
  2. 2. Legal <ul><ul><li>Java and all Java-based trademarks and logos are trademarks or registered trademarks of Oracle and/or its affiliates.
  3. 3. OSGi and the OSGi logo are trademarks or registered trademarks of the OSGi Alliance
  4. 4. Other product and service names might be trademarks of IBM or other companies. A current list of IBM trademarks is available on the Web at “Copyright and trademark information” at www.ibm.com/legal/copytrade.shtml </li></ul></ul><ul>Page </ul>
  5. 5. Agenda <ul><li>A Quick EJB refresher
  6. 6. How EJBs might fit into OSGi
  7. 7. A case study in implementing Modular EJB
  8. 8. Proof that it really works </li></ul>
  9. 9. Terminology <ul><li>EJB – Enterprise Java Bean
  10. 10. EJB JAR – A JAR packaging EJBs
  11. 11. Modular EJB – An EJB running in OSGi
  12. 12. EJB Bundle – An OSGi bundle packaging Modular EJBs </li></ul>
  13. 13. EJBs – the basics <ul><li>EJBs are managed objects, the container injects their dependencies </li></ul><ul><li>Session EJBs define one or more business “views” </li></ul><ul><ul><li>These proxy the real EJB object(s)
  14. 14. The same view object may delegate to the same, or a different, EJB for successive calls
  15. 15. Different EJB types have different delegation styles </li></ul></ul><ul><li>The EJB runtime adds declarative transactions, security and other services when a business method is called </li></ul>
  16. 16. EJB Views and OSGi services EJB View OSGi Service Cardinality May proxy one or many EJB objects One per registration or one per client bundle Location Stored in JNDI Stored in the OSGi service registry Interface One business interface per EJB view One service may expose many interfaces Lifecycle Relatively static once created, no reinjection, no notifications from JNDI Dynamic, may be removed or modified and underlying dependencies may change An EJB view shares a number of concepts with an OSGi service. <ul>Page </ul>
  17. 17. Integrating EJBs with OSGi <ul><li>The service registry is the integration point in OSGi </li></ul><ul><ul><li>Expose modular EJBs as OSGi services </li></ul></ul><ul><li>Register one service per EJB view </li></ul><ul><ul><li>Remote EJB views should be Remoteable Services </li><ul><li>service.exported.interfaces = * </li></ul></ul></ul><ul><li>EJB services only work with the right lookup lifecycle </li></ul><ul><ul><li>Stateless are an interchangable pool
  18. 18. Singleton is like a normal service
  19. 19. Stateful EJBs are “one per lookup” </li></ul></ul>
  20. 20. Identifying EJB Bundles in OSGi <ul>Requirement </ul><ul><li>EJB Bundles should be able to be valid EJB JARs
  21. 21. Fit with existing OSGi module types (e.g. WABs) </li></ul><ul>Proposal <li>Add a new header “Export-EJB:” </li></ul><ul><ul><li>Identifies a bundle as an EJB-Bundle
  22. 22. Defines which EJBs are exposed as OSGi services
  23. 23. Pioneered by Glassfish Application Server </li><ul><li>Known to work – good basis for a standard? </li></ul></ul></ul>
  24. 24. Running Modular EJBs in Aries <ul><li>Apache Aries provides pieces of an OSGi container </li></ul><ul><ul><li>Integrate with existing projects where possible </li></ul></ul><ul><li>OpenEJB has been packaged as an OSGi bundle for a couple of releases </li></ul><ul><ul><li>Some tentative OSGi support, little true integration </li></ul></ul>Mission Statement Integrate OpenEJB with existing OSGi standards and Aries features to provide support for Modular EJBs
  25. 25. Issues Running EJBs in OSGi <ul><li>Locating EJBs in OSGi bundles
  26. 26. OSGi class loading semantics
  27. 27. Building EJB proxy stubs
  28. 28. Transaction Manager integration
  29. 29. JPA integration
  30. 30. Security integration
  31. 31. Miscellaneous </li></ul>
  32. 32. Working with EJB Bundles – Finding the EJBs <ul><li>EJBs are defined in one of two ways </li></ul><ul><ul><li>via annotations @Stateless
  33. 33. via XML <session>... </li></ul></ul><ul><li>Requirement 1 says that EJB Bundles should put XML in META-INF/ejb-jar.xml (just like Java EE)
  34. 34. Finding the XML is non-trivial for OpenEJB in OSGi </li></ul><ul><ul><li>Use the extender pattern to help OpenEJB out
  35. 35. Recognise bundles using Export-EJB header </li></ul></ul>
  36. 36. Working with EJB Bundles – Finding the EJBs (2) <ul><li>Finding Annotated EJBs much harder than for XML </li></ul><ul><ul><li>Typically a OpenEJB “scans” the classpath by listing files on the file system (using file: or jar: URLs) </li></ul></ul><ul><li>In OSGi there is no guarantee of the bundle being on the filesystem (or in its original layout) </li></ul><ul><ul><li>Typical scanning breaks at this point, so either: </li><ul><li>Try to scan the raw bundle bytes (if they exist!) </li><ul><li>No fragments, imports or bundle classpath </li></ul><li>Implement an OSGi aware scanner </li></ul></ul></ul>
  37. 37. Apache Aries – Locating META-INF/ejb-jar.xml <ul><li>OpenEJB allows us to build our own EJBModule representing the EJB Bundle
  38. 38. An EJBModule allows us to supply a URL to the EJB XML deployment descriptor </li></ul><ul><ul><li>This is parsed and processed by OpenEJB </li></ul></ul><ul><li>Aries makes all .xml files in META-INF available </li></ul><ul><ul><li>Covers other Java EE specs
  39. 39. Covers OpenEJB config files </li></ul></ul>
  40. 40. Apache Aries – Writing an OSGi aware scanner <ul><li>In OSGi 4.3 a new core API method was added </li></ul><ul><ul><li>BundleWiring.listResources(String, String, int) </li></ul></ul><ul><li>We can use this to build an OSGi specific Xbean scanner for our EJBModule </li></ul>for (String resource : bundle.adapt(BundleWiring. class ). listResources( &quot;/&quot; , &quot;*.class&quot; , LISTRESOURCES_RECURSE )) { URL u = bundle.getResource(resource) readClassDef(u.openStream()); }
  41. 41. Issues Running EJBs in OSGi <ul><li>Locating EJBs in OSGi bundles
  42. 42. OSGi class loading semantics
  43. 43. Building EJB proxy stubs
  44. 44. Transaction Manager integration
  45. 45. JPA integration
  46. 46. Security integration
  47. 47. Miscellaneous </li></ul>
  48. 48. Java EE vs OSGi class loading <ul><li>One of the key differences between OSGi and Java EE is how they load classes
  49. 49. Java EE has a hierarchy of ClassLoader instances </li></ul><ul><ul><li>EJB JAR -> Application -> EJB Container -> Java </li></ul></ul><ul><li>OSGi has a ClassLoader network... </li></ul>
  50. 50. Apache Aries – Classloading for EJB Bundles <ul><li>Clearly the OpenEJB EJBModule ClassLoader should be the EJB Bundle ClassLoader
  51. 51. OpenEJB relies on the fact that its internals are visible from the EJB JAR ClassLoader </li></ul><ul><ul><li>No requirement for EJB Bundles to import OpenEJB
  52. 52. Make OpenEJB visible from Application ClassLoader </li></ul></ul>EJB Bundle EJB API Business API
  53. 53. Issues Running EJBs in OSGi <ul><li>Locating EJBs in OSGi bundles
  54. 54. OSGi class loading semantics
  55. 55. Building EJB proxy stubs
  56. 56. Transaction Manager integration
  57. 57. JPA integration
  58. 58. Security integration
  59. 59. Miscellaneous </li></ul><ul>Page </ul>
  60. 60. Implementing EJBs in OSGi <ul><li>EJBs are usually implemented using proxy “stubs” </li></ul><ul><ul><li>Implement a single business interface
  61. 61. May implement other container specific interfaces
  62. 62. Often implement javax.naming.Referencable </li></ul></ul><ul><li>Stubs may be dynamic proxys or generated classes </li></ul><ul><ul><li>In either case they must be loaded as classes </li></ul></ul><ul><li>In OSGi each bundle has a different ClassLoader </li></ul><ul><ul><li>There may not be any one bundle that can see all the interfaces on the proxy! </li></ul></ul>
  63. 63. Apache Aries – OSGi safe proxy classes <ul><li>Aries contains an OSGi aware proxy implementation </li></ul><ul><ul><li>Supports dynamic interface implementation generation for one to N interfaces </li></ul></ul><ul><li>The proxy allows a parent bundle to be specified </li></ul><ul><ul><li>The proxy understands that not all interfaces may be visible to the bundle! </li></ul></ul><ul><li>Aries replaces the default OpenEJB Proxy factory </li></ul><ul><ul><li>EJB stubs can use any mixture of interfaces </li></ul></ul>
  64. 64. Issues Running EJBs in OSGi <ul><li>Locating EJBs in OSGi bundles
  65. 65. OSGi class loading semantics
  66. 66. Building EJB proxy stubs
  67. 67. Transaction Manager integration
  68. 68. JPA integration
  69. 69. Security integration
  70. 70. Miscellaneous </li></ul><ul>Page </ul>
  71. 71. EJBs and Transactions <ul><li>EJBs have an extremely strong link with transactions </li></ul><ul><ul><li>All invocations use a global transaction by default </li></ul></ul><ul><li>More complex interactions can be configured </li></ul><ul><ul><li>EJBs can control their own transactions too </li></ul></ul><ul><li>In OSGi we use the JTA Service to get hold of a TransactionManager </li></ul><ul><ul><li>OpenEJB knows nothing about this... </li></ul></ul>Tx Client
  72. 72. Apache Aries – JTA integration <ul><li>Aries contains a JTA Service implementation that uses Geronimo's transaction manager </li></ul><ul><ul><li>Also provides a TransactionSynchronizationRegistry </li></ul></ul><ul><li>Aries overrides the OpenEJB transaction manager </li></ul><ul><ul><li>Use the JTA Service
  73. 73. Provide the Tx Manager and Tx Registry </li></ul></ul><ul><li>This is a clean and well used plug point </li></ul>
  74. 74. Issues Running EJBs in OSGi <ul><li>Locating EJBs in OSGi bundles
  75. 75. OSGi class loading semantics
  76. 76. Building EJB proxy stubs
  77. 77. Transaction Manager integration
  78. 78. JPA integration
  79. 79. Security integration
  80. 80. Miscellaneous </li></ul>
  81. 81. EJBs and JPA <ul><li>JPA replaced Entity Beans as the persistence strategy for Java EE
  82. 82. EJBs have tight integration with JPA </li></ul><ul><ul><li>Injection via Annotations @PersistenceUnit
  83. 83. Injection via XML <persistence-unit-ref> ...
  84. 84. JNDI lookup in java:comp/env </li></ul></ul><ul><li>EJBs may use JPA in two ways </li></ul><ul><ul><li>Application Managed
  85. 85. Container Managed </li></ul></ul>
  86. 86. EJBs and JPA – Application Managed <ul><li>In Application managed JPA the EJB manages lifecycle </li></ul><ul><ul><li>Responsible for creating and closing EntityManagers
  87. 87. Responsible for joining any active transactions </li></ul></ul><ul><li>Adds a dependency on a named persistence unit </li></ul><ul><ul><li>Injects or looks up an EntityManagerFactory </li></ul></ul><ul><li>OpenEJB expects to find, create, and manage any persistence units in persistence.xml </li></ul><ul><ul><li>ClassLoader problems make this impossible in OSGi </li></ul></ul>
  88. 88. Apache Aries – Updates to Aries JPA container <ul><li>Aries JPA normally uses the Meta-Persistence header to locate persistence bundles in the framework
  89. 89. Java EE also defines rules for finding persistence.xml </li></ul><ul><ul><li>WARs in WEB-INF/classes, or in WEB-INF/lib
  90. 90. EJB JARs in META-INF </li></ul></ul><ul><li>Aries JPA already checks for Web-ContextPath </li></ul><ul><ul><li>Add support for the Export-EJB header too </li></ul></ul>
  91. 91. Apache Aries – JPA (Application Managed) <ul><li>Hide META-INF/persistence.xml from OpenEJB </li></ul><ul><ul><li>Don't put the URL in the EJBModule
  92. 92. Override the OpenEJB validation failure, Aries JPA will provide the missing EntityManagerFactory! </li></ul></ul><ul><li>Listen for the registration of OSGi persistence units </li></ul><ul><ul><li>If the unit is used by an EJB then bind it into the right place in java:comp/env </li></ul></ul>EJB java:comp Extender
  93. 93. EJBs and JPA – Container Managed <ul><li>In Container managed JPA the container manages everything! </li></ul><ul><ul><li>Tx integration
  94. 94. Creating and closing EntityManagers </li></ul></ul><ul><li>More importantly, the container propagates context </li></ul><ul><ul><li>Different EJBs that use the same persistence unit in a transaction will get the same EntityManager </li></ul></ul><ul><li>Aries JPA already supports this mode of operation for blueprint beans and OSGi service lookups </li></ul>
  95. 95. Apache Aries – JPA (Container Managed) <ul><li>Replace the existing OpenEJB JPA context registry </li></ul><ul><ul><li>Check for Aries JPA contexts and OpenEJB contexts
  96. 96. Cross register any created contexts so both agree! </li></ul></ul><ul><li>Listen for the registration of OSGi persistence units </li></ul><ul><ul><li>If the unit is used as a managed context in an EJB then create an OpenEJB managed EntityManager </li><ul><li>Register this EntityManager in the relevant part of java:comp/env </li></ul></ul></ul>EJB java:comp Extender
  97. 97. Issues Running EJBs in OSGi <ul><li>Locating EJBs in OSGi bundles
  98. 98. OSGi class loading semantics
  99. 99. Building EJB proxy stubs
  100. 100. Transaction Manager integration
  101. 101. JPA integration
  102. 102. Security integration
  103. 103. Miscellaneous </li></ul>
  104. 104. Java EE Security <ul><li>Java EE supports role-based authorization </li></ul><ul><ul><li>On Servlet methods
  105. 105. On EJB method calls </li></ul></ul><ul><li>OpenEJB provides a Security Service plugpoint </li></ul><ul><ul><li>Allows third party authentication/authorization engines to be used </li></ul></ul><ul><li>Aries has no security component - any volunteers? </li></ul><ul><ul><li>OpenEJB can cope without a Security Service
  106. 106. No integration at this time </li></ul></ul>
  107. 107. Issues Running EJBs in OSGi <ul><li>Locating EJBs in OSGi bundles
  108. 108. OSGi class loading semantics
  109. 109. Building EJB proxy stubs
  110. 110. Transaction Manager integration
  111. 111. JPA integration
  112. 112. Security integration
  113. 113. Miscellaneous </li></ul>
  114. 114. Issues with OpenEJB in OSGi <ul><li>OpenEJB makes extensive use of XBean internally to build things </li></ul><ul><ul><li>This has the option of providing a ClassLoader </li><ul><li>OpenEJB typically provides none </li></ul></ul></ul><ul><li>OpenEjbVersion throws an ExceptionInInitializerError </li></ul><ul><ul><li>Attempts to classpath scan for properties </li></ul></ul><ul><li>To work around these Aries has to extensively set the Thread Context ClassLoader when starting OpenEJB </li></ul><ul><ul><li>New JAXB code in OpenEJB needs delegating to a 2.1 JAXB implementation every time we build an app </li></ul></ul>
  115. 115. Summary <ul><li>There are a few rough edges </li></ul><ul><ul><li>Some can easily be remedied in OpenEJB internals </li></ul></ul><ul><li>Some support is clearly missing, but could be added </li></ul><ul><ul><li>Security, Messaging, EJB lite </li></ul></ul><ul><li>Broadly speaking, it works </li></ul><ul><ul><li>And I can prove it! </li><ul><li>Apache Aries Blog sample with an EJB implemented comment service! </li></ul></ul></ul>
  116. 116. References Apache Aries: http://aries.apache.org/ Tim Ward: @TimothyWard [email_address] OSGi and JPA on YouTube: http://www.youtube.com/user/EnterpriseOSGi For more information on Enterprise OSGi take a look at Enterprise OSGi in Action : http://www.manning.com/cummins
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×