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: