I tried the org.springframework.transaction.support.Transactio nTemplate to receive Message in transaction
my code:
Code:
TransactionTemplate tt = (TransactionTemplate)ac.getBean("transactionTemplate");
final JmsTemplate jms = (JmsTemplate) ac.getBean("jmsTemplate");
tt.execute(new TransactionCallback() {
public Object doInTransaction(TransactionStatus ts) {
try{
for(int i=0;i<10;i++){
Message tm = jms.receive();
if(i==5) throw new RuntimeException();
log.info("[" + tm.getStringProperty("SN") + "]");
}
}catch(Exception e){
ts.setRollbackOnly();
}
return null;
}
});
my configuratrion file is below:
Code:
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="defaultDestination">
<ref bean="dest"/>
</property>
<property name="connectionFactory" ref="connectionFactory"/>
</bean>
<bean id="connectionFactory" class="org.activemq.ActiveMQConnectionFactory">
<!--<property name="brokerURL" value="tcp://localhost:61616" />-->
<property name="brokerURL" value="tcp://localhost:61616"/>
<property name="useEmbeddedBroker" value="false"/>
</bean>
<bean id="dest" class="org.activemq.message.ActiveMQQueue">
<constructor-arg index="0" value="TestHUGE77.Queue" />
</bean>
<bean id="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate">
<property name="transactionManager">
<ref bean="jmsTransactionManager"/>
</property>
</bean>
<bean id="jmsTransactionManager" class="org.springframework.jms.connection.JmsTransactionManager">
<property name="connectionFactory">
<ref local="connectionFactory" />
</property>
</bean>
I use the Activemq as the default configuration.
Here is the Information in console:
Code:
2005-11-21 11:38:50,109 DEBUG [org.springframework.jms.connection.JmsTransactionManager] - Initiating transaction rollback
2005-11-21 11:38:50,109 DEBUG [org.activemq.TransactionContext] - Rolledback local transaction: null
2005-11-21 11:38:50,109 DEBUG [org.springframework.transaction.support.TransactionSynchronizationManager] - Removed value [org.springframework.jms.connection.ConnectionHolder@d251a3] for key [org.activemq.ActiveMQConnectionFactory@860d49] from thread [main]
I also can't receive the Message I want.
Is there any problem in my code or configuration.
PS:When I use the spring configuration to send Messages ,it is OK.
Any body give some suggestions?