DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Building REST API Backend Easily With Ballerina Language
  • Composite Requests in Salesforce Are a Great Idea
  • Understanding the Fan-Out/Fan-In API Integration Pattern
  • How To REST With Rails and ActiveResource: Part Three

Trending

  • Evaluating RAG in Production — What the Numbers Actually Revealed
  • Three Measured Outcomes of Manually Scaling Fleet in Prod
  • Every Acquisition Promises One Company. Most Deliver Two.
  • Why Your GPU Fleet Is Both Full and Idle: Building a Capacity Orchestration Layer

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Data Engineering
  3. Databases
  4. First Successful Router in Mule

First Successful Router in Mule

Learn how to set up message processing in Mule flows with the First Successful router, and how to customize its behavior.

By 
Ankit Lawaniya
·
Sep. 08, 17 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
12.5K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

First successful is one of the flow control components in Mule which iterates through message processors until one succeeds.

The First Successful message processor iterates through its list of child message processors, routing a received message to each of them in order until one processes the message successfully. If none succeed, an exception is thrown.

Defining success and failure:

  • If the child message processor throws an exception, this is a failure.

  • Otherwise:

    • If the child message processor returns a message that contains an exception payload, this is a failure.

    • If the child message processor returns a message that does not contain an exception payload, this is a success.

    • If the child message processor does not return a message (e.g. is a one-way connector), this is a success.

    • If the child message processor does not return a message (e.g. is a one-way connector), this is a success.

Defining failureExpression

We can further customize the behavior of this router by specifying a 'failureExpression' that allows you to use Mule Expressions to define a failure. The failureExpression attribute is configured as follows:

Syntax:

<first-successful failureExpression="exception-type:java.sql.SQLException">   
</first-successful>

A failure expression is being used to more exactly define the exception type that’s considered a failure.

Let’s walk through how the First Successful router works in a Mule application.

In this example, we have defined two routes; in both of the routes, we are connecting with HSQL databases. One of the routes is configured with the correct DB configuration "Generic_Database_Configuration_success," and the other with the wrong DB configuration "Generic_Database_Configuration_failed," which is configured to connect to HSQL DB on port 9002. After making a request through a REST client, the second route configured will be able to process the message successfully.

If none of the routes is able to process, then the below exception will be thrown:

Root Exception stack trace:

org.mule.api.routing.CouldNotRouteOutboundMessageException: Failed to route event via endpoint: org.mule.routing.FirstSuccessful@79baf566.

    at org.mule.routing.FirstSuccessful.route(FirstSuccessful.java:58)

Flow:

Image title

Code:

<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:db="http://www.mulesoft.org/schema/mule/db"
xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns="http://www.mulesoft.org/schema/mule/core"
xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
xmlns:spring="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.mulesoft.org/schema/mule/db http://www.mulesoft.org/schema/mule/db/current/mule-db.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd">
<http:listener-config name="HTTP_Listener_Configuration"
host="0.0.0.0" port="8081" basePath="/first" doc:name="HTTP Listener Configuration" />
<db:generic-config name="Generic_Database_Configuration_success"
url="jdbc:hsqldb:hsql://localhost:9001" driverClassName="org.hsqldb.jdbcDriver"
doc:name="Generic Database Configuration" />
<db:generic-config name="Generic_Database_Configuration_failed"
url="jdbc:hsqldb:hsql://localhost:9002" driverClassName="org.hsqldb.jdbcDriver"
doc:name="Generic Database Configuration" />
<flow name="first-successfulFlow">
<http:listener config-ref="HTTP_Listener_Configuration"
path="/first" doc:name="HTTP" />
<first-successful doc:name="First Successful">
  <processor-chain>
           <db:select config-ref="Generic_Database_Configuration_failed"
doc:name="Select * from Employee">
<db:dynamic-query><![CDATA[select name from employee where id=3]]></db:dynamic-query>
</db:select>
<logger level="INFO" message="Processed by first message Processor"
doc:name="Logger" />
 </processor-chain>
  <processor-chain>
 <db:select config-ref="Generic_Database_Configuration_success"
doc:name="Select * from Employee">
<db:dynamic-query><![CDATA[select name from employee where id=3]]></db:dynamic-query>
</db:select>
<logger level="INFO" message="Processed by second message Processor"
doc:name="Logger" />
 </processor-chain>
</first-successful>
<object-to-string-transformer />
</flow>
</mule>


Failed Request:

Image title

Success Request:

Image title


Hope this helps.

Thanks.

Keep learning.

Flow control (data) Requests Payload (computing) Database Flow (web browser) application REST Web Protocols

Opinions expressed by DZone contributors are their own.

Related

  • Building REST API Backend Easily With Ballerina Language
  • Composite Requests in Salesforce Are a Great Idea
  • Understanding the Fan-Out/Fan-In API Integration Pattern
  • How To REST With Rails and ActiveResource: Part Three

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook