|
You are here: Documentation » Integrating TransactionsEssentials » Tomcat Integration » Tomcat6Integration35 Tomcat 6 Integration with Atomikos 3.5.2+It is possible to fully integrate the Atomikos transaction manager into Tomcat. Doing it this way makes the transaction manager shared across all web applications exactly like with any full-blown J2EE server.
Important note When the Atomikos transaction manager is installed globally in Tomcat, you now must also install your JDBC driver at the same global location (ie: into the This is not a limitation of Atomikos nor of Tomcat but of the J2EE class loading design that both Tomcat and Atomikos must follow. InstallationInstallation is quite simple, it just involves copying some JAR files, a property file and editing some Tomcat configuration files. Copying Atomikos Tomcat Lifecycle libraryThe LifecycleListener has to be changed since release 3.5.2. The first class which calls UserTransactionManager.init() is the master for UserTransactionManager. It is not the first class which calls new UserTransactionManager(). Only the master closes UserTransactionManager with its close() method. Therefore UserTransactionManager.init() has to be called after the new operator. Here is the revised source code: Atomikos Lifecycle Listener.javapackage com.atomikos.tomcat; import org.apache.catalina.Lifecycle; import org.apache.catalina.LifecycleEvent; import org.apache.catalina.LifecycleListener; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import com.atomikos.icatch.jta.UserTransactionManager; import com.atomikos.icatch.system.Configuration; public class AtomikosLifecycleListener implements LifecycleListener { private static Log log = LogFactory.getLog(AtomikosLifecycleListener.class); private UserTransactionManager utm; public void lifecycleEvent(LifecycleEvent event) { try { if (Lifecycle.START_EVENT.equals(event.getType())) { if (utm == null) { log.info("starting Atomikos Transaction Manager " + Configuration.VERSION); utm = new UserTransactionManager(); } utm.init(); } else if (Lifecycle.AFTER_STOP_EVENT.equals(event.getType())) { if (utm != null) { log.info("shutting down Atomikos Transaction Manager"); utm.close(); } } } catch (Exception e) { log.error("Exception", e); } } } Copying Atomikos Tomcat BeanFactory library
This JAR contains a single class file that is an enhanced version of Tomcat JNDI's Bean Factory. Here is its source code: Bean Factory.javapackage com.atomikos.tomcat; import java.util.Hashtable; import java.util.Enumeration; import javax.naming.Name; import javax.naming.Context; import javax.naming.NamingException; import javax.naming.Reference; import javax.naming.RefAddr; import javax.naming.spi.ObjectFactory; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.naming.ResourceRef; import org.apache.naming.factory.Constants; import com.atomikos.beans.PropertyUtils; import com.atomikos.jdbc.AtomikosDataSourceBean; public class BeanFactory implements ObjectFactory { private final static Log log = LogFactory.getLog(BeanFactory.class); public Object getObjectInstance ( Object obj, Name name, Context nameCtx, Hashtable environment) throws NamingException { if (obj instanceof ResourceRef) { try { Reference ref = (Reference) obj; String beanClassName = ref.getClassName(); Class beanClass = null; ClassLoader tcl = Thread.currentThread().getContextClassLoader(); if (tcl != null) { try { beanClass = tcl.loadClass(beanClassName); } catch(ClassNotFoundException e) { } } else { try { beanClass = Class.forName(beanClassName); } catch(ClassNotFoundException e) { e.printStackTrace(); } } if (beanClass == null) { throw new NamingException("Class not found: " + beanClassName); } if (!AtomikosDataSourceBean.class.isAssignableFrom(beanClass)) { throw new NamingException( "Class is not a AtomikosDataSourceBean: " + beanClassName); } if (log.isDebugEnabled()) log.debug("instanciating bean of class " + beanClass.getName()); AtomikosDataSourceBean bean = (AtomikosDataSourceBean) beanClass.newInstance(); int i=0; Enumeration en = ref.getAll(); while (en.hasMoreElements()) { RefAddr ra = (RefAddr) en.nextElement(); String propName = ra.getType(); if (propName.equals(Constants.FACTORY) || propName.equals("description") || propName.equals("scope") || propName.equals("auth")) { continue; } String value = (String) ra.getContent(); if (log.isDebugEnabled()) log.debug("setting property '" + propName + "' to '" + value + "'"); PropertyUtils.setProperty(bean, propName, value); i++; } if (log.isDebugEnabled()) log.debug("done setting " + i + " property(ies), now initializing resource " + bean); bean.init(); return bean; } catch (Exception ex) { throw (NamingException) new NamingException( "error creating AtomikosDataSourceBean").initCause(ex); } } else { return null; } } } Copying TransactionsEssentials libraries
You should also copy the transactions-hibernate3.jar and/or transactions-hibernate2.jar at the same location if you're planning to use Hibernate. Copying Atomikos configuration file
Edit server.xml Then edit the <Listener className="org.apache.catalina.core.AprLifecycleListener" /> <Listener className="org.apache.catalina.mbeans.ServerLifecycleListener" /> <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" /> <Listener className="org.apache.catalina.storeconfig.StoreConfigLifecycleListener"/> Right after the last one, add this fifth one: <Listener className="com.atomikos.tomcat.AtomikosLifecycleListener" /> Edit context.xml Then edit the <WatchedResource>WEB-INF/web.xml</WatchedResource> Right after it, add that one: <Transaction factory="com.atomikos.icatch.jta.UserTransactionFactory" /> Example applicationHere is a sample application that demonstrates how you can run TransactionsEssentials in a web application after it has been globally installed. It is a simple blueprint application that shows and updates the content of a single Derby database. Download the sample application here: dbtest.war To install it, simply copy the WAR file in Tomcat's webapps folder. You also need to install Derby's JDBC driver in You can then access it via this URL: http://localhost:8080/dbtest/. Notes
Using MySQLThe example uses Derby - however it can be configured with MySQL by changing the webapp context similar to this: <?xml version="1.0" encoding="UTF-8"?> <Context path="/dbtest" docBase="dbtest.war" reloadable="true" crossContext="true"> <!-- Resource configuration for JDBC datasource--> <Resource name="jdbc/myDB" auth="Container" type="com.atomikos.jdbc.AtomikosDataSourceBean" factory="com.atomikos.tomcat.BeanFactory" uniqueResourceName="jdbc/myDB" xaDataSourceClassName="com.mysql.jdbc.jdbc2.optional.MysqlXADataSource" xaProperties.databaseName="test" xaProperties.serverName="localhost" xaProperties.port="3306" xaProperties.user="USER" xaProperties.password="PASSWORD" xaProperties.url="jdbc:mysql://localhost:3306/test" /> </Context> Remember to change the parameter values to your specific environment... Using WebSphere MQThis example shows how to define pooled JMS Queue Connection Factories and Queues for WebSphere MQ. Note that the uniqueResourceName MUST contain the text MQSeries_XA_RMI. <?xml version="1.0" encoding="UTF-8"?> <Context path="/dbtest" docBase="dbtest.war" reloadable="true" crossContext="true"> <Resource name="jms/myQCF" auth="Container" type="com.atomikos.jms.AtomikosConnectionFactoryBean" factory="com.atomikos.tomcat.EnhancedTomcatAtomikosBeanFactory" uniqueResourceName="myQCF_MQSeries_XA_RMI" xaConnectionFactoryClassName="com.ibm.mq.jms.MQXAQueueConnectionFactory" xaProperties.queueManager="XXXX" xaProperties.hostName="hostname" xaProperties.port="1426" xaProperties.channel="XXXX" maxPoolSize="3" minPoolSize="1" /> <Resource name="jms/myQ" auth="Container" type="com.ibm.mq.jms.MQQueue" factory="com.ibm.mq.jms.MQQueueFactory" description="JMS Queue for reading messages" QU="MYQ.IN" CCSID="819" persistence="2" /> </Context> For this to work, you need an improved version of the Bean Factory described above, which can also handle JMS Connection Factory Beans: EnhancedTomcatAtomikosBeanFactory .javapackage com.atomikos.tomcat; import java.util.Hashtable; import java.util.Enumeration; import javax.jms.JMSException; import javax.naming.Name; import javax.naming.Context; import javax.naming.NamingException; import javax.naming.Reference; import javax.naming.RefAddr; import javax.naming.spi.ObjectFactory; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.naming.ResourceRef; import org.apache.naming.factory.Constants; import com.atomikos.beans.PropertyException; import com.atomikos.beans.PropertyUtils; import com.atomikos.jdbc.AtomikosDataSourceBean; import com.atomikos.jdbc.AtomikosSQLException; import com.atomikos.jms.AtomikosConnectionFactoryBean; /** * enhancement of com.atomikos.tomcat.BeanFactory (see http://www.atomikos.com/Documentation/Tomcat6Integration33) * */ public class EnhancedTomcatAtomikosBeanFactory implements ObjectFactory { private final static Log log = LogFactory.getLog(RSFTomcatAtomikosBeanFactory.class); public Object getObjectInstance (Object obj, Name name, Context nameCtx, Hashtable,?> environment) throws NamingException { if (obj instanceof ResourceRef) { try { Reference ref = (Reference) obj; String beanClassName = ref.getClassName(); Class> beanClass = null; ClassLoader tcl = Thread.currentThread().getContextClassLoader(); if (tcl != null) { try { beanClass = tcl.loadClass(beanClassName); } catch(ClassNotFoundException e) { } } else { try { beanClass = Class.forName(beanClassName); } catch(ClassNotFoundException e) { e.printStackTrace(); } } if (beanClass == null) { throw new NamingException("Class not found: " + beanClassName); } if (AtomikosDataSourceBean.class.isAssignableFrom(beanClass)) { return createDataSourceBean(ref, (Class extends AtomikosDataSourceBean>)beanClass); } else if (AtomikosConnectionFactoryBean.class.isAssignableFrom(beanClass)) { return createConnectionFactoryBean(ref, (Class extends AtomikosConnectionFactoryBean>)beanClass); } else { throw new NamingException( "Class is neither an AtomikosDataSourceBean nor an AtomikosConnectionFactoryBean: " + beanClassName); } } catch (Exception ex) { throw (NamingException) new NamingException( "error creating AtomikosDataSourceBean").initCause(ex); } } else { return null; } } /** * create a DataSourceBean for a JMS datasource * @param ref * @param beanClass * @return * @throws InstantiationException * @throws IllegalAccessException * @throws PropertyException * @throws AtomikosSQLException * @throws JMSException */ private Object createConnectionFactoryBean(Reference ref, Class extends AtomikosConnectionFactoryBean> beanClass) throws InstantiationException, IllegalAccessException, PropertyException, JMSException { if (log.isDebugEnabled()) log.debug("instanciating bean of class " + beanClass.getName()); AtomikosConnectionFactoryBean bean = (AtomikosConnectionFactoryBean) beanClass.newInstance(); int i=0; Enumeration
Attach
![]() |