Skip to content

Instantly share code, notes, and snippets.

@garyrussell
Created May 23, 2013 21:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save garyrussell/5639657 to your computer and use it in GitHub Desktop.
Save garyrussell/5639657 to your computer and use it in GitHub Desktop.
Test Outbound RMI Error Handling
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:int-rmi="http://www.springframework.org/schema/integration/rmi"
xmlns:int="http://www.springframework.org/schema/integration"
xsi:schemaLocation="http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/integration/rmi http://www.springframework.org/schema/integration/rmi/spring-integration-rmi.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<int:gateway id="noErrorChannel"
service-interface="foo.Foo$FooService" default-request-channel="foo" />
<int:gateway id="withDefaultErrorChannel" error-channel="errorChannel" default-reply-timeout="2000"
service-interface="foo.Foo$FooService" default-request-channel="foo" />
<int:channel id="foo" />
<int-rmi:outbound-gateway remote-channel="bar" host="localhost" request-channel="foo"/>
<int-rmi:inbound-gateway request-channel="bar" />
<int:service-activator input-channel="bar">
<bean class="foo.Foo$RemoteService"/>
</int:service-activator>
</beans>
/*
* Copyright 2002-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 foo;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.fail;
import java.util.concurrent.atomic.AtomicReference;
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.core.MessageHandler;
import org.springframework.integration.core.SubscribableChannel;
import org.springframework.integration.message.ErrorMessage;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* @author Gary Russell
* @since 3.0
*
*/
@ContextConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
public class Foo {
@Autowired
private FooService noErrorChannel;
@Autowired
private FooService withDefaultErrorChannel;
@Autowired
private SubscribableChannel errorChannel;
@Test
public void testNoErrorChannel() {
try {
noErrorChannel.foo("baz");
fail("Expected exception");
}
catch (RuntimeException e) {
assertEquals("bar", e.getMessage());
}
}
@Test
public void testDefaultErrorChannel() {
final AtomicReference<ErrorMessage> em = new AtomicReference<ErrorMessage>();
errorChannel.subscribe(new MessageHandler() {
@Override
public void handleMessage(Message<?> message) throws MessagingException {
em.set((ErrorMessage) message);
}
});
assertNull(withDefaultErrorChannel.foo("baz"));
assertNotNull(em.get());
assertEquals("bar", em.get().getPayload().getCause().getMessage());
}
////////////////////////////////////////////////////////////
public interface FooService {
String foo(String foo);
}
public static class RemoteService {
public String foo(String foo) {
throw new RuntimeException("bar");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment