Refactored error-handling demo to use a chain with a transformer instead of the channel-interceptor.

This commit is contained in:
Mark Fisher
2008-12-17 13:25:05 +00:00
parent 750cf7a9b7
commit 355a8e70e9
6 changed files with 40 additions and 37 deletions

View File

@@ -12,18 +12,24 @@
* 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;
import org.springframework.integration.annotation.Transformer;
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 {
/**
* @author Iwein Fuld
*/
@MessageEndpoint
public class ErrorUnwrapper {
public Message<?> transform(Message<?> message) {
Assert.isAssignable(ErrorMessage.class, message.getClass());
return ((MessagingException) ((ErrorMessage) message).getPayload()).getFailedMessage();
@Transformer
public Message<?> transform(ErrorMessage errorMessage) {
return ((MessagingException) errorMessage.getPayload()).getFailedMessage();
}
}

View File

@@ -12,8 +12,12 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.integration.samples.errorhandling;
/**
* @author Iwein Fuld
*/
public class Invitation {
private final int number;
@@ -26,4 +30,5 @@ public class Invitation {
public String toString() {
return "Invitation number " + number;
}
}

View File

@@ -12,18 +12,18 @@
* 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;
/**
* @author Iwein Fuld
*/
public class PartyDemo {
/**
* @param args
*/
public static void main(String[] args) {
new ClassPathXmlApplicationContext("errorHandlingDemo.xml", PartyDemo.class).start();
new ClassPathXmlApplicationContext("errorHandlingDemo.xml", PartyDemo.class);
}
}

View File

@@ -12,6 +12,7 @@
* 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;
@@ -21,12 +22,13 @@ import org.springframework.integration.annotation.MessageEndpoint;
* 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

@@ -12,6 +12,7 @@
* 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;
@@ -19,19 +20,18 @@ 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.
* Sends invitations to <code>PartyGuest</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);
private final AtomicInteger counter = new AtomicInteger(0);
public Invitation nextInvitation() {
return new Invitation(counter.getAndIncrement());
return new Invitation(counter.incrementAndGet());
}
public void onInvitationFailed(Invitation inv) {

View File

@@ -11,15 +11,14 @@
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">
<context:component-scan base-package="org.springframework.integration.samples.errorhandling" />
<poller default="true" max-messages-per-poll="1">
<interval-trigger interval="1000" />
</poller>
<inbound-channel-adapter ref="partyHost"
auto-startup="true" method="nextInvitation" channel="invitations" />
<inbound-channel-adapter ref="partyHost" method="nextInvitation" channel="invitations" />
<channel id="invitations">
<queue capacity="100" />
@@ -30,22 +29,13 @@
<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>
<channel id="failed-invitations" />
<service-activator ref="partyHost" method="onInvitationFailed"
input-channel="failed-invitations" />
<chain input-channel="failed-invitations">
<transformer ref="errorUnwrapper" />
<service-activator ref="partyHost" method="onInvitationFailed" />
</chain>
<stream:stderr-channel-adapter channel="errorChannel"
append-newline="true" />
<stream:stderr-channel-adapter channel="errorChannel" append-newline="true" />
</beans:beans>