title: How can I define a failover spring integration gateway?
body: <p>I'm fairly new to Spring Integration. I have a channel that is wired up to a JMS queue. I then have a gateway defined that has that channel setup as the default-request-channel. </p>
  
  <p>This basic configuration is working flawlessly for me. The defined service-interface is being injected into my bean and I'm able to send a message through this interface and it goes all the way through to the queue.</p>
  
  <p>However, I have an additional requirement to have a failover queue defined that if for some reason the primary is unreachable, that an attempt will be made to send to the failover/secondary queue.</p>
  
  <p>I am not finding any examples of this type of configuration.</p>
  
  <p>Since my channels are wired to JMS queues, my first thought was to create another connection factory (pointing to the secondary JMS server), create another queue bean and channel and then have another gateway that would be used if an exception is thrown from the first gateway invocation.</p>
  
  <pre><code>try {
    primaryGateway.sendMessage(message);
  } catch (Exception e) {
    secondaryGateway.sendMessage(message);
  }
  </code></pre>
  
  <p>However, I began to wonder if there was a built-in (under-the-covers) way that spring integration could be configured to autmatically try a secondary queue if the send to the primary fails.</p>
  
  <p>Also, I had a problem trying to define a secondary connection factory. I'm not sure how to tell the queue beans (or their owning channels) which connection factory to use. If I did not use the default "connectionFactory" id, it was complaining.</p>
  
  <p>Here is the key portions of my configuration with just the single queue/channel/gateway defined:</p>
  
  <pre><code>&lt;bean id="mqConnectionFactory" class="com.ibm.mq.jms.MQQueueConnectionFactory"&gt;
    &lt;property name="transportType" value="1"/&gt;
    &lt;property name="hostName" value="${lsm.primary.sch.outbound.host}"/&gt;
    &lt;property name="port" value="${lsm.primary.sch.outbound.port}"/&gt;
    &lt;property name="queueManager" value="${lsm.primary.sch.outbound.manager}"/&gt;
    &lt;property name="channel" value="${lsm.primary.sch.outbound.channel}"/&gt;
  &lt;/bean&gt;
  
  &lt;bean id="connectionFactory"
    class="org.springframework.jms.connection.UserCredentialsConnectionFactoryAdapter"&gt;
    &lt;property name="targetConnectionFactory" ref="mqConnectionFactory" /&gt;
    &lt;property name="username" value="blah" /&gt;
    &lt;property name="password" value="blah" /&gt;
  &lt;/bean&gt;
  
  &lt;bean id="lsmScheduleActualOutboundQueue" class="com.ibm.mq.jms.MQQueue"&gt;
    &lt;property name="baseQueueManagerName" value="${lsm.primary.sch.outbound.manager}"/&gt;
    &lt;property name="baseQueueName" value="${lsm.primary.sch.outbound.queue}"/&gt;
  &lt;/bean&gt;
  
  &lt;int:channel id="lsmScheduleActualOutboundChannel" /&gt;
  
  &lt;jms:outbound-channel-adapter id="jmsOutboundAdapter" 
    channel="lsmScheduleActualOutboundChannel" 
    destination="lsmScheduleActualOutboundQueue" /&gt;
  
  
  &lt;int:gateway id="lsmScheduleActualOutboundGateway" 
    service-interface="com.myapp.service.LSMScheduleActualsGateway"
    default-request-channel="lsmScheduleActualOutboundChannel" 
    default-request-timeout="1000" /&gt;
  </code></pre>