Announcement Announcement Module
Collapse
No announcement yet.
Exception handling Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Exception handling

    Hi guys,

    I'm a newbie to spring integration. Been using spring itself for sometime. I have a situation where I'm reading of a jms queue. I have read:
    http://forum.springsource.org/showth...t=errorChannel
    where it talks about things being within the same thread etc. I think I've understood this. I believe that for the current situation I'm doing everything within one thread.

    I am throwing an exception in the service-activator. I want that exception to cause a rollback onto the queue. This isn't happening and I"m not sure why. I hope someone can help me.

    NOTE: the unit test is feeding object properties, so the TestService class WILL throw the exception that is caught, in turn throwing the MessageHandlingException. I have tried throwing JmsException and other types of exception. All do the same thing, which is stop the flow, but the transaction is still commited.

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans:beans xmlns="http://www.springframework.org/schema/integration"
        xmlns:beans="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xmlns:jms="http://www.springframework.org/schema/integration/jms"
        xmlns:stream="http://www.springframework.org/schema/integration/stream"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
               http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
               http://www.springframework.org/schema/integration
               http://www.springframework.org/schema/integration/spring-integration-1.0.xsd
               http://www.springframework.org/schema/integration/jms
               http://www.springframework.org/schema/integration/jms/spring-integration-jms-1.0.xsd
               http://www.springframework.org/schema/integration/stream
               http://www.springframework.org/schema/integration/stream/spring-integration-stream-1.0.xsd">
    
        <beans:bean id="connectionFactory" class="org.springframework.jms.connection.CachingConnectionFactory">
            <beans:property name="targetConnectionFactory">
                <beans:bean class="org.apache.activemq.ActiveMQConnectionFactory">
                    <beans:property name="brokerURL" value="vm://localhost"/>     
                </beans:bean>
            </beans:property>
            <beans:property name="sessionCacheSize" value="10"/>
            <beans:property name="cacheProducers" value="false"/>
        </beans:bean>
    
        <poller id="poller" default="true">
            <interval-trigger interval="1000"/>
        </poller>
        
        <!-- The error-channel only comes into play if there is an asynchronous boundary - if the task is submitted 
             to a TaskExecutor along the way (e.g. when a channel with a 'queue' is used). However, that would also 
             break the transactional boundary that starts with the JMS message being received. 
             http://forum.springsource.org/showthread.php?t=76436&highlight=errorChannel -->
        <channel id="errorChannel">
    	    <queue capacity="500"/>
    	</channel>
        <stream:stderr-channel-adapter id="stdoutError" channel="errorChannel" append-newline="true" />
        
        
        <!-- INBOUND QUEUE HANDLER -->
        <channel id="requests"/>
        <jms:message-driven-channel-adapter
            connection-factory="connectionFactory" destination-name="Q.TEST1" channel="requests" 
            message-converter="messageConverter" transaction-manager="transactionManager" />
        <stream:stdout-channel-adapter id="stdout" channel="requests" append-newline="true"/>
        
        <!-- TransactionManager -->
        <!-- beans:bean id="jbossTransactionManager" class="com.arjuna.ats.internal.jta.transaction.arjunacore.TransactionManagerImple">
        </beans:bean>
        <beans:bean id="jbossUserTransaction" class="com.arjuna.ats.internal.jta.transaction.arjunacore.UserTransactionImple"/>
        
        <beans:bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager">
            <beans:property name="userTransaction" ref="jbossUserTransaction" />
            <beans:property name="transactionManager" ref="jbossTransactionManager" />
        </beans:bean -->
        
        <beans:bean id="transactionManager" class="org.springframework.jms.connection.JmsTransactionManager102">
            <beans:property name="connectionFactory" ref="connectionFactory" />
        </beans:bean>
        
        
        <!-- NOTE NOTE NOTE -->
        <!-- technically do not need to have a message converter, as by default it will convert to 
             org.springframework.integration.message.GenericMessage -->
        <beans:bean id="messageConverter" class="org.springframework.jms.support.converter.SimpleMessageConverter102" />
        
        <!-- TEST SERVICE - Print info about message -->
        <beans:bean id="TestService" class="com.hyro.springintegrationtest.testclasses.jmsrouter.TestService">
        </beans:bean>
        <service-activator input-channel="requests" output-channel="processedRequests" ref="TestService" method="printInfo" />
        <channel id="processedRequests" />
        
        <!-- OUTPUT TO QUEUE -->
        <!-- right, so we got it (it will have printed the body to the console - now put it into the other queue -->
        <jms:outbound-channel-adapter
            connection-factory="connectionFactory" destination-name="Q.TEST2" channel="processedRequests">
        </jms:outbound-channel-adapter>
    
    </beans:beans>
    The service class as it currently stands:
    Code:
    package com.hyro.springintegrationtest.testclasses.jmsrouter;
    
    import java.util.Iterator;
    import java.util.Set;
    
    import javax.jms.JMSException;
    
    import org.springframework.integration.core.Message;
    import org.springframework.integration.core.MessageHeaders;
    import org.springframework.integration.message.MessageHandlingException;
    
    /** 
     * Creation Details:
     * @author pedwards
     * Created: 23/02/2010
     * Time: 10:36:24 AM
     */
    public class TestService {
    
        /**
         * Print whatever info we can find about the received message
         * @param info
         * @return
         */
        public Message<?> printInfo(Message<?> info) throws Exception {
            try {
                System.out.println("*** TestService PrintInfo ***");
                System.out.println("info class type: " + info.getClass().getName());
                
                MessageHeaders headers = info.getHeaders();
                Set<String>keySet = headers.keySet();
                for(Iterator<String>i=keySet.iterator(); i.hasNext();) {
                    String headerKey = i.next();
                    //Object headerValue = headers.get(headerKey);
                    String headerValue = (String)headers.get(headerKey);
                    System.out.println("Header: '" + headerKey + "' value: '" + headerValue + "'");
                }
                
                System.out.println("Contents: '" + info.getPayload() + "'");
            }
            catch(Exception e) {
                //System.out.println("Exception thrown during PrintInfo: " + e.getMessage());
                //e.printStackTrace();
                //lets kill the process
                throw new MessageHandlingException(info, "Exception thrown during PrintInfo: " + e.getMessage());
            }
            return info;
        }
    
    }
Working...
X