Results 1 to 3 of 3

Thread: How do ClientAcknowlege take effect in JmsTemplate?

  1. #1
    Join Date
    Jul 2005
    Posts
    24

    Default How do ClientAcknowlege take effect in JmsTemplate?

    When I set the ClientAcknowlege in JMS, According to JMS Spec,No. 15th message will redelivered,I must receive NO.15th Message in next receive operation.

    But when I use the code below,it would skip No. 15th message, I can't receive the NO.15th Message anymore.
    What is the problem in my code or configuration?
    Code:
    ApplicationContext ac = new FileSystemXmlApplicationContext("send.xml");
    		JmsTemplate jms = (JmsTemplate) ac.getBean("jmsTemplate");
    		jms.setSessionAcknowledgeMode(Session.CLIENT_ACKNOWLEDGE);
    for(int i=1; i<100000;i++){
    			System.out.println(i);			
    			try {
    				Message tm = jms.receive();
    				if(i == 15) throw new RuntimeException();
    				log.info("[" + tm.getStringProperty("SN") + "]");
    				tm.acknowledge();
    			} catch (Exception e) {
    				System.exit(0);
    			}
    			//tm.acknowledge();			
    		}
    My config file:
    Code:
    <bean id="dest"  class="org.activemq.message.ActiveMQQueue">
        <constructor-arg index="0" value="TestHUGE66.Queue" />
    </bean>  
    
    <bean id="jmsFactory" class="org.activemq.pool.PooledConnectionFactory">
    	<property name="connectionFactory">
    		<bean class="org.activemq.ActiveMQConnectionFactory" init-method="start">
    			<property name="brokerURL" value="tcp://localhost:61616"/>
    		</bean>
    	</property>
    </bean>
    
    <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
    	<property name="connectionFactory">
    		<bean class="org.springframework.jms.connection.SingleConnectionFactory">
    			<property name="targetConnectionFactory">
    				<ref local="jmsFactory"/>
    			</property>
    		</bean>
    	</property>
    	<property name="defaultDestination">
    	    	<ref bean="dest"/>
    	</property>
    </bean>
    ActiveMq use postgres8.0.4,jdbc drivers is postgresql-8.0-314.jdbc3.jar
    Last edited by zjnbshifox; Nov 20th, 2005 at 09:37 PM.

  2. #2
    Join Date
    Jul 2005
    Posts
    24

    Default

    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?

  3. #3
    Join Date
    Aug 2004
    Location
    London
    Posts
    164

    Default

    Note that the JmsTemplate will create a connection, session, consumer then close them all down again each time. So you should never use JmsTemplate with a regular JMS ConnectionFactory - there should be massive warnings all over the javadoc of JmsTemplate as its only intended for use inside a J2EE application server along with an application servers JCA provider of the JMS ConnectionFactory.

    So I'd recommend using Message Driven POJOs with ActiveMQ instead...

    http://jencks.org/Message+Driven+POJOs

    If you absolutely insist on using JmsTemplate, which I don't recommend for consumption, then ensure you use Jencks to create a pooled version of the ConnectionFactory....

    http://jencks.org/Outbound+JMS
    James Strachan
    ------------------
    Open Source Integration
    Iona

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •