OPEN - issue INT-496: Design success/failure notifications for source adapters

http://jira.springframework.org/browse/INT-496

Added sample to show error handling
This commit is contained in:
Iwein Fuld
2008-12-15 19:29:23 +00:00
parent 783861945d
commit caa93b4a67
6 changed files with 211 additions and 0 deletions

View File

@@ -0,0 +1,29 @@
/* Copyright 2002-2008 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.samples.errorhandling;
public class Invitation {
private final int number;
public Invitation(int number) {
this.number = number;
}
@Override
public String toString() {
return "Invitation number " + number;
}
}

View File

@@ -0,0 +1,29 @@
/* Copyright 2002-2008 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.samples.errorhandling;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class PartyDemo {
/**
* @param args
*/
public static void main(String[] args) {
new ClassPathXmlApplicationContext("errorHandlingDemo.xml", PartyDemo.class).start();
}
}

View File

@@ -0,0 +1,32 @@
/* Copyright 2002-2008 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.samples.errorhandling;
import org.springframework.integration.annotation.MessageEndpoint;
/**
* This guest receives invitations, but returns them to sender with a wrong
* address exception message.
*
* @author Iwein Fuld
*
*/
@MessageEndpoint
public class PartyGuest {
public void onInvitation(Invitation invitation) {
System.out.println("Guest is throwing exception");
throw new RuntimeException("Wrong address");
}
}

View File

@@ -0,0 +1,41 @@
/* Copyright 2002-2008 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.samples.errorhandling;
import java.util.concurrent.atomic.AtomicInteger;
import org.springframework.integration.annotation.MessageEndpoint;
/**
* Sends invitations to <code>Guest</code>s and likes to be notified of delivery
* failures of course.
*
* @author Iwein Fuld
*
*/
@MessageEndpoint
public class PartyHost {
private final AtomicInteger counter = new AtomicInteger(1);
public Invitation nextInvitation() {
return new Invitation(counter.getAndIncrement());
}
public void onInvitationFailed(Invitation inv) {
System.out.println("Host received failure notification for: " + inv);
}
}

View File

@@ -0,0 +1,29 @@
/* Copyright 2002-2008 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.samples.errorhandling;
import org.springframework.integration.core.Message;
import org.springframework.integration.core.MessagingException;
import org.springframework.integration.message.ErrorMessage;
import org.springframework.integration.transformer.Transformer;
import org.springframework.util.Assert;
public class UnwrappingErrorMessageTransformer implements Transformer {
public Message<?> transform(Message<?> message) {
Assert.isAssignable(ErrorMessage.class, message.getClass());
return ((MessagingException) ((ErrorMessage) message).getPayload()).getFailedMessage();
}
}

View File

@@ -0,0 +1,51 @@
<?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:context="http://www.springframework.org/schema/context"
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/context
http://www.springframework.org/schema/context/spring-context-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/stream
http://www.springframework.org/schema/integration/stream/spring-integration-stream-1.0.xsd">
<context:component-scan
base-package="org.springframework.integration.samples.errorhandling" />
<poller id="jack" default="true">
<interval-trigger interval="1000" />
</poller>
<inbound-channel-adapter ref="partyHost"
auto-startup="true" method="nextInvitation" channel="invitations" />
<channel id="invitations">
<queue capacity="100" />
</channel>
<chain input-channel="invitations">
<header-enricher error-channel="failed-invitations" />
<service-activator ref="partyGuest" method="onInvitation" />
</chain>
<channel id="failed-invitations">
<interceptors>
<beans:bean
class="org.springframework.integration.transformer.MessageTransformingChannelInterceptor">
<beans:constructor-arg>
<beans:bean
class="org.springframework.integration.samples.errorhandling.UnwrappingErrorMessageTransformer" />
</beans:constructor-arg>
</beans:bean>
</interceptors>
</channel>
<service-activator ref="partyHost" method="onInvitationFailed"
input-channel="failed-invitations" />
<stream:stderr-channel-adapter channel="errorChannel"
append-newline="true" />
</beans:beans>