<?xml version="1.0" encoding="UTF-8"?>
<chapter id="ch.integration"
         version="5.0"
         xsi:schemaLocation="http://docbook.org/ns/docbook http://www.docbook.org/xml/5.0/xsd/docbook.xsd http://www.w3.org/1999/xlink http://www.docbook.org/xml/5.0/xsd/xlink.xsd"
         xml:base="../" xmlns="http://docbook.org/ns/docbook" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xlink="http://www.w3.org/1999/xlink"
         xmlns:xi="http://www.w3.org/2001/XInclude" xmlns:ns="http://docbook.org/ns/docbook">

  <title>Integration with Maven, OSGi, Spring, etc.</title>

  <para>jBPM can be integrated with a lot of other technologies.  This chapter gives an
  overview of a few of those that are supported out-of-the-box.  Most of these modules
  are developed as part of the droolsjbpm-integration module, so they work not only for your
  business processes but also for business rules and complex event processing.</para>
  
  <section>
    <title>Maven</title>

  <para>By using a Maven pom.xml to define your project dependencies, you can let maven get
  your dependencies for you.  The following pom.xml is an example that could for example be
  used to create a new Maven project that is capable of executing a BPMN2 process:</para>
  
<programlisting language="xml">
&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;project xmlns="http://maven.apache.org/POM/4.0.0" 
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"&gt;

  &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
  &lt;groupId&gt;org.jbpm&lt;/groupId&gt;
  &lt;artifactId&gt;jbpm-maven-example&lt;/artifactId&gt;
  &lt;name&gt;jBPM Maven Project&lt;/name&gt;
  &lt;version&gt;1.0-SNAPSHOT&lt;/version&gt;
  
  &lt;repositories&gt;
    &lt;!--  use this repository for stable releases --&gt;
    &lt;repository&gt;
      &lt;id&gt;jboss-public-repository-group&lt;/id&gt;
      &lt;name&gt;JBoss Public Maven Repository Group&lt;/name&gt;
      &lt;url&gt;https://repository.jboss.org/nexus/content/groups/public/&lt;/url&gt;
      &lt;layout&gt;default&lt;/layout&gt;
      &lt;releases&gt;
        &lt;enabled&gt;true&lt;/enabled&gt;
        &lt;updatePolicy&gt;never&lt;/updatePolicy&gt;
      &lt;/releases&gt;
      &lt;snapshots&gt;
        &lt;enabled&gt;false&lt;/enabled&gt;
      &lt;/snapshots&gt;
    &lt;/repository&gt;
    &lt;!-- use this repository for snapshot releases --&gt;
    &lt;repository&gt;
      &lt;id&gt;jboss-snapshot-repository-group&lt;/id&gt;
      &lt;name&gt;JBoss SNAPSHOT Maven Repository Group&lt;/name&gt;
      &lt;url&gt;https://repository.jboss.org/nexus/content/repositories/snapshots/&lt;/url&gt;
      &lt;layout&gt;default&lt;/layout&gt;
      &lt;releases&gt;
        &lt;enabled&gt;false&lt;/enabled&gt;
      &lt;/releases&gt;
      &lt;snapshots&gt;
        &lt;enabled&gt;true&lt;/enabled&gt;
        &lt;updatePolicy&gt;never&lt;/updatePolicy&gt;
      &lt;/snapshots&gt;
    &lt;/repository&gt;
    
  &lt;/repositories&gt;
    
  &lt;dependencies&gt;
    &lt;dependency&gt;
      &lt;groupId&gt;org.jbpm&lt;/groupId&gt;
      &lt;artifactId&gt;jbpm-bpmn2&lt;/artifactId&gt;
      &lt;version&gt;5.4.0.Final&lt;/version&gt;
    &lt;/dependency&gt;
  &lt;/dependencies&gt;
  
&lt;/project&gt;</programlisting>

    <para>To use this as the basis for your project in Eclipse, either use M2Eclipse or
    use "mvn eclipse:eclipse" to generate eclipse .project and .classpath files based on
    this pom.</para>

    </section>
    
    <section>
      <title>OSGi</title>
    
      <para>All core jbpm jars (and core dependencies) are OSGi-enabled.  That means that
      they contain MANIFEST.MF files (in the META-INF directory) that describe their
      dependencies etc.  These manifest files are automatically generated by the build.
      You can plug these jars directly into an OSGi environment.</para>
      
  <para>OSGi is a dynamic module system for declarative services. So what does
  that mean? Each jar in OSGi is called a bundle and has it's own Classloader.
  Each bundle specifies the packages it exports (makes publicly available) and
  which packages it imports (external dependencies). OSGi will use this
  information to wire the classloaders of different bundles together; the key
  distinction is you don't specify what bundle you depend on, or have a single
  monolithic classpath, instead you specify your package import and version
  and OSGi attempts to satisfy this from available bundles.</para>

  <para>It also supports side by side versioning, so you can have multiple
  versions of a bundle installed and it'll wire up the correct one. Further to
  this Bundles can register services for other bundles to use. These services
  need initialisation, which can cause ordering problems - how do you make
  sure you don't consume a service before its registered? OSGi has a number of
  features to help with service composition and ordering. The two main ones
  are the programmatic ServiceTracker and the xml based Declarative Services.
  There are also other projects that help with this; Spring DM, iPOJO,
  Gravity.</para>

  <para> The following jBPM jars are OGSi-enabled:</para>
      <itemizedlist>
        <listitem>jbpm-flow</listitem>
        <listitem>jbpm-flow-builder</listitem>
        <listitem>jbpm-bpmn2</listitem>
      </itemizedlist>
      
  <para>For example, the following code example shows how you can look up the
  necessary services in an OSGi environment using the service registry and 
  create a session that can then be used to start processes, signal events, etc.</para>
       
<programlisting>ServiceReference serviceRef = bundleContext.getServiceReference( ServiceRegistry.class.getName() );
ServiceRegistry registry = (ServiceRegistry) bundleContext.getService( serviceRef );

KnowledgeBuilderFactoryService knowledgeBuilderFactoryService = registry.get( KnowledgeBuilderFactoryService.class );
KnowledgeBaseFactoryService knowledgeBaseFactoryService = registry.get( KnowledgeBaseFactoryService.class );
ResourceFactoryService resourceFactoryService = registry.get( ResourceFactoryService.class );

KnowledgeBaseConfiguration kbaseConf = knowledgeBaseFactoryService.newKnowledgeBaseConfiguration( null, getClass().getClassLoader() );

KnowledgeBuilderConfiguration kbConf = knowledgeBuilderFactoryService.newKnowledgeBuilderConfiguration( null, getClass().getClassLoader() );
KnowledgeBuilder kbuilder = knowledgeBuilderFactoryService.newKnowledgeBuilder( kbConf );
kbuilder.add( resourceFactoryService.newClassPathResource( "MyProcess.bpmn", Dummy.class ), ResourceType.BPMN2 );

kbaseConf = knowledgeBaseFactoryService.newKnowledgeBaseConfiguration( null, getClass().getClassLoader() );
KnowledgeBase kbase = knowledgeBaseFactoryService.newKnowledgeBase( kbaseConf );
kbase.addKnowledgePackages( kbuilder.getKnowledgePackages() );

StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
</programlisting>      
    </section>
    
    <section>
      <title>Spring</title>
      
    <para>A Spring XML configuration file can be used to easily define and configure knowledge
    bases and sessions in a Spring environment.  This allows you to simply access a session and
    invoke processes from within your Spring application.</para>
    
    <para>For example, the following configuration file sets up a new session based on a
    knowledge base with one process definition (loaded from the classpath).</para>
    
    <programlisting language="xml">&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:jbpm="http://drools.org/schema/drools-spring"       
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
                           http://drools.org/schema/drools-spring org/drools/container/spring/drools-spring-1.2.0.xsd"&gt;
   
  &lt;jbpm:kbase id="kbase"&gt;
    &lt;jbpm:resources&gt;
      &lt;jbpm:resource type="BPMN2" source="classpath:HelloWorld.bpmn2"/&gt;
    &lt;/jbpm:resources&gt;
  &lt;/jbpm:kbase&gt;

  &lt;jbpm:ksession id="ksession" type="stateful" kbase="kbase" /&gt;

&lt;/beans&gt;</programlisting>
    
    <para>The following piece of code can be used to load the above Spring configuration, retrieve
    the session and start the process.</para>
    
    <programlisting language="java">ClassPathXmlApplicationContext context =
    new ClassPathXmlApplicationContext("spring-conf.xml");
StatefulKnowledgeSession ksession = (StatefulKnowledgeSession) context.getBean("ksession");
ksession.startProcess("com.sample.HelloWorld");</programlisting>
    
    <para>Note that you can also inject the session in one of your domain objects, for example by adding
    the following fragment in the configuration file.</para>
    
    <programlisting language="xml">&lt;bean id="myObject" class="org.jbpm.sample.MyObject"&gt;
  &lt;property name="session" ref="ksession" /&gt;
&lt;/bean&gt;</programlisting>
    
    <para>As a result, the session will be injected in your domain object can then be accessed directly.
    For example:</para>
    
    <programlisting language="java">public class MyObject {
  private StatefulKnowledgeSession ksession;
  public void setSession(StatefulKnowledgeSession ksession) {
    this.ksession = ksession;
  }
  public void doSomething() {
    ksession.startProcess("com.sample.HelloWorld");
  }
}</programlisting>
    
    <para>The following example shows a slightly more complex example, where the session is
    configured to use persistence (JPA using an in-memory database in this case) and transaction
    (using the Spring transaction manager).  When using the Spring transaction manager, you have
    three options:</para>
    <itemizedlist>
      <listitem>using the JTA transaction manager with a shared entity manager factory (emf)</listitem>
      <!--listitem>using the JTA transaction manager with a shared entity manager (em)</listitem-->
      <listitem>using local transactions with a shared entity manager factory (emf)</listitem>
      <!--listitem>using local transactions with a shared entity manager (em)</listitem-->
    </itemizedlist>
    
    <section>
      <title>Spring using the JTA transaction manager</title>
    
    <para>The following code sample shows the Spring configuration file, configured for JTA
    transactions (using Bitronix in this case) with a shared entity manager factory (emf).</para>
    
    <programlisting language="xml">&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:jbpm="http://drools.org/schema/drools-spring"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
                           http://drools.org/schema/drools-spring http://drools.org/schema/drools-spring-1.3.0.xsd"&gt;

  &lt;bean id="jbpmEMF" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"&gt;
    &lt;property name="persistenceUnitName" value="org.jbpm.persistence.jta"/&gt;
  &lt;/bean&gt;

  &lt;bean id="btmConfig" factory-method="getConfiguration" class="bitronix.tm.TransactionManagerServices"&gt;
  &lt;/bean&gt;

  &lt;bean id="BitronixTransactionManager" factory-method="getTransactionManager"
        class="bitronix.tm.TransactionManagerServices" depends-on="btmConfig" destroy-method="shutdown" /&gt;
  
  &lt;bean id="jbpmTxManager" class="org.springframework.transaction.jta.JtaTransactionManager"&gt;
    &lt;property name="transactionManager" ref="BitronixTransactionManager" /&gt;
    &lt;property name="userTransaction" ref="BitronixTransactionManager" /&gt;
  &lt;/bean&gt;
  
  &lt;jbpm:kbase id="kbase1"&gt;
    &lt;jbpm:resources&gt;
      &lt;jbpm:resource type="BPMN2" source="classpath:MyProcess.bpmn"/&gt;
    &lt;/jbpm:resources&gt;
  &lt;/jbpm:kbase&gt;

  &lt;jbpm:ksession id="ksession1" type="stateful" kbase="kbase1"&gt;
    &lt;jbpm:configuration&gt;
      &lt;jbpm:jpa-persistence&gt;
        &lt;jbpm:transaction-manager ref="txManager"/&gt;
        &lt;jbpm:entity-manager-factory ref="emf"/&gt;
      &lt;/jbpm:jpa-persistence&gt;
    &lt;/jbpm:configuration&gt;
  &lt;/jbpm:ksession&gt;
&lt;/beans&gt;</programlisting>

    <para>And the matching persistence.xml file:</para>
    
    <programlisting language="xml">&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
                                 http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"&gt;

  &lt;persistence-unit name="org.jbpm.persistence.jta" transaction-type="JTA"&gt;
    &lt;provider&gt;org.hibernate.ejb.HibernatePersistence&lt;/provider&gt;
    &lt;jta-data-source&gt;jdbc/jbpm-ds&lt;/jta-data-source&gt;

    &lt;!-- Use this if you are using JPA1 / Hibernate3 --&gt; 
    &lt;mapping-file&gt;META-INF/JBPMorm.xml&lt;/mapping-file&gt;
    &lt;mapping-file&gt;META-INF/ProcessInstanceInfo.hbm.xml&lt;/mapping-file&gt;
    &lt;!-- Use this if you are using JPA2 / Hibernate4 --&gt; 
    &lt;!--mapping-file&gt;META-INF/JBPMorm-JPA2.xml&lt;/mapping-file--&gt;

    &lt;class&gt;org.drools.persistence.info.SessionInfo&lt;/class&gt;
    &lt;class&gt;org.jbpm.persistence.processinstance.ProcessInstanceInfo&lt;/class&gt;
    &lt;class&gt;org.drools.persistence.info.WorkItemInfo&lt;/class&gt;

    &lt;class&gt;org.jbpm.process.audit.ProcessInstanceLog&lt;/class&gt;
    &lt;class&gt;org.jbpm.process.audit.NodeInstanceLog&lt;/class&gt;
    &lt;class&gt;org.jbpm.process.audit.VariableInstanceLog&lt;/class&gt;
    
    &lt;properties&gt;
      &lt;property name="hibernate.max_fetch_depth" value="3"/&gt;
      &lt;property name="hibernate.hbm2ddl.auto" value="update"/&gt;
      &lt;property name="hibernate.show_sql" value="false"/&gt;
      &lt;property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/&gt;
      &lt;property name="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.BTMTransactionManagerLookup" /&gt;
    &lt;/properties&gt;
  &lt;/persistence-unit&gt;
&lt;/persistence&gt;</programlisting>

    </section>
    
    <section>
      <title>Spring using local transactions</title>

    <para>To use local transactions (instead of JTA) with a shared entity manager (emf), use:</para>
    
    <programlisting language="xml">&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:jbpm="http://drools.org/schema/drools-spring"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
                           http://drools.org/schema/drools-spring http://drools.org/schema/drools-spring-1.3.0.xsd"&gt;

  &lt;bean id="jbpmEMF" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"&gt;
    &lt;property name="persistenceUnitName" value="org.jbpm.persistence.local"/&gt;
  &lt;/bean&gt;

  &lt;bean id="jbpmTxManager" class="org.springframework.orm.jpa.JpaTransactionManager"&gt;
    &lt;property name="entityManagerFactory" ref="jbpmEMF"/&gt;
    &lt;property name="nestedTransactionAllowed" value="false"/&gt;
  &lt;/bean&gt;
  
  &lt;jbpm:kbase id="kbase1"&gt;
    &lt;jbpm:resources&gt;
      &lt;jbpm:resource type="BPMN2" source="classpath:MyProcess.bpmn"/&gt;
    &lt;/jbpm:resources&gt;
  &lt;/jbpm:kbase&gt;

  &lt;jbpm:ksession id="ksession1" type="stateful" kbase="kbase1"&gt;
    &lt;jbpm:configuration&gt;
      &lt;jbpm:jpa-persistence&gt;
        &lt;jbpm:transaction-manager ref="txManager"/&gt;
        &lt;jbpm:entity-manager-factory ref="emf"/&gt;
      &lt;/jbpm:jpa-persistence&gt;
    &lt;/jbpm:configuration&gt;
  &lt;/jbpm:ksession&gt;
&lt;/beans&gt;</programlisting>    

    <para>And the matching persistence.xml file:</para>
    
        <programlisting language="xml">&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
                                 http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"&gt;

  &lt;persistence-unit name="org.jbpm.persistence.local" transaction-type="RESOURCE_LOCAL"&gt;
    &lt;provider&gt;org.hibernate.ejb.HibernatePersistence&lt;/provider&gt;
    &lt;non-jta-data-source&gt;jdbc/jbpm-ds&lt;/non-jta-data-source&gt;

    &lt;!-- Use this if you are using JPA1 / Hibernate3 --&gt; 
    &lt;mapping-file&gt;META-INF/JBPMorm.xml&lt;/mapping-file&gt;
    &lt;mapping-file&gt;META-INF/ProcessInstanceInfo.hbm.xml&lt;/mapping-file&gt;
    &lt;!-- Use this if you are using JPA2 / Hibernate4 --&gt; 
    &lt;!--mapping-file&gt;META-INF/JBPMorm-JPA2.xml&lt;/mapping-file--&gt;

    &lt;class&gt;org.drools.persistence.info.SessionInfo&lt;/class&gt;
    &lt;class&gt;org.jbpm.persistence.processinstance.ProcessInstanceInfo&lt;/class&gt;
    &lt;class&gt;org.drools.persistence.info.WorkItemInfo&lt;/class&gt;

    &lt;class&gt;org.jbpm.process.audit.ProcessInstanceLog&lt;/class&gt;
    &lt;class&gt;org.jbpm.process.audit.NodeInstanceLog&lt;/class&gt;
    &lt;class&gt;org.jbpm.process.audit.VariableInstanceLog&lt;/class&gt;
    
    &lt;properties&gt;
      &lt;property name="hibernate.max_fetch_depth" value="3"/&gt;
      &lt;property name="hibernate.hbm2ddl.auto" value="update"/&gt;
      &lt;property name="hibernate.show_sql" value="false"/&gt;
      &lt;property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/&gt;
    &lt;/properties&gt;
  &lt;/persistence-unit&gt;
&lt;/persistence&gt;</programlisting>

    </section>
    
    <section>
      <title>Spring using a shared entity manager</title>
      
    <para>Instead of using a shared entity manager factory (emf), you can also use a
    shared entity manager instead (both using JTA or local transactions).  To do so,
    create the entity manager in your Spring configuration file:</para>
    
    <programlisting language="xml">&lt;bean id="jbpmEM" class="org.springframework.orm.jpa.support.SharedEntityManagerBean"&gt;
  &lt;property name="entityManagerFactory" ref="jbpmEMF"/&gt;
&lt;/bean&gt;</programlisting>

    <para>You can then create a ksession using the following code:</para>
    
    <programlisting language="java">EntityManager em = (EntityManager) context.getBean("jbpmEM");
Environment env = EnvironmentFactory.newEnvironment();	
env.set(EnvironmentName.APP_SCOPED_ENTITY_MANAGER, em);		
env.set(EnvironmentName.CMD_SCOPED_ENTITY_MANAGER, em);
env.set("IS_JTA_TRANSACTION", false);
env.set("IS_SHARED_ENTITY_MANAGER", true);
		
AbstractPlatformTransactionManager aptm = (AbstractPlatformTransactionManager) context.getBean( "jbpmTxManager" );		
TransactionManager transactionManager = new DroolsSpringTransactionManager( aptm );
env.set(EnvironmentName.TRANSACTION_MANAGER, transactionManager);

PersistenceContextManager persistenceContextManager = new DroolsSpringJpaManager(env);
env.set(EnvironmentName.PERSISTENCE_CONTEXT_MANAGER, persistenceContextManager);
		
StatefulKnowledgeSession ksession = JPAKnowledgeService.newStatefulKnowledgeSession( kbase, null, env );</programlisting>
    
    </section>
	    
    <section>
      <title>Using a local task service</title>
      
      <para>If you also want to use a local task server, linked to the engine, first of
      all add the following lines to your persistence.xml:</para>
      
    <programlisting language="xml">&lt;mapping-file>META-INF/Taskorm.xml&lt;/mapping-file&gt;
&lt;class&gt;org.jbpm.task.Attachment&lt;/class&gt;
&lt;class&gt;org.jbpm.task.Content&lt;/class&gt;
&lt;class&gt;org.jbpm.task.BooleanExpression&lt;/class&gt;
&lt;class&gt;org.jbpm.task.Comment&lt;/class&gt;
&lt;class&gt;org.jbpm.task.Deadline&lt;/class&gt;
&lt;class&gt;org.jbpm.task.Comment&lt;/class&gt;
&lt;class&gt;org.jbpm.task.Deadline&lt;/class&gt;
&lt;class&gt;org.jbpm.task.Delegation&lt;/class&gt;
&lt;class&gt;org.jbpm.task.Escalation&lt;/class&gt;
&lt;class&gt;org.jbpm.task.Group&lt;/class&gt;
&lt;class&gt;org.jbpm.task.I18NText&lt;/class&gt;
&lt;class&gt;org.jbpm.task.Notification&lt;/class&gt;
&lt;class&gt;org.jbpm.task.EmailNotification&lt;/class&gt;
&lt;class&gt;org.jbpm.task.EmailNotificationHeader&lt;/class&gt;
&lt;class&gt;org.jbpm.task.PeopleAssignments&lt;/class&gt;
&lt;class&gt;org.jbpm.task.Reassignment&lt;/class&gt;
&lt;class&gt;org.jbpm.task.Status&lt;/class&gt;
&lt;class&gt;org.jbpm.task.Task&lt;/class&gt;
&lt;class&gt;org.jbpm.task.TaskData&lt;/class&gt;
&lt;class&gt;org.jbpm.task.SubTasksStrategy&lt;/class&gt;
&lt;class&gt;org.jbpm.task.OnParentAbortAllSubTasksEndStrategy&lt;/class&gt;
&lt;class&gt;org.jbpm.task.OnAllSubTasksEndParentEndStrategy&lt;/class&gt;
&lt;class&gt;org.jbpm.task.User&lt;/class&gt;</programlisting>

    <para>Next, add the task service configuration to your Spring configuration file,
    after which you can get your local task service from your Spring context.</para>
    
    <programlisting language="xml">&lt;bean id="systemEventListener" class="org.drools.SystemEventListenerFactory" factory-method="getSystemEventListener" /&gt;

&lt;bean id="internalTaskService" class="org.jbpm.task.service.TaskService" &gt;
  &lt;property name="systemEventListener" ref="systemEventListener" /&gt;
&lt;/bean&gt;

&lt;bean id="htTxManager" class="org.drools.container.spring.beans.persistence.HumanTaskSpringTransactionManager"&gt;
  &lt;constructor-arg ref="jbpmTxManager" /&gt;
&lt;/bean&gt;

&lt;bean id="springTaskSessionFactory" class="org.jbpm.task.service.persistence.TaskSessionSpringFactoryImpl"
      init-method="initialize" depends-on="internalTaskService" &gt;
  &lt;property name="entityManagerFactory" ref="jbpmEMF" /&gt;
  &lt;property name="transactionManager" ref="htTxManager" /&gt;
  &lt;property name="useJTA" value="true" /&gt;
  &lt;property name="taskService" ref="internalTaskService" /&gt;
&lt;/bean&gt;
  
&lt;bean id="taskService" class="org.jbpm.task.service.local.LocalTaskService" depends-on="internalTaskService" &gt;
  &lt;constructor-arg ref="internalTaskService" /&gt;
&lt;/bean&gt;</programlisting>
    
    <para>Note that, if you want your session linked to your local task service, you still
    need to create a synchronous human task handler and register it to the session using:</para>
    
    <programlisting language="java">SyncWSHumanTaskHandler humanTaskHandler = new SyncWSHumanTaskHandler(taskService, ksession);
humanTaskHandler.setLocal(true);
humanTaskHandler.connect();
ksession.getWorkItemManager().registerWorkItemHandler("Human Task", humanTaskHandler);</programlisting>
      
    </section>
  </section>

  <section>
    <title>Apache Camel Integration</title>

    <para>Camel provides a lightweight bus framework for geting information into and
      out of jBPM. Additionally Camel provides a way to expose your KnowledgeBases
      remotely for any sort of client application that can use HTTP, through a SOAP
      or REST interface.
    </para>

    <para>The following example shows how to setup a remote accessible session:</para>

    <programlisting language="xml">&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:cxf="http://camel.apache.org/schema/cxf"
       xmlns:jaxrs="http://cxf.apache.org/jaxrs"
       xmlns:kb="http://drools.org/schema/drools-spring"
       xsi:schemaLocation="
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
       http://drools.org/schema/drools-spring http://drools.org/schema/drools-spring.xsd
       http://camel.apache.org/schema/cxf http://camel.apache.org/schema/cxf/camel-cxf.xsd
       http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd
       http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd"&gt;

  &lt;!-- jBPM Knowledge Related Config --&gt;

  &lt;kb:grid-node id="node1"/&gt;

  &lt;kb:kbase id="kbase1" node="node1"&gt;
    &lt;kb:resources&gt;
      &lt;kb:resource type="BPMN2" source="classpath:MyProcess.bpmn"/&gt;
    &lt;/kb:resources&gt;
  &lt;/kb:kbase&gt;

  &lt;kb:ksession id="ksession1" type="stateless" kbase="kbase1" node="node1"/&gt;

  &lt;!-- Camel Config --&gt;

  &lt;import resource="classpath:META-INF/cxf/cxf.xml" /&gt;
  &lt;import resource="classpath:META-INF/cxf/cxf-servlet.xml" /&gt;

  &lt;cxf:rsServer id="rsServer"
                address="/rest"
                serviceClass="org.drools.jax.rs.CommandExecutorImpl"&gt;
       &lt;cxf:providers&gt;
           &lt;bean class="org.drools.jax.rs.CommandMessageBodyReader"/&gt;
       &lt;/cxf:providers&gt;
  &lt;/cxf:rsServer&gt;

  &lt;cxf:cxfEndpoint id="soapServer"
            address="/soap"
             serviceName="ns:CommandExecutor"
             endpointName="ns:CommandExecutorPort"
          wsdlURL="soap.wsdl"
          xmlns:ns="http://soap.jax.drools.org/" &gt;
    &lt;cxf:properties&gt;
      &lt;entry key="dataFormat" value="MESSAGE"/&gt;
      &lt;entry key="defaultOperationName" value="execute"/&gt;
    &lt;/cxf:properties&gt;
  &lt;/cxf:cxfEndpoint&gt;

  &lt;bean id="kbPolicy" class="org.drools.camel.component.DroolsPolicy" /&gt;

  &lt;camelContext id="camel" xmlns="http://camel.apache.org/schema/spring"&gt;
    &lt;route&gt;
       &lt;from uri="cxfrs://bean://rsServer"/&gt;
       &lt;policy ref="kbPolicy"&gt;
         &lt;unmarshal ref="xstream" /&gt;
         &lt;to uri="drools:node1/ksession1" /&gt;
         &lt;marshal ref="xstream" /&gt;
       &lt;/policy&gt;
    &lt;/route&gt;

    &lt;route&gt;
      &lt;from uri="cxf://bean://soapServer"/&gt;
      &lt;policy ref="kbPolicy"&gt;
        &lt;unmarshal ref="xstream" /&gt;
        &lt;to uri="drools:node1/ksession1" /&gt;
        &lt;marshal ref="xstream" /&gt;
      &lt;/policy&gt;
    &lt;/route&gt;

  &lt;/camelContext&gt;

&lt;/beans&gt;</programlisting>

    <para>To execute the above example you must be sure that you have the following
      content in your web.xml file:</para>

    <programlisting language="xml">&lt;web-app&gt;
  (...)
  &lt;context-param&gt;
    &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
    &lt;param-value&gt;classpath:beans.xml&lt;/param-value&gt;
  &lt;/context-param&gt;

  &lt;listener&gt;
    &lt;listener-class&gt;
      org.springframework.web.context.ContextLoaderListener
    &lt;/listener-class&gt;
  &lt;/listener&gt;

  &lt;servlet&gt;
    &lt;display-name&gt;CXF Servlet&lt;/display-name&gt;
    &lt;servlet-name&gt;CXFServlet&lt;/servlet-name&gt;
    &lt;servlet-class&gt;
      org.apache.cxf.transport.servlet.CXFServlet
    &lt;/servlet-class&gt;
    &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
  &lt;/servlet&gt;

  &lt;servlet-mapping&gt;
    &lt;servlet-name&gt;CXFServlet&lt;/servlet-name&gt;
    &lt;url-pattern&gt;/kservice/*&lt;/url-pattern&gt;
  &lt;/servlet-mapping&gt;

  &lt;session-config&gt;
    &lt;session-timeout&gt;10&lt;/session-timeout&gt;
  &lt;/session-config&gt;
&lt;/web-app&gt;</programlisting>

    <para>After deploying the above example, you can test it using any http compatible
      tool like curl, directly from you command line</para>

    <programlisting>$ curl -v \
  -H 'Content-Type: text/plain' \
  -d '&lt;batch-execution lookup="ksession1"&gt; \
  &lt;start-process processId="org.jbpm.sample.my-process" out-identifier = "processId"/&gt; \
  &lt;/batch-execution&gt;' \
  http://localhost:8080/jbpm-camel/kservice/rest/execute</programlisting>

    <para>The above execution will result in something similar to the following code snippet:</para>

    <programlisting>HTTP/1.1 200 OK
Content-Length: 131
Server: Apache-Coyote/1.1
Date: Mon, 13 Apr 2012 17:02:42 GMT
Content-Type: text/plain
Connection: close

&lt;?xml version='1.0' encoding='UTF-8'?&gt;&lt;execution-results&gt;&lt;result identifier="processId"&gt;&lt;long&gt;1&lt;/long&gt;&lt;/result&gt;&lt;/execution-results&gt;</programlisting>

  </section>

</chapter>
