Created
October 31, 2012 16:05
-
-
Save cbertoldi/3987919 to your computer and use it in GitHub Desktop.
Problem with JMS transactions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <bean id="inVMConnectionFactory" class="org.springframework.jndi.JndiObjectFactoryBean"> | |
| <property name="jndiName"> | |
| <value>java:/ConnectionFactory</value> | |
| </property> | |
| </bean> | |
| <bean id="cachedConnectionFactory" class="org.springframework.jms.connection.CachingConnectionFactory"> | |
| <property name="targetConnectionFactory" ref="inVMConnectionFactory" /> | |
| </bean> | |
| <bean id="producer" class="it.ubiquity.gestoreprofilazione.onweb.OnWebProducer" scope="singleton"> | |
| <property name="queue" ref="retryQueue" /> | |
| <property name="connectionFactory" ref="cachedConnectionFactory" /> | |
| </bean> | |
| <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate"> | |
| <property name="connectionFactory" ref="cachedConnectionFactory" /> | |
| <property name="sessionTransacted" value="true" /> | |
| <property name="sessionAcknowledgeMode" value="#{T(javax.jms.Session).CLIENT_ACKNOWLEDGE}" /> | |
| <property name="pubSubDomain" value="false" /> | |
| <property name="receiveTimeout" value="#{T(org.springframework.jms.core.JmsTemplate).RECEIVE_TIMEOUT_NO_WAIT}" /> | |
| </bean> | |
| <bean id="jmsTransactionManager" class="org.springframework.jms.connection.JmsTransactionManager"> | |
| <property name="connectionFactory" ref="cachedConnectionFactory" /> | |
| </bean> | |
| <bean id="consumer" class="it.ubiquity.gestoreprofilazione.onweb.OnWebConsumer" scope="singleton"> | |
| <property name="queue" ref="retryQueue" /> | |
| <property name="jmsTemplate" ref="jmsTemplate" /> | |
| <property name="transactionManager" ref="jmsTransactionManager" /> | |
| </bean> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public void receive() { | |
| TransactionStatus status = transactionManager.getTransaction(new DefaultTransactionDefinition()); | |
| Message msg = jmsTemplate.receive(queue); | |
| boolean success = false; | |
| if (msg != null) { | |
| try { | |
| success = handleMessage(msg); | |
| if (success) { | |
| msg.acknowledge(); // session is still open within the transaction | |
| } | |
| } catch (JMSException e) { | |
| transactionManager.rollback(status); | |
| } | |
| if (success) | |
| transactionManager.commit(status); | |
| else | |
| transactionManager.rollback(status): | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment