Hazelcast can be integrated into J2EE containers via Hazelcast Resource Adapter ( hazelcast-ra.rar ). After proper configuration, Hazelcast can participate in standard J2EE transactions.
<%@page import="javax.resource.ResourceException" %> <%@page import="javax.transaction.*" %> <%@page import="javax.naming.*" %> <%@page import="javax.resource.cci.*" %> <%@page import="java.util.*" %> <%@page import="com.hazelcast.core.Hazelcast" %> <% UserTransaction txn = null; Connection conn = null; Queue queue = Hazelcast.getQueue ("default"); Map map = Hazelcast.getMap ("default"); Set set = Hazelcast.getSet ("default"); List list = Hazelcast.getList ("default"); try { Context context = new InitialContext(); txn = (UserTransaction) context.lookup("java:comp/UserTransaction"); txn.begin(); ConnectionFactory cf = (ConnectionFactory) context.lookup ("java:comp/env/HazelcastCF"); conn = cf.getConnection(); queue.offer("newitem"); map.put ("1", "value1"); set.add ("item1"); list.add ("listitem1"); txn.commit(); } catch (Throwable e) { if (txn != null) { try { txn.rollback(); } catch (Exception ix) {ix.printStackTrace();}; } e.printStackTrace(); } finally { if (conn != null) { try { conn.close(); } catch (Exception ignored) {}; } } %>
Deploying and configuring Hazelcast resource adapter is no different than any other
resource adapter since it is a standard JCA resource adapter but
resource adapter installation and configuration is container specific, so please consult
your J2EE vendor documentation for details. Most common steps are:
Add the hazelcast.jar to container's classpath. Usually
there is a lib directory that is loaded automatically by the container on
startup.
Deploy hazelcast-ra.rar. Usually there is a some kind of
deploy directory. Name of the directory varies by container.
Make container specific configurations when/after
deploying hazelcast-ra.rar. Besides container specific
configurations, JNDI name for Hazelcast resource is
set.
Configure your application to use the Hazelcast resource. Updating
web.xml and/or ejb-jar.xml to let
container know that your application will use the Hazelcast resource and
define the resource reference.
Make container specific application configuration to specify
JNDI name used for the resource in the application.
Place the hazelcast-<version>.jar into
GLASSFISH_HOME/glassfish/domains/domain1/lib/ext/
directory.
Place the hazelcast-ra-<version>.rar into
GLASSFISH_HOME/glassfish/domains/domain1/autodeploy/
directory
Add the following lines to the web.xml file.
<resource-ref>
<res-ref-name>HazelcastCF</res-ref-name>
<res-type>com.hazelcast.jca.ConnectionFactoryImpl</res-type>
<res-auth>Container</res-auth>
</resource-ref>
Notice that we didn't have to put sun-ra.xml into the
rar file because it comes with the hazelcast-ra-<version>.rar
file already.
If Hazelcast resource is used from EJBs, you should configure
ejb-jar.xml for resource reference and JNDI
definitions, just like we did for web.xml.
Place the hazelcast-<version>.jar into
JBOSS_HOME/server/deploy/default/lib directory.
Place the hazelcast-ra-<version>.rar into
JBOSS_HOME/server/deploy/default/deploy directory
Create a hazelcast-ds.xml at
JBOSS_HOME/server/deploy/default/deploy directory
containing the following content. Make sure to set the
rar-name element
to hazelcast-ra-<version>.rar.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE connection-factories
PUBLIC "-//JBoss//DTD JBOSS JCA Config 1.5//EN"
"http://www.jboss.org/j2ee/dtd/jboss-ds_1_5.dtd">
<connection-factories>
<tx-connection-factory>
<local-transaction/>
<track-connection-by-tx>true</track-connection-by-tx>
<jndi-name>HazelcastCF</jndi-name>
<rar-name>hazelcast-ra-<version>.rar</rar-name>
<connection-definition>
javax.resource.cci.ConnectionFactory
</connection-definition>
</tx-connection-factory>
</connection-factories>
Add the following lines to the web.xml file.
<resource-ref>
<res-ref-name>HazelcastCF</res-ref-name>
<res-type>com.hazelcast.jca.ConnectionFactoryImpl</res-type>
<res-auth>Container</res-auth>
</resource-ref>
Add the following lines to the jboss-web.xml file.
<resource-ref>
<res-ref-name>HazelcastCF</res-ref-name>
<jndi-name>java:HazelcastCF</jndi-name>
</resource-ref>
If Hazelcast resource is used from EJBs, you should configure
ejb-jar.xml and jboss.xml for resource
reference and JNDI definitions.