Skip to content

Instantly share code, notes, and snippets.

@artembilan
Created November 13, 2013 15:21
Show Gist options
  • Save artembilan/7450789 to your computer and use it in GitHub Desktop.
Save artembilan/7450789 to your computer and use it in GitHub Desktop.
Propagation an Exception from Executor Channel to Gateway's error-channel's handler
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/integration"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd">
<task:executor id="executor" pool-size="5"/>
<gateway id="gateway" service-interface="org.springframework.integration.gateway.GatewayErrorPropagationTests$Gateway"
default-request-channel="myChannel"
error-channel="myErrorChannel"/>
<channel id="myChannel">
<dispatcher task-executor="executor"/>
</channel>
<service-activator input-channel="myChannel">
<beans:bean class="org.springframework.integration.gateway.GatewayErrorPropagationTests$Service"/>
</service-activator>
<transformer input-channel="myErrorChannel">
<beans:bean class="org.springframework.integration.gateway.GatewayErrorPropagationTests$ErrorHandler"/>
</transformer>
</beans:beans>
/*
* Copyright 2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.integration.gateway;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import org.hamcrest.Matchers;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.integration.Message;
import org.springframework.integration.MessagingException;
import org.springframework.integration.message.ErrorMessage;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* @author Artem Bilan
* @since 3.0
*/
@ContextConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
public class GatewayErrorPropagationTests {
@Autowired
private Gateway gateway;
@Test
public void testIt() {
String payload = "foo";
String result = this.gateway.service(payload);
assertEquals(payload, result);
}
public static interface Gateway {
public String service(String payload);
}
public static class Service {
public String service(String payload) {
throw new RuntimeException("Intentional");
}
}
public static class ErrorHandler {
public Object handle(Message<?> message) {
assertThat(message, Matchers.instanceOf(ErrorMessage.class));
Throwable throwable = ((ErrorMessage) message).getPayload();
assertThat(throwable, Matchers.instanceOf(MessagingException.class));
return ((MessagingException) throwable).getFailedMessage().getPayload();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment