對spring瞭解的不夠精通,這兩天在解決jms異常的過程中發現,spring中提供了jmsTrasactionManager,同樣實現了交易管理介面。這樣用自動的攔截器,就可以象資料庫一樣自動控制事務。在同時配置了JMS和資料庫事務的時候,兩者同時有效。這樣系統的訊息和資料庫事務就輕量級的一致了!
資料庫的spring配置參見:http://steeven.cnblogs.com/archive/2005/06/14/174410.html
jms部分如下:
<bean id="remoteJmsConnectionFactory"
class="org.activemq.ActiveMQConnectionFactory">
<property name="useEmbeddedBroker">
<value>true</value>
</property>
<property name="brokerURL">
<value>tcp://localhost:61616</value>
</property>
</bean>
<bean id="jmsTM"
class="org.springframework.jms.connection.JmsTransactionManager">
<property name="connectionFactory">
<ref bean="remoteJmsConnectionFactory" />
</property>
</bean>
<bean id="jmsTransactionInterceptor"
class="org.springframework.transaction.interceptor.TransactionInterceptor">
<property name="transactionManager">
<ref bean="jmsTM" />
</property>
<property name="transactionAttributeSource">
<bean
class="org.springframework.transaction.annotation.AnnotationTransactionAttributeSource" />
</property>
</bean>
<bean
class="org.springframework.transaction.interceptor.TransactionAttributeSourceAdvisor">
<property name="transactionInterceptor">
<ref bean="jmsTransactionInterceptor" />
</property>
</bean>
<bean id="destResolver"
class="test.message.EnumDestinationResolver" />
<!-- for send jms to remote server -->
<bean id="remoteJmsTemplate"
class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory">
<ref bean="remoteJmsConnectionFactory" />
</property>
<property name="destinationResolver">
<ref local="destResolver" />
</property>
</bean>
應用程式很簡單
@Transactional
public class TestServiceImpl implements TestService {
public void someMethod() {
getJmsTemplate().send(someMessage);
}
}