Saturday, 29 July 2017

Error handling in Camel Spring DSL

Error handling is one big the major advantage of Camel. It's basically a mimic of the regular try catch finally in the Java language but with more power.The Try Catch Finally as DSL you can use directly on your route.

Sysntax:

<route>
    <from uri="direct:start"/>
    <!-- here the try starts. its a try .. catch .. finally just as regular java code -->
    <doTry>
        <process ref="processorFail"/>
        <to uri="mock:result"/>
        <doCatch>
            <!-- catch multiple exceptions -->
            <exception>java.lang.exception</exception>
            <exception>java.lang.IllegalStateException</exception>
            <to uri="mock:catch"/>
        </doCatch>
        <doFinally>
            <to uri="mock:finally"/>
        </doFinally>
    </doTry>
</route>


Example :


<
route id="BLOG_TIMEOUT" >
    <from uri="direct:start"/>
    <!-- here the try starts. its a try .. catch .. finally just as regular java code -->
    <doTry>
       <to uri="{{HTTPURL}}?httpClient.soTimeout=50(Timout in milliseconds)" />
 <to uri="direct:catch"/>
 <!-- catch multiple exceptions -->
        <doCatch>
            <exception>java.net.SocketTimeoutException</exception>
          <handled><constant>true<constant></handled>
            <to uri="direct:catch"/>
        </doCatch>
    <doCatch>
           <exception>java.lang.Exception</exception>
           <handled><constant>true<constant></handled>
            <to uri="direct:catch"/>
        </doCatch>
        <doFinally>
            <to uri="direct:finally"/>
        </doFinally>
    </doTry>
</route>

Above camel route calling one HTTP end point.While returning a response if any error it will fall into catch block and execute the catch endpoint.To simulate rethrowing an exception from a doCatch you should use the handled predicate. If it is evaluated to false Camel will reattach the exception on the Exchange.




Camel HTTP endpoint Timeout configuration using Apache HttpClient

I have used  Apache HttpClient for a timeout in Camel HTTP end point.It specifically for HTTP calls.

We have various types of timeouts
  1. The Connection Timeout – the time to establish the connection with the remote host
  2. The Socket Timeout – the time to waiting for data – after the connection was established; maximum time wait for response
  3. The Connection Manager Timeout (http.connection-manager.timeout) – the time to wait for a connection from the connection manager/pool
I have used Socket Timeout to handle timeouts in Camel HTTP endpoint.Below things based on Spring DSL.We can do the same way in Java DSL.

Syntax :

{{HTTPURL}}?httpClient.soTimeout=50(Timout in milliseconds)

Syntax with Camel route:

   <route id="BLOG_TIMEOUT">
        <from uri="direct:blog.start"/>
         <to  uri="{{HTTPURL}}?httpClient.soTimeout=50(Timout in milliseconds)" />
        <to uri="direct:blog.end"/>
    </route>

BLOG_TIMEOUT -- route id
direct:blog.start -- from uri
{{HTTPURL}}?httpClient.soTimeout=50(Timout in milliseconds) -- http enpoint with socket timeout 
blog.start - one more endpoint

Tuesday, 23 June 2015

Faster Databases Import & Export Process in MySQL

Normally We are exporting and Importing Mysql database using .sql format.It's exporting and importing process speed depend on size of Database.But while using compress format increase the speed of export.

 Use of  gzip we can increase speed of import and export process of mysql databases.

  For Export Database

   mysqldump -uroot -p database_name | gzip -v > database_name.sql.gz

 For Import Database

    zcat database_name.sql.gz | mysql -uroot -p database_name


These comments very useful for database size is very large.