jeudi 20 février 2020

Camel 3: How to intercept an error route that is sent to by onException

During the migration from Camel 2 to 3, my error routing tests broke. I force an exception and ensure that errorHandlingRoute receives the exchange.

myRoute -> onException -> errorHandlingRoute

The pattern that worked successfully in Camel 2 was as follows:

// ROUTE
val errorUri = "direct:all_errors"

onException(Exception::class.java)
    .to(errorUri)

from(errorUri)
  .routeId(errorUri)
  .process(...)


val uri = "direct:start"
from(uri)
  .routeId(uri) // use the same value for both
  .process(processorThatWillBeMockedToThrow)
  .to(...)
@Test
fun `exception is routed to error logging route`() {
    val exchange = createExchangeWithBody(""))
    val startUri = "direct:start"

    // Force error
    whenever(mockProcessor.process(exchange)).thenThrow(Exception())

    // Intercept error route to check that it was hit
    val mockEndpoint = getMockEndpoint("mock:for-assertions")

    val advice = object : RouteBuilder() {
        override fun configure() {
            interceptSendToEndpoint("direct:all_errors")
                .skipSendToOriginalEndpoint()
                .to(mockEndpoint)
        }
    }
    context.getRouteDefinition(startUri)
        .adviceWith(this, advice)


    // Expect intercepted error endpoint to have been hit
    mockEndpoint.expectedMessageCount(1)

    template.send(queueUri, exchange)

    assertMockEndpointsSatisfied()
}

After updating to Camel 3 and using the following for the intercept, the expectedMessageCount assertion is failing because it is receiving zero messages.

AdviceWithRouteBuilder.adviceWith(this, "direct:start") { routeBuilder ->
    routeBuilder.interceptSendToEndpoint("direct:all_errors")
        .skipSendToOriginalEndpoint()
        .to(redirectTo)
}

Are routes no longer connected in the same way in Camel 3? How can I intercept a route that is sent to from an onException block?

Aucun commentaire:

Enregistrer un commentaire