Wednesday, February 24, 2016

Connecting Canon MX490 to Google Cloud Printer

Connect the printer to the Google Cloud Print web service
  • Click the setup button
  • Select webservice setup
  • Connection setup
  • Google Cloud Print
  • Register for Google Cloud Print
  • A paper will be printed with a url (goo.gl/printer/......)
  • goto your browser - type in the url - this will register the printer to the google user

Share the Printer
  • https://www.google.com/cloudprint?user=0#printers
  • select the desired printer
  • add the desired user to the list of users who can use the printer

Monday, February 22, 2016

Apache Camle & Dozer - Java Bean to Bean mapper using Dozer. (11/250-2016)

Using dozer for Java Bean to Bean mapping in Apache Camel - makes life extremely simple to recursively copy data from one object to another.
The below steps will help to quickly add a dozer mapping file in OSGI Blueprint xml
  • In the blueprint.xml add the bean definitions below
    Note - the name of the camel context should be same as the argument index="0"

<bean id="dozerConverterLoader"
  class="org.apache.camel.converter.dozer.DozerTypeConverterLoader">
  <argument index="0" ref="abcdCamelContext" />
  <argument index="1" ref="abcMapper" />
</bean>
<bean id="abcMapper" class="org.apache.camel.converter.dozer.DozerBeanMapperConfiguration" />


  • Inside the route include the sample code to invoke the dozer mapping file.
  • Also mention the expected source object / destination object

<camelContext id="abcdCamelContext" xmlns="http://camel.apache.org/schema/blueprint">
  <route>
    <from uri="direct:test" />
    .....
<to uri="dozer:submitRequest?mappingFile=mapping/abcd.xml&amp;sourceModel=com.abcd.SourceReq&amp;targetModel=com.abcd.DestReq" />
    .....
  </route>
</camelContext>


  • Mapping file - location - under src/main/resources

<mappings xmlns="http://dozer.sourceforge.net" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://dozer.sourceforge.net  http://dozer.sourceforge.net/schema/beanmapping.xsd">
  
  <mapping wildcard="false">
    <class-a>com.abcd.SourceReq</class-a>
    <class-b>com.abcd.DestReq</class-b>
    
    <field>
      <a>caseNumberSrc</a>
      <b>caseNumberDest</b>
    </field>
  </mapping>
</mappings>


  • Sample Source Object - SourceReq

public class SourceReq {
 
 String caseNumberSrc;
 
 public String getCaseNumberSrc() {
  return caseNumberSrc;
 }
 public void setCaseNumberSrc(String caseNumberSrc) {
  this.caseNumberSrc = caseNumberSrc;
 }

}


  • Sample Destination Object - DestReq

public class DestReq {

 String caseNumberDest;

 public String getCaseNumberDest() {
  return caseNumberDest;
 }

 public void setCaseNumberDest(String caseNumberDest) {
  this.caseNumberDest = caseNumberDest;
 }
}


Saturday, February 13, 2016

Apache Camel - Synchronicity & Threading (9/250-2016)

A service can be called synchronously as well as asynchronously
After the service is invoked - the consumer can use multiple / single thread to complete the operation.

The factors which affect the threading model 

  • Component - which starts the process - whether it is fire and forget or request/reply messaging style
  • EIP - some support parallel processing (concurrency)
  • Configured Synchronicity - some components can be configure to be sync/async
  • Transactions - if a route is transacted -- the transactional context is limited to run within one thread only.
  • MEP (Message Exchange Pattern) - information in the exchange telling InOnly / OutOnly / InOut
Different scenarios discussed in the book - 
  • Async caller - Camel uses only one thread 
    • template.sendBody = InOnly Msg
    • caller sending InOnly Msg
    • single thread - pros
      • simple
      • support transaction propagation
    • cons
      • consumer threads can be overloaded by number of incoming messages
  • Sync Called - Camel uses multi thread
    • template.requestBody = InOutBody
  • Async caller - Camel uses only one thread
  • Sync caller - Camel uses multi thread

References: 

Apache Camel - How to use concurrency with WIPs? (8/250-2016)

Parallel Processing - 
EIPs in Camel that support concurrency - you can turn on parallel processing to use thread pool profiles to apply a matching thread pool (see pg 330 of Camel in Action)
  • Aggregate
  • Multicast -   (see pg 327 of   Camel in Action)
    • parallel processing of multiple routes - best used when multiple routes interacts with multiple systems
    • send copies of the msg to multiple routes
    • aggregate data after all execution complets
  • RecipientList
  • Splitter
  • Threads
  • WireTap
Custom Thread Pool - (see pg 320 of Camel in Action)

  • create thread pools using java.util.concurrent.Executors 
  • ExecutorService threadpool = Executors.newCachedThreadPool
    • the thread pool will grow and shrink - no upper bound - very aggressive
  • ExecutorService threadpool = Executors.newFixedThreadPool(20)


Staged Event Driven Architecture (SEDA) -  (see pg 322 of Camel in Action)

  •  breaks down complex application into statges using internal queues - to handover messages between routes.
  • eg:
from("file:\\...").to("seda:test")...from("seda:test?concurrentConsumers=20")

  • 20  

Thursday, February 11, 2016

Concurrency / Java Thread Pools and Apache Camel - (7/250-2016)


Concurrency - is multitasking.
Camel leverages concurrency features from Java - Thread Pools
  • Applications are generally bound by IO or CPU - Integration applications are generally bound by IO. Use concurrency only if applications are bound by IO
  • The example below shows the usage of concurrency in Camel - default pool size is 10.
  • The example can be changed with Custom Thread pools - and providing the number of threads in the pool.
Example:
<split streaming="true" parallelProcessing="true">
    <tokenize token="\n"/>
   <bean beanType="xxx.XyzService" method="testMeth"/>
</split>

  • There are situations where some applications (external systems) dont allow concurrency / allow till only a certain number of concurrent messages - check SLA first.
Thread Pools
  • java.util.concurrent package has the concurrency API - Java 1.5 onward.
  • Clients for this api are both Camel End users and Camel as well.
  • class ThreadPoolExecutor implements ExecutorService - provides thread pool with the options.
    • corePoolSize
    • maximumPoolSize
    • keepAliveTime
    • unit
    • rejected
    • workQueue
    • threadFactory
  • Managing Thread Pools
    • Shutdown
    • Management
    • Unique Thread names
    • Activity Logging
  • Ensure to Use human understandable thread names
Camel & Thread pools

  • Thread pools are not used directly but via configurations of thread pool profiles.
  • one default profiles and multiple custom profiles
    • poolSize
    • maxPoolSize
    • keepAliveTime
    • maxQueueSize
    • rejectedPolicy - Abot, CallerRuns, DiscardOldest, Discard
      • CallerRuns - will use the caller thread to execute the task
Example Default Thread Pool Profile:-
<camelContext .... >
    <threadPoolProfile id="testdefaultProfile" defaultProfile="true" maxPoolSize="50"/>
</camelContext>

Example Configuring the Custom Thread Pool Profile:-
  • Created using the ThreadPoolProfileSupport class -  "xyzPool" - this name will be referred later. (see pg 327 of   Camel in Action )
  • Set maxPoolSize to - say 100
  • register the ThreadPoolProfile to the context
  • all other options will be inherited from the default profile
  • in the camel route refer to the threadPool using the name "xyzPool"
    • .split(...).streaming().executorServiceRef("xyzPool").bean(...)
  • in spirng
    • <camelContext ....> 
      • <threadPoolProfile id="xyzPool" maxPoolSize="100"/>
    • </camelContext>


Custom Thread Pool

  • Java DSL - using ThreadPoolBuilder  (see pg 328 of   Camel in Action )
  • Spring - using <threadPool> tag
  • both uses the camelContext to refer to the defaultProfile as a base line

Camel will first check for Custom Thread Pool --> Custom Thread Pool Profile  --> Default

Executor Service Strategy (see pg 329 of   Camel in Action )
org.apache.camel.spi.ExecutorServiceStrategy is a pluggable API for thread pool providers.
  • default - DefaultExecutorServiceStrategy - creates thread pools using concurrency API in Java
  • custom - ExecutorServiceStrategy
    • USECASE - Custom Camel component - and you need to run a scheduled background task - recommended to use the ScheduledExecutorService.





Apache Camel - What is SEDA? How can I add concurrency in SEDA? (8/250-2016)

SEDA - Staged Event Driven Architecture - breaks donw complex application into statges using internal queues - to handover messages between routes.

DIRECT - is similar to SEDA component - but it is fully synchronized and acts as a direct call.

Ex.

from("file:\\...")
.to("seda:test")
...

from("seda:test?concurrentConsumers=20")
....
....


In this case -  20 threads will be created and the processing will be fast.



Friday, February 5, 2016

Apache Camel - How to trigger Compensation Handler? (7/250-2016)





Compensation handler in camel is based on the concept of Unit of Work — group together multiple tasks as a single unit — mimicking transnational boundaries.
  • org.apache.camel.spi.UnitOfWork Interface represents UnitOfWork in Camel.
  • Exchange has exactly one UnitOfWork (private to Exchange — not shared with others) — accessed from exchange using method — getUnitOfWork
  • Camel will automatically inject a new UnitOfWork into Exchange — using internal processor UnitOfWorkProcessor — involved in start of every route
  • At the end of the route, the processor invokes registered Synchronization callback

Synchronization (a callback)
  • org.apache.camel.spi.Synchronization Interface is used to represent Synchronization.
  • used to execute custom logic — after processing — using the methods below.
onComplete
onFailure
  • An exchange can add multiple callBacks.
  • If there is an error while processing callBack — Camel will log in WARN level and continue to next callback.
Synchronization is added / removed to the UnitOfWork using the methods below — using a processor.
addSynchronization
removeSynchronization
done — invoked when UnitOfWork is complete
OR
addOnCompletetion
This is a method on the exchange — it creates a new thread — and is the preferred method for compensation handling.
addOnCompletion can be written in Java DSL as well as Spring DSL.
It can be route / context scoped.

References:

Friday, January 29, 2016

Apache Camel - How to create a Datasource in Camel - OSGI Blueprint DSL? (6/250-2016)


  • Create a Fuse project (archtype contract first) and name it Datasource - this is the Datasource project which will be deployed independently
  • In the blueprint.xml add the code below
<cm:property-placeholder persistent-id="configuration" update-strategy="reload" />

  <bean id="dbcp" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName" value="com.ibm.db2.jcc.DB2Driver"/>
    <property name="url" value="${db.jdbc.url}"/>
    <property name="username" value="${db.jdbc.username}"/>
    <property name="password" value="${db.jdbc.password}"/>
  </bean>  

  <service interface="javax.sql.DataSource" ref="dbcp">
    <service-properties>
      <entry key="osgi.jndi.service.name" value="jdbc/DBDS"/>
      <entry key="datasource.name" value="DBDS"/>
    </service-properties>
  </service>

  • Configuration - this needs to be saved as configuration.cfg and places under <installs-dir>/etc
db.datasource=DBDS
db.jndi.datasource=jdbc/DBDS
db.jdbc.username=abcd
db.jdbc.password=abcd
db.jdbc.url=jdbc:db2://<ip-address>:<port>/<databasename>


  • Dependency to be added in pom.xml

<modelVersion>4.0.0</modelVersion>
<groupId>com.test.ds</groupId>
<artifactId>datasource</artifactId>
<name>DataSource</name>
<version>1.0.0-SNAPSHOT</version>
<packaging>bundle</packaging>
<dependencies>
    <dependency>
 <groupId>commons-dbcp</groupId>
 <artifactId>commons-dbcp</artifactId>
 <version>1.4</version>
    </dependency>
</dependencies>

  • Once the Datasource project is ready the Datasource object can be used in beans across several projects
  • Create a new Fuse project - name it TestDB (archtype - contract first)
  • In the blueprint add the code below

<bean id="SomeBean" class="com.some.bean.SomeBean">
    <property name="dataSource">
        <reference filter="(datasource.name=DBDS)" interface="javax.sql.DataSource" />
    </property>
</bean>

  • the datasource name is registered as "DBDS" - and this is referred from the bean "SomeBean"

public class SomeBean{
        private DataSource dataSource = null;
 public DataSource getDataSource() {
  return dataSource;
 }
 public void setDataSource(DataSource dataSource) {
  this.dataSource = dataSource;
 }
 
 public String testDBConnection()
 {
  //in this bean u can access the datasource and get connection
  DataSource ds = getDataSource();
  if(ds != null)
  {
   Connection conn = ds.getConnection()
  }
 }
} 


  • Once the datasource is accessable from a bean - life is simple and we can do any kind of DB queries
  • While testing in JBoss Fuse - please install features mentioned below
    • osgi:install wrap:mvn:commons-dbcp/commons-dbcp/1.4




Wednesday, January 27, 2016

Apache Camel - What is a Dynamic Router EIP? (5/250 - 2016)

The Dynamic Router from the EIP patterns allows you to route messages while avoiding the dependency of the router on all possible destinations while maintaining its efficiency.

Dynamic router is similar to Routing Slip - the difference being
1. Routing Slip - the decision is made before hand
2. Dynamic Router (Dynamic Routing Slip) - the decision is based on the fly - Provide logic - using Java code/query Database /rules Engine  - to route msg to particular destination

Example:

Spring DSL


<bean id="myBean" class="xyz.DynamicRouterAnnotationBean"/>
<camelContext xmlns="http://camel.apache.org/schema/spring">
        <route>
            <from uri="direct:start"/>
            <bean ref="myBean" method="route"/>
            <to uri="mock:result"/>
        </route>
        <route>
            <from uri="direct:a"/>
    <log message="in direct:a"/>
        </route>
        <route>
            <from uri="direct:b"/>
    <log message="in direct:b"/>
        </route>
        <route>
            <from uri="direct:c"/>
    <log message="in direct:c"/>
        </route>
        <route>
            <from uri="direct:d"/>
    <log message="in direct:c"/>
        </route>
</camelContext>

Java Bean

public class DynamicRouterAnnotationBean {
    @DynamicRouter
    public String route(String body, @Header(Exchange.SLIP_ENDPOINT) String previous) {
    System.out.println("**************route, body-"+body+", previous-"+previous);
        if (previous == null) {
            // 1st time
            return "direct://a";
        } else if ("direct://a".equals(previous)) {
            // 2nd time -
            return "direct://b";
        } else if ("direct://b".equals(previous)) {
            // 2nd time -
            return "direct://d,direct://c";
        } else if ("direct://c".equals(previous)) {
        // 3rd time - transform the message body using the simple language
            return "direct://d";
        } else if ("direct://d".equals(previous)) {
            // 3rd time - transform the message body using the simple language
            return "language://simple:Bye ${body}";
        } else {
            // no more, so return null to indicate end of dynamic router
            return null;
        }
    }
}
  • From the camel route "direct:start", we call the method "route" in the bean DynamicRouterAnnotationBean. 
  • The method "route" has an annotation @DynamicRouter which tells camel that this is not a regular java bean, but follows EIPs
  • the route method will then be invoked repeatedly until it returns "null"
  • you can return multiple endpoints using delimeter


Output

**************route, body-Camel, previous-null
2016-01-27 11:17:14,910 [                     main] INFO  route2                         - in direct:a
**************route, body-Camel, previous-direct://a
2016-01-27 11:17:14,914 [                     main] INFO  route3                         - in direct:b
**************route, body-Camel, previous-direct://b
2016-01-27 11:17:14,925 [                     main] INFO  route5                         - in direct:d
2016-01-27 11:17:14,928 [                     main] INFO  route4                         - in direct:c
**************route, body-Camel, previous-direct://c
2016-01-27 11:17:14,930 [                     main] INFO  route5                         - in direct:d
**************route, body-Camel, previous-direct://d
**************route, body-Bye Camel, previous-language://simple:Bye%20$%7Bbody%7D



References:
http://camel.apache.org/dynamic-router.html
https://github.com/camelinaction/camelinaction

Apache Camel - What is a Routing Slip? (4/250 for 2016)

The Routing Slip from the EIP patterns allows you to route a message consecutively through a series of processing steps where the sequence of steps is not known at design time and can vary for each message.







References:

http://camel.apache.org/routing-slip.html