项目需要些一个在Websphere下写一个Scheduler,很自然想到使用Quartz,可是总出现unmanaged thead的问题,折腾多时最后放弃使用org.springframework.transaction.jta.WebSphereUowTransactionManager,改用com.atomikos.icatch.jta.UserTransactionManager,问题就解决了
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jee="http://www.springframework.org/schema/jee"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd">
<context:component-scan base-package="xx.yy" />
<context:annotation-config />
<jee:jndi-lookup id="spds" jndi-name="jdbc/X1"
cache="true" expected-type="javax.sql.DataSource"
lookup-on-startup="true" />
<jee:jndi-lookup id="srsds" jndi-name="jdbc/X2"
cache="true" expected-type="javax.sql.DataSource"
lookup-on-startup="true" />
<jee:jndi-lookup id="casds" jndi-name="jdbc/X3"
cache="true" expected-type="javax.sql.DataSource"
lookup-on-startup="true" />
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceXmlLocation"
value="classpath:META-INF/persistence.xml" />
<property name="persistenceUnitName" value="sp" />
<property name="dataSource" ref="spds" />
</bean>
<bean id="srsEntityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceXmlLocation"
value="classpath:META-INF/persistence.xml" />
<property name="persistenceUnitName" value="srs" />
<property name="dataSource" ref="srsds" />
</bean>
<bean id="casEntityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceXmlLocation"
value="classpath:META-INF/persistence.xml" />
<property name="persistenceUnitName" value="cas" />
<property name="dataSource" ref="casds" />
</bean>
<bean id="AtomikosTransactionManager"
class="com.atomikos.icatch.jta.UserTransactionManager"
init-method="init" destroy-method="close">
<property name="forceShutdown" value="false" />
</bean>
<bean id="AtomikosUserTransaction"
class="com.atomikos.icatch.jta.UserTransactionImp">
<property name="transactionTimeout" value="300" />
</bean>
<bean id="transactionManager"
class="org.springframework.transaction.jta.JtaTransactionManager">
<property name="transactionManager"
ref="AtomikosTransactionManager" />
<property name="userTransaction" ref="AtomikosUserTransaction" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
<tx:advice id="cronAdvice"
transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="get*" read-only="true" />
<tx:method name="*" read-only="false" />
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="cronOperation"
expression="execution(public * xx.yy..*Service(..))" />
<aop:advisor advice-ref="cronAdvice"
pointcut-ref="cronOperation" />
</aop:config>
<!-- mail -->
<context:property-placeholder location="WEB-INF/mail.properties" />
<bean id="mailSender"
class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host" value="${mail.host}" />
<property name="username" value="${mail.username}" />
<property name="password" value="${mail.password}" />
<property name="port" value="${mail.port}" />
<property name="javaMailProperties">
<props>
<prop key="mail.debug">false</prop>
<prop key="mail.smtp.auth">true</prop>
</props>
</property>
</bean>
<bean id="templateMessage"
class="org.springframework.mail.SimpleMailMessage">
<property name="from" value="${mail.from}" />
</bean>
</beans>
applicationContext-Quartz.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jee="http://www.springframework.org/schema/jee"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd">
<bean id="ttl006Timer"
class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="txnSyncJob" />
<property name="targetMethod" value="run" />
<property name="concurrent" value="false" />
</bean>
<bean id="ttl06cronTrigger"
class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="ttl006Timer" />
<property name="cronExpression" value="0/30 * * * * ?" />
</bean>
<bean id="act016Timer"
class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="activityJob" />
<property name="targetMethod" value="run" />
<property name="concurrent" value="false" />
</bean>
<bean id="act016cronTrigger"
class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="act016Timer" />
<property name="cronExpression" value="0/50 * * * * ?" />
</bean>
<bean
class="org.springframework.scheduling.quartz.SchedulerFactoryBean" destroy-method="destroy">
<property name="waitForJobsToCompleteOnShutdown" value="false"/>
<property name="triggers">
<list>
<ref bean="ttl06cronTrigger" />
<ref bean="act016cronTrigger" />
</list>
</property>
</bean>
</beans>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml, /WEB-INF/applicationContext-Quartz.xml</param-value>
</context-param>
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/classes/log4j.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>