/hazelcast-documentation/src/main/docbook/manual/content/j2ee/JEEIntegration.xml

https://bitbucket.org/gabral6_gmailcom/hazelcast · XML · 264 lines · 242 code · 7 blank · 15 comment · 0 complexity · 20b37856568fff2fa4b7b8f6a17ccc4d MD5 · raw file

  1. <?xml version='1.0' encoding='UTF-8'?>
  2. <!--
  3. ~ Copyright (c) 2008-2013, Hazelcast, Inc. All Rights Reserved.
  4. ~
  5. ~ Licensed under the Apache License, Version 2.0 (the "License");
  6. ~ you may not use this file except in compliance with the License.
  7. ~ You may obtain a copy of the License at
  8. ~
  9. ~ http://www.apache.org/licenses/LICENSE-2.0
  10. ~
  11. ~ Unless required by applicable law or agreed to in writing, software
  12. ~ distributed under the License is distributed on an "AS IS" BASIS,
  13. ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. ~ See the License for the specific language governing permissions and
  15. ~ limitations under the License.
  16. -->
  17. <sect1 xml:id="JEEIntegration" version='5.0' xmlns='http://docbook.org/ns/docbook'
  18. xmlns:xi="http://www.w3.org/2001/XInclude"
  19. xmlns:xlink="http://www.w3.org/1999/xlink"
  20. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  21. xsi:schemaLocation="http://docbook.org/ns/docbook http://www.docbook.org/xml/5.0/xsd/docbook.xsd
  22. http://www.w3.org/1999/xlink http://www.w3.org/1999/xlink.xsd">
  23. <title>J2EE Integration</title>
  24. <para>Hazelcast can be integrated into J2EE containers via Hazelcast Resource Adapter (
  25. hazelcast-ra.rar ). After proper configuration, Hazelcast can participate in standard J2EE
  26. transactions.
  27. <programlisting language="java"><![CDATA[<%@page import="javax.resource.ResourceException" %>
  28. <%@page import="javax.transaction.*" %>
  29. <%@page import="javax.naming.*" %>
  30. <%@page import="javax.resource.cci.*" %>
  31. <%@page import="java.util.*" %>
  32. <%@page import="com.hazelcast.core.Hazelcast" %>
  33. <%
  34. UserTransaction txn = null;
  35. Connection conn = null;
  36. Config cfg = new Config();
  37. HazelcastInstance hz = Hazelcast.newHazelcastInstance(cfg);
  38. Queue queue = hz.getQueue ("default");
  39. Map map = hz.getMap ("default");
  40. Set set = hz.getSet ("default");
  41. List list = hz.getList ("default");
  42. try {
  43. Context context = new InitialContext();
  44. txn = (UserTransaction) context.lookup("java:comp/UserTransaction");
  45. txn.begin();
  46. ConnectionFactory cf = (ConnectionFactory) context.lookup ("java:comp/env/HazelcastCF");
  47. conn = cf.getConnection();
  48. queue.offer("newitem");
  49. map.put ("1", "value1");
  50. set.add ("item1");
  51. list.add ("listitem1");
  52. txn.commit();
  53. } catch (Throwable e) {
  54. if (txn != null) {
  55. try {
  56. txn.rollback();
  57. } catch (Exception ix) {ix.printStackTrace();};
  58. }
  59. e.printStackTrace();
  60. } finally {
  61. if (conn != null) {
  62. try {
  63. conn.close();
  64. } catch (Exception ignored) {};
  65. }
  66. }
  67. %>
  68. ]]></programlisting>
  69. </para>
  70. <sect2 xml:id="JEEIntegrationConfig" version='5.0' xmlns='http://docbook.org/ns/docbook'
  71. xmlns:xlink='http://www.w3.org/1999/xlink'
  72. xmlns:xi='http://www.w3.org/2001/XInclude'
  73. xmlns:ns5='http://www.w3.org/2000/svg'
  74. xmlns:ns4='http://www.w3.org/1998/Math/MathML'
  75. xmlns:ns3='http://www.w3.org/1999/xhtml'
  76. xmlns:db='http://docbook.org/ns/docbook'>
  77. <title>Resource Adapter Configuration</title>
  78. <para>Deploying and configuring Hazelcast resource adapter is no different than any other
  79. resource adapter since it is a standard
  80. <literal>JCA</literal>
  81. resource adapter but
  82. resource adapter installation and configuration is container specific, so please consult
  83. your J2EE vendor documentation for details. Most common steps are:
  84. <orderedlist>
  85. <listitem>
  86. <para>Add the
  87. <literal>hazelcast.jar</literal>
  88. to container's classpath. Usually
  89. there is a lib directory that is loaded automatically by the container on
  90. startup.
  91. </para>
  92. </listitem>
  93. <listitem>
  94. <para>Deploy<literal>hazelcast-ra.rar</literal>. Usually there is a some kind of
  95. deploy directory. Name of the directory varies by container.
  96. </para>
  97. </listitem>
  98. <listitem>
  99. <para>Make container specific configurations when/after
  100. deploying<literal>hazelcast-ra.rar</literal>. Besides container specific
  101. configurations,
  102. <literal>JNDI</literal>
  103. name for Hazelcast resource is
  104. set.
  105. </para>
  106. </listitem>
  107. <listitem>
  108. <para>Configure your application to use the Hazelcast resource. Updating
  109. <literal>web.xml</literal>
  110. and/or
  111. <literal>ejb-jar.xml</literal>
  112. to let
  113. container know that your application will use the Hazelcast resource and
  114. define the resource reference.
  115. </para>
  116. </listitem>
  117. <listitem>
  118. <para>Make container specific application configuration to specify
  119. <literal>JNDI</literal>
  120. name used for the resource in the application.
  121. </para>
  122. </listitem>
  123. </orderedlist>
  124. </para>
  125. </sect2>
  126. <sect2 xml:id="JEEIntegrationGlassfishSample" version='5.0' xmlns='http://docbook.org/ns/docbook'
  127. >
  128. <title>Sample Glassfish v3 Web Application Configuration</title>
  129. <para>
  130. <orderedlist>
  131. <listitem>
  132. <para>Place the
  133. <literal>hazelcast-&lt;version&gt;.jar</literal>
  134. into
  135. <literal>GLASSFISH_HOME/glassfish/domains/domain1/lib/ext/</literal>
  136. directory.
  137. </para>
  138. </listitem>
  139. <listitem>
  140. <para>Place the
  141. <literal>hazelcast-ra-&lt;version&gt;.rar</literal>
  142. into
  143. <literal>GLASSFISH_HOME/glassfish/domains/domain1/autodeploy/</literal>
  144. directory
  145. </para>
  146. </listitem>
  147. <listitem>
  148. <para>Add the following lines to the
  149. <literal>web.xml</literal>
  150. file.
  151. <programlisting language="xml"><![CDATA[<resource-ref>
  152. <res-ref-name>HazelcastCF</res-ref-name>
  153. <res-type>com.hazelcast.jca.ConnectionFactoryImpl</res-type>
  154. <res-auth>Container</res-auth>
  155. </resource-ref>
  156. ]]></programlisting>
  157. </para>
  158. </listitem>
  159. </orderedlist>
  160. Notice that we didn't have to put
  161. <literal>sun-ra.xml</literal>
  162. into the
  163. rar file because it comes with the
  164. <literal>hazelcast-ra-&lt;version&gt;.rar</literal>
  165. file already.
  166. </para>
  167. <para>If Hazelcast resource is used from EJBs, you should configure
  168. <literal>ejb-jar.xml</literal>
  169. for resource reference and
  170. <literal>JNDI</literal>
  171. definitions, just like we did for<literal>web.xml</literal>.
  172. </para>
  173. </sect2>
  174. <sect2 xml:id="JEEIntegrationJBossSample" version='5.0' xmlns='http://docbook.org/ns/docbook'
  175. >
  176. <title>Sample JBoss Web Application Configuration</title>
  177. <para>
  178. <itemizedlist>
  179. <listitem>
  180. <para>Place the
  181. <literal>hazelcast-&lt;version&gt;.jar</literal>
  182. into
  183. <literal>JBOSS_HOME/server/deploy/default/lib</literal>
  184. directory.
  185. </para>
  186. </listitem>
  187. <listitem>
  188. <para>Place the
  189. <literal>hazelcast-ra-&lt;version&gt;.rar</literal>
  190. into
  191. <literal>JBOSS_HOME/server/deploy/default/deploy</literal>
  192. directory
  193. </para>
  194. </listitem>
  195. <listitem>
  196. <para>Create a
  197. <literal>hazelcast-ds.xml</literal>
  198. at
  199. <literal>JBOSS_HOME/server/deploy/default/deploy</literal>
  200. directory
  201. containing the following content. Make sure to set the
  202. <literal>rar-name</literal>
  203. element
  204. to<literal>hazelcast-ra-&lt;version&gt;.rar</literal>.
  205. <programlisting language="xml"><![CDATA[<?xml version="1.0" encoding="UTF-8"?>
  206. <!DOCTYPE connection-factories
  207. PUBLIC "-//JBoss//DTD JBOSS JCA Config 1.5//EN"
  208. "http://www.jboss.org/j2ee/dtd/jboss-ds_1_5.dtd">
  209. <connection-factories>
  210. <tx-connection-factory>
  211. <local-transaction/>
  212. <track-connection-by-tx>true</track-connection-by-tx>
  213. <jndi-name>HazelcastCF</jndi-name>
  214. <rar-name>hazelcast-ra-<version>.rar</rar-name>
  215. <connection-definition>
  216. javax.resource.cci.ConnectionFactory
  217. </connection-definition>
  218. </tx-connection-factory>
  219. </connection-factories>
  220. ]]></programlisting>
  221. </para>
  222. </listitem>
  223. <listitem>
  224. <para>Add the following lines to the
  225. <literal>web.xml</literal>
  226. file.
  227. <programlisting language="xml"><![CDATA[<resource-ref>
  228. <res-ref-name>HazelcastCF</res-ref-name>
  229. <res-type>com.hazelcast.jca.ConnectionFactoryImpl</res-type>
  230. <res-auth>Container</res-auth>
  231. </resource-ref>
  232. ]]></programlisting>
  233. </para>
  234. </listitem>
  235. <listitem>
  236. <para>Add the following lines to the
  237. <literal>jboss-web.xml</literal>
  238. file.
  239. <programlisting language="xml"><![CDATA[<resource-ref>
  240. <res-ref-name>HazelcastCF</res-ref-name>
  241. <jndi-name>java:HazelcastCF</jndi-name>
  242. </resource-ref>
  243. ]]></programlisting>
  244. </para>
  245. </listitem>
  246. </itemizedlist>
  247. If Hazelcast resource is used from EJBs, you should configure
  248. <literal>ejb-jar.xml</literal>
  249. and
  250. <literal>jboss.xml</literal>
  251. for resource
  252. reference and
  253. <literal>JNDI</literal>
  254. definitions.
  255. </para>
  256. </sect2>
  257. </sect1>