[[BAMExample-BusinessActivityMonitorExample]]
Business Activity Monitor (BAM) Example
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The BAM (Business Activity Monitor) example shows how to
monitor your transaction flows using Camel.

In this example we will use Camel to monitor a business process
consisting of

* purchase orders
* invoices

Then we will check to see that for every purchase order created by
system A, that system B will generate an invoice within the specified
amount of time (2 seconds in this example). If an invoice is not
generated within the allowed amount of time and error is generated and
sent to an Endpoint.

[[BAMExample-Overview]]
Overview
^^^^^^^^

This example lives in the _examples/camel-example-bam_ directory. It
will poll the following directories

* the child _src/data/purchaseOrders_ directory for XML purchase orders
* the child _src/data/invoices_ directory for XML invoices

The
http://svn.apache.org/repos/asf/camel/trunk/examples/camel-example-bam/src/main/java/org/apache/camel/example/bam/MyActivities.java[MyActivities]
class defines the BAM activities; that is

* the input sources (the two directories above) which could be any of
the supported camel URIs
* how the activities relate to each other - namely the
Correlation Identifier pattern
* the maixmum amount of time allowed from the time a purchase order is
received when if an invoice is not received an error should be raised.

There is also a spring configuration file in
http://svn.apache.org/repos/asf/camel/trunk/examples/camel-example-bam/src/main/resources/META-INF/spring/camel-context.xml[src/resources/META-INF/services/camel-context.xml]
which defines the JPA `EntityManagerFactory` and tells Camel to look in
the *org.apache.camel.example.bam* package to find its routes.

[[BAMExample-Codewalkthrough]]
Code walkthrough
^^^^^^^^^^^^^^^^

So lets start with the activities definition in
http://svn.apache.org/repos/asf/camel/trunk/examples/camel-example-bam/src/main/java/org/apache/camel/example/bam/MyActivities.java[MyActivities]

[source,java]
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
return new ProcessBuilder(entityManagerFactory, transactionTemplate) {
    public void configure() throws Exception {
 
        // let's define some activities, correlating on an XPath on the message bodies
        ActivityBuilder a = activity("seda:a").name("a")
                .correlate(xpath("/hello/@id"));
 
        ActivityBuilder b = activity("seda:b").name("b")
                .correlate(xpath("/hello/@id"));
 
        // now let's add some rules
        b.starts().after(a.completes())
                .expectWithin(seconds(1))
                .errorIfOver(seconds(errorTimeout)).to("mock:overdue");
    }
};
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------

The first two lines of code sets up the inputs for the
BAM activities via the *activity()* method which defines

* the URIs of the inputs (which could come from any of
the Camel Components
* the Correlation Identifier used to
correlate together the purchase order and invoice messages which can be
any Expression via any of the
Languages Supported. In this case we are
using <<xpath-language,XPath>>.

Then the final line of code defines the temporal rules to use; namely
that it is considered to be an error if an invoice is not received
within 2 seconds of a purchase order being received. When a failure
occurs in this example we just send it to the <<log-component,Log>>
component to log out an error level message to commons-logging / log4j.
You could change this to use some of the other
Components such as ActiveMQ,
<<jms-component,JMS>>, <<jms-component,IRC>>, <<jms-component,Mail>>,
<<xmpp-component,XMPP>> etc.

[[BAMExample-Runningtheexample]]
Running the example
^^^^^^^^^^^^^^^^^^^

To run the example we use the link:camel-maven-plugin.html[Camel Maven
Plugin]. For example from the source or binary distribution the
following should work

[source,bash]
-----------------------------
cd examples/camel-example-bam
mvn camel:run
-----------------------------

If you prefer you can just run the Main directly using

[source,bash]
---------------------
mvn compile exec:java
---------------------

Failing that you can run the Main from inside your IDE if you prefer.
Follow the Building instructions to create an
Eclipse/IDEA project to import
