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
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.