Permalink
Browse files

SWITCHYARD-1950 for IN_ONLY, ClientProxyBean and camel OutboundHandle…

…r doesn't propagate a fault to the consumer
  • Loading branch information...
1 parent d6a5290 commit 4a784f7ba701bc64886862d21daf0a11ea855f82 @igarashitm igarashitm committed with rcernich Jan 30, 2014
@@ -298,22 +298,7 @@ public Object invoke(Object proxy, Method method, Object[] args) throws Throwabl
if (exchangeOut.getState() == ExchangeState.OK) {
return exchangeOut.getMessage().getContent(method.getReturnType());
} else {
- Object exceptionObj = exchangeOut.getMessage().getContent();
-
- if (exceptionObj instanceof Throwable) {
- if (exceptionObj instanceof BeanComponentException) {
- BeanComponentException beanCompException = (BeanComponentException) exceptionObj;
- Throwable cause = beanCompException.getCause();
- if (cause instanceof InvocationTargetException) {
- throw cause.getCause();
- } else {
- throw cause;
- }
- }
- throw (Throwable) exceptionObj;
- } else {
- throw BeanMessages.MESSAGES.beanComponentInvocationFailureServiceOperation(_serviceName, method.getName()).setFaultExchange(exchangeOut);
- }
+ return handleException(exchangeOut, method);
}
} else {
Exchange exchange = createExchange(_service, method, null);
@@ -326,6 +311,11 @@ public Object invoke(Object proxy, Method method, Object[] args) throws Throwabl
exchange.send(exchange.createMessage().setContent(args));
}
+ Object propagateException = _service.getDomain().getProperty(Exchange.PROPAGATE_EXCEPTION_ON_IN_ONLY);
+ if (propagateException != null && Boolean.parseBoolean(propagateException.toString())
+ && exchange.getState().equals(ExchangeState.FAULT)) {
+ handleException(exchange, method);
+ }
return null;
}
}
@@ -339,5 +329,23 @@ private Exchange createExchange(ServiceReference service, Method method, Exchang
return service.createExchange(operationName, responseExchangeHandler);
}
+ private Throwable handleException(Exchange exchange, Method method) throws Throwable {
+ Object exceptionObj = exchange.getMessage().getContent();
+ if (exceptionObj instanceof Throwable) {
+ if (exceptionObj instanceof BeanComponentException) {
+ BeanComponentException beanCompException = (BeanComponentException) exceptionObj;
+ Throwable cause = beanCompException.getCause();
+ if (cause instanceof InvocationTargetException) {
+ throw cause.getCause();
+ } else {
+ throw cause;
+ }
+ }
+ throw (Throwable) exceptionObj;
+ } else {
+ throw BeanMessages.MESSAGES.beanComponentInvocationFailureServiceOperation(_serviceName, method.getName()).setFaultExchange(exchange);
+ }
+ }
+
}
}
@@ -14,5 +14,9 @@
<entry name="testQueue"/>
<durable>true</durable>
</queue>
-
-</configuration>
+ <queue name="GreetingServiceQueue">
+ <entry name="GreetingServiceQueue"/>
+ <durable>false</durable>
+ </queue>
+</configuration>
+
@@ -58,6 +58,7 @@
private final String _bindingName;
private final String _referenceName;
private final String _uri;
+ private final ServiceDomain _domain;
/**
* Constructor that will create a default {@link ProducerTemplate}.
@@ -82,6 +83,7 @@ public OutboundHandler(final CamelBindingModel binding, final SwitchYardCamelCon
*/
public OutboundHandler(final CamelBindingModel binding, final SwitchYardCamelContext context, MessageComposer<CamelBindingData> messageComposer, ProducerTemplate producerTemplate, ServiceDomain domain) {
super(domain);
+ _domain = domain;
if (binding == null) {
throw CommonCamelMessages.MESSAGES.bindingArgumentMustNotBeNull();
}
@@ -152,7 +154,17 @@ private boolean isInOnly(final Exchange exchange) {
private void handleInOnly(final Exchange exchange) throws HandlerException {
try {
- _producerTemplate.send(_uri, createProcessor(exchange));
+ final org.apache.camel.Exchange camelExchange = _producerTemplate.send(_uri, createProcessor(exchange));
+ Object propagateException = _domain.getProperty(Exchange.PROPAGATE_EXCEPTION_ON_IN_ONLY);
+ if (propagateException != null && Boolean.parseBoolean(propagateException.toString())
+ && camelExchange.isFailed()) {
+ Exception camelException = camelExchange.getException();
+ if (camelException != null) {
+ throw new HandlerException(camelException);
+ } else {
+ throw CommonCamelMessages.MESSAGES.camelExchangeFailedWithoutAnException("");
+ }
+ }
} catch (final CamelExecutionException e) {
throw new HandlerException(e);
}
@@ -100,7 +100,7 @@ public void setupSwitchyardService() {
_serviceDomain.registerService(_targetService.getServiceName(),
new InOnlyService(),
new OutboundHandler(bindingModel,
- (SwitchYardCamelContext) context, _messageComposer, null
+ (SwitchYardCamelContext) context, _messageComposer, _serviceDomain
) {
{
setState(State.STARTED);
View
@@ -49,6 +49,10 @@
</dependency>
<dependency>
<groupId>org.switchyard.components</groupId>
+ <artifactId>switchyard-component-camel-jms</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.switchyard.components</groupId>
<artifactId>switchyard-component-test-mixin-cdi</artifactId>
<scope>test</scope>
</dependency>
@@ -0,0 +1,107 @@
+/*
+ * Copyright 2013 Red Hat Inc. and/or its affiliates and other contributors.
+ *
+ * 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.switchyard.component.itests.fault;
+
+import junit.framework.Assert;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.switchyard.Message;
+import org.switchyard.test.Invoker;
+import org.switchyard.test.ServiceOperation;
+import org.switchyard.test.SwitchYardRunner;
+import org.switchyard.test.SwitchYardTestCaseConfig;
+import org.switchyard.component.test.mixins.cdi.CDIMixIn;
+import org.switchyard.component.test.mixins.hornetq.HornetQMixIn;
+import org.switchyard.component.test.mixins.transaction.TransactionMixIn;
+
+/**
+ * Functional test for a fault propagation from camel outbound binding.
+ *
+ */
+@RunWith(SwitchYardRunner.class)
+@SwitchYardTestCaseConfig(
+ config = "switchyard-camel-outbound-fault-test.xml",
+ mixins = { CDIMixIn.class, TransactionMixIn.class, HornetQMixIn.class })
+public class CamelOutboundFaultPropagationTest {
+ @ServiceOperation("GreetingService.greet")
+ private Invoker _greet;
+ @ServiceOperation("StoreService.store")
+ private Invoker _store;
+
+ @Test
+ public void testReferenceBindingInOut() throws Exception {
+ final String payload = "store/IN_OUT";
+ Message msg = null;
+ try {
+ msg = _store.sendInOut(payload);
+ } catch (Throwable t) {
+ System.out.println("Received an Exception: " + formatThrowable(t));
+ return;
+ }
+ Assert.fail("It succeeded while an Exception is expected: " + msg.getContent());
+ }
+
+ @Test
+ public void testReferenceBindingInOnly() throws Exception {
+ final String payload = "store/IN_ONLY";
+ try {
+ _store.sendInOnly(payload);
+ } catch (Throwable t) {
+ System.out.println("Received an Exception: " + formatThrowable(t));
+ return;
+ }
+ Assert.fail("It succeeded while an Exception is expected");
+ }
+
+ @Test
+ public void testBeanServiceInOut() throws Exception {
+ final String payload = "greet/IN_OUT";
+ Message msg = null;
+ try {
+ msg = _greet.sendInOut(payload);
+ } catch (Throwable t) {
+ System.out.println("Received an Exception: " + formatThrowable(t));
+ return;
+ }
+ Assert.fail("It succeeded while an Exception is expected: " + msg.getContent());
+ }
+
+ @Test
+ public void testBeanServiceInOnly() throws Exception {
+ final String payload = "greet/IN_ONLY";
+ try {
+ _greet.sendInOnly(payload);
+ } catch (Throwable t) {
+ System.out.println("Received an Exception: " + formatThrowable(t));
+ return;
+ }
+ Assert.fail("It succeeded while an Exception is expected");
+ }
+
+ private String formatThrowable(Throwable t) {
+ StringBuilder buf = new StringBuilder(t.getClass().getName())
+ .append(" : ")
+ .append(t.getMessage());
+ Throwable cause = t;
+ while((cause = cause.getCause()) != null) {
+ buf.append(" -- ")
+ .append(cause.getClass().getName())
+ .append(" : ")
+ .append(cause.getMessage());
+ }
+ return buf.toString();
+ }
+}
+
@@ -0,0 +1,29 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2013, Red Hat, Inc. and/or its affiliates, and individual
+ * contributors by the @authors tag. See the copyright.txt in the
+ * distribution for a full listing of individual contributors.
+ *
+ * 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.switchyard.component.itests.fault;
+
+/**
+ * Simple interface for a Greeter Service.
+ */
+public interface GreetingService {
+ /**
+ * Prints a greeting message using the passed in name.
+ *
+ * @param name the name of the person to be greeted.
+ */
+ void greet(String name);
+}
@@ -0,0 +1,40 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2013, Red Hat, Inc. and/or its affiliates, and individual
+ * contributors by the @authors tag. See the copyright.txt in the
+ * distribution for a full listing of individual contributors.
+ *
+ * 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.switchyard.component.itests.fault;
+
+import javax.inject.Inject;
+
+import org.switchyard.component.bean.Reference;
+import org.switchyard.component.bean.Service;
+
+/**
+ * A POJO Service implementation.
+ *
+ */
+@Service(GreetingService.class)
+public class GreetingServiceBean implements GreetingService {
+
+ @Inject @Reference
+ private StoreService _store;
+
+ @Override
+ public void greet(final String name) {
+ String greet = "Hello there " + name + " :-) ";
+ System.out.println(greet);
+ _store.store(greet);
+ }
+}
@@ -0,0 +1,29 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2013, Red Hat, Inc. and/or its affiliates, and individual
+ * contributors by the @authors tag. See the copyright.txt in the
+ * distribution for a full listing of individual contributors.
+ *
+ * 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.switchyard.component.itests.fault;
+
+/**
+ * Simple interface for a Store Service.
+ */
+public interface StoreService {
+ /**
+ * Store a greeting message using the passed in name.
+ *
+ * @param name the name of the person to be greeted.
+ */
+ void store(String name);
+}
@@ -0,0 +1,25 @@
+<configuration xmlns="urn:hornetq">
+
+ <paging-directory>target/data/paging</paging-directory>
+ <bindings-directory>target/data/bindings</bindings-directory>
+ <persistence-enabled>false</persistence-enabled>
+ <journal-directory>target/data/journal</journal-directory>
+ <journal-min-files>10</journal-min-files>
+ <large-messages-directory>target/data/large-messages</large-messages-directory>
+ <security-enabled>false</security-enabled>
+
+ <connectors>
+ <connector name="invm-connector">
+ <factory-class>org.hornetq.core.remoting.impl.invm.InVMConnectorFactory</factory-class>
+ </connector>
+ </connectors>
+
+ <acceptors>
+ <acceptor name="invm-acceptor">
+ <factory-class>org.hornetq.core.remoting.impl.invm.InVMAcceptorFactory</factory-class>
+ </acceptor>
+ </acceptors>
+
+ <queues/>
+
+</configuration>
@@ -0,0 +1,24 @@
+<configuration xmlns="urn:hornetq">
+
+ <connection-factory name="ConnectionFactory">
+ <connectors>
+ <connector-ref connector-name="invm-connector"/>
+ </connectors>
+
+ <entries>
+ <entry name="ConnectionFactory"/>
+ </entries>
+ </connection-factory>
+
+ <queue name="GreetingServiceQueue">
+ <entry name="GreetingServiceQueue"/>
+ <durable>false</durable>
+ </queue>
+
+<!--
+ <queue name="StoreQueue">
+ <entry name="StoreQueue"/>
+ <durable>false</durable>
+ </queue>
+-->
+</configuration>
Oops, something went wrong.

0 comments on commit 4a784f7

Please sign in to comment.