*So here is my configure now:*
c*amelContext.addRoutes(new RouteBuilder() {
public void configure() {
exception(Exception.class).process(changeRequestInputExceptionProcessor).to(MOCK_OUTPUT_ERROR_DESTINATION_URI);
errorHandler(deadLetterChannel(MOCK_OUTPUT_ERROR_DESTINATION_URI));
// create the routes for async-processing:
from(inputUri).process(processor).to(outputUri);
/*exception(Exception.class)
.setHeader("MESSAGE_INFO", constant("Damm just
exception"))
.to(MOCK_OUTPUT_ERROR_DESTINATION_URI);*/
}
});
*
So when I run my test with bad data, my console shows the following error:
[myproject] INFO [DefaultMessageListenerContainer-1]
ChangeRequestControllerImpl.process(31) |
ChangeRequestControllerImpl.process(Exchange)
*[myproject] INFO [DefaultMessageListenerContainer-1]
ChangeRequestControllerImpl.process(82) |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XX*
[myproject] DEBUG [DefaultMessageListenerContainer-1] Pipeline.process(67) |
Message exchange has failed so breaking out of pipeline: Exchange[JmsMess
age: ActiveMQTextMessage {commandId = 5, responseRequired = true, messageId
= ID:mickknutson-1489-1222285987396-2:6:1:1:1, originalDestination = null,
originalTransactionId = null, producerId =
ID:mickknutson-1489-1222285987396-2:6:1:1, destination =
queue://channel/changerequest/add, transactionId
= null, expiration = 0, timestamp = 1222285992458, arrival = 0, brokerInTime
= 1222285992458, brokerOutTime = 1222285992458, correlationId = null, rep
lyTo = null, persistent = true, type = null, priority = 4, groupID = null,
groupSequence = 0, targetConsumerId = null, compressed = false, userID = nu
ll, content = null, marshalledProperties = null, dataStructure = null,
redeliveryCounter = 0, size = 1024, properties = null, readOnlyProperties =
tru
e, readOnlyBody = true, droppable = false, text = {"customerId":"*abcd*
","changeRequestType":"BADREQUEST","quota":"*xyz*"}}] exception:
java.lang.Exceptio
n: org.codehaus.jettison.json.JSONException: JSONObject["customerId"] is not
a number. fault: null
[myproject] WARN [DefaultMessageListenerContainer-1]
EndpointMessageListener.onMessage(80) |
Endpoint[activemq:queue:channel/changerequest/add] consum
er caught an exception while processing JMS message: ActiveMQTextMessage
{commandId = 5, responseRequired = true, messageId =
ID:mickknutson-1489-1222
285987396-2:6:1:1:1, originalDestination = null, originalTransactionId =
null, producerId = ID:mickknutson-1489-1222285987396-2:6:1:1, destination =
q
ueue://channel/changerequest/add, transactionId = null, expiration = 0,
timestamp = 1222285992458, arrival = 0, brokerInTime = 1222285992458,
brokerOu
tTime = 1222285992458, correlationId = null, replyTo = null, persistent =
true, type = null, priority = 4, groupID = null, groupSequence = 0, targetCo
nsumerId = null, compressed = false, userID = null, content = null,
marshalledProperties = null, dataStructure = null, redeliveryCounter = 0,
size = 1
024, properties = null, readOnlyProperties = true, readOnlyBody = true,
droppable = false, text =
{"customerId":"abcd","changeRequestType":"BADREQUEST
","quota":"xyz"}}
org.apache.camel.RuntimeCamelException: java.lang.Exception:
org.codehaus.jettison.json.JSONException: JSONObject["customerId"] is not a
number.
at
org.apache.camel.component.jms.EndpointMessageListener.onMessage(EndpointMessageListener.java:71)
at
org.springframework.jms.listener.AbstractMessageListenerContainer.doInvokeListener(AbstractMessageListenerContainer.java:531)
at
org.springframework.jms.listener.AbstractMessageListenerContainer.invokeListener(AbstractMessageListenerContainer.java:466)
at
org.springframework.jms.listener.AbstractMessageListenerContainer.doExecuteListener(AbstractMessageListenerContainer.java:435)
at
org.springframework.jms.listener.AbstractPollingMessageListenerContainer.doReceiveAndExecute(AbstractPollingMessageListenerContainer.java:3
22)
at
org.springframework.jms.listener.AbstractPollingMessageListenerContainer.receiveAndExecute(AbstractPollingMessageListenerContainer.java:260
)
at
org.springframework.jms.listener.DefaultMessageListenerContainer$AsyncMessageListenerInvoker.invokeListener(DefaultMessageListenerContainer
.java:944)
at
org.springframework.jms.listener.DefaultMessageListenerContainer$AsyncMessageListenerInvoker.run(DefaultMessageListenerContainer.java:875)
at java.lang.Thread.run(Thread.java:619)
Caused by: java.lang.Exception: org.codehaus.jettison.json.JSONException:
JSONObject["customerId"] is not a number.
at
com.servepath.gogrid.changerequest.impl.ChangeRequestControllerImpl.process(ChangeRequestControllerImpl.java:83)
*Now here is the method itself:*
public void process(Exchange exchange)
throws Exception {
try {
String body = (String) exchange.getIn().getBody();
JSONObject jsonIn = new JSONObject(body);
// validate input message, route to verifyErrorEndpoint if there
are errors.
Long customerId = jsonIn.getLong(Constants.CUSTOMER_ID);
String type = jsonIn.getString(Constants.CHANGEREQUST_TYPE);
int quota = jsonIn.getInt(Constants.QUOTA);
ChangeRequest changeRequest = null;
if (type.equalsIgnoreCase(Constants.PROVISION)) {
log.info("***** PROVISION *****");
changeRequest = createProvisionChangeRequest(customerId,
quota); // can throw Exception
exchange.getOut().setHeader(Constants.REQUEST_DESTINATION,
Constants.REQUEST_OUTPUT_CHANNEL);
} else if (type.equalsIgnoreCase(Constants.DEPROVISION)) {
log.info("DEPROVISION");
} else {
log.error("Invalid Change Request");
exchange.getOut().setHeader(Constants.REQUEST_DESTINATION,
Constants.REQUEST_INPUT_ERROR_CHANNEL);
throw new Exception("Invalid Change Request Type");
}
String jsonOut = toJSON(body);
exchange.getOut().setMessageId(changeRequest.getChangeRequestId() + "");
exchange.getOut().setHeader(Constants.CORRELATION_ID,
changeRequest.getChangeRequestId());
exchange.getOut().setHeader(Constants.TIMEOUT, new
Integer(5000)); // fixme: 5 seconds....
exchange.getOut().setHeader(Constants.CUSTOMER_ID,
changeRequest.getCustomerId());
exchange.getOut().setBody(jsonOut);
} catch (Exception e) {
log.info("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
throw new Exception(e);
}
}
*Here is my test method:*
@Test(groups = {"functional"})
@Parameters({"inputDestinationURI", "messageInputBody",
"messageOutputBody"})
public void testCreateInvalidChangeRequest(String inputDestinationURI,
String messageInputBody,
String messageOutputBody
)
throws Exception {
*resultEndpoint.expectedMessageCount(0);
resultErrorEndpoint.expectedMessageCount(1);*
// setup RouteBuilder...
setRoutes(inputDestinationURI, changeRequestControllerImpl,
MOCK_OUTPUT_DESTINATION_URI);
// Create and Send message to input queue
createMessage(inputDestinationURI, messageInputBody);
MockEndpoint.assertIsSatisfied(resultEndpoint, resultErrorEndpoint);
// verify that the destination channel
log.debug("----------------------------------------------------");
}
*But now my test assertion fails:*
*<exception class="java.lang.AssertionError">
<message>
<![CDATA[mock:outputDestinationURI Received message
count. Expected:
<0> but was: <1>]]>
</message>*