Month: January 2016

Dropwizard, CDI and Activiti

Posted on Updated on

As I like Dropwizard and CDI and as I started to study Activiti, I wanted to see how they could work together. The idea is to deploy a test bpmn that outputs a “hello world” and run it with a simple Dropwizard REST call using only pure Activiti Java API.

Ingredients

Dropwizard 0.9.2 (http://www.dropwizard.io/0.9.2/docs/)

CDI (http://weld.cdi-spec.org/)

Activiti 6.0.0. Beta2 (http://activiti.org/download.html)

Postgresql 9.5 (http://www.postgresql.org/)

I’ve already created a Github project  on Dropwizard and CDI demonstrating how they work together. I’ve used that as a base. The sources are available here: https://github.com/mpevec/dwtest-weld.

From the Activiti stack I’ve only taken Activiti Engine and embedded it in Dropwizard (java) app. I haven’t used neither Activiti REST (it’s a WAR file that needs to be deployed on servlet container) nor Spring to configure Activiti Engine. For the Activiti database, the Postgresql database has been used.

Creating Activiti DB

After downloading  and installing Postgresql I’ve prepared the database for Activiti. I’ve created a Role “activiti” and with it created a database “activiti”.

In a zip file of Activiti 6.0.0.Beta2 there is a folder “database” and subfolder “create”. Inside, there are three SQL scripts that create all the DB objects needed by Activiti. You can easily run them within the newly created db.

Then I’ve created a DB configuration needed by Activiti inside the Dropwizard config.yml:

activiti:
   jdbcUrl: jdbc:postgresql://localhost:5432/activiti
   jdbcUsername: activiti
   jdbcPassword: activiti
   jdbcDriver: org.postgresql.Driver

POM configuration

There are the following dependencies within the POM file (for convenience I’ve left out the dependencies on CDI):

 <dependency>
     <groupId>org.activiti</groupId>
     <artifactId>activiti-engine</artifactId>
     <version>6.0.0.Beta2</version>
 </dependency>
 
 <dependency>
     <groupId>org.codehaus.groovy</groupId>
     <artifactId>groovy-all</artifactId>
     <version>2.4.5</version>
 </dependency>
 
 <dependency>
     <groupId>postgresql</groupId>
     <artifactId>postgresql</artifactId>
     <version>9.1-901-1.jdbc4</version>
 </dependency>

I’ve used groovy to output “Hello world” inside bpmn; hence we need its dependency.

Dropwizard

There’s nothing special here. I’ve just registered a REST resource and stored the reference to the Dropwizard configuration:

 @Override
 public void run(DwConfiguration config, Environment environment) throws Exception {
    ConfigurationHolder.set(config);
    environment.jersey().register(DwActivitResource.class);
 }

CDI producer

I’ve used a producer method for creating the Activiti Process Engine:

public class ProjectEngineFactory {
 
    @ApplicationScoped
    @StandaloneBinding
    @Produces
    public ProcessEngine getProcessEngine() {
       ProcessEngine processEngine = ProcessEngineConfiguration.createStandaloneProcessEngineConfiguration()
         .setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_FALSE)
         .setJdbcUrl(ConfigurationHolder.get().getActiviti().getJdbcUrl())
         .setJdbcUsername(ConfigurationHolder.get().getActiviti().getJdbcUsername())
         .setJdbcPassword(ConfigurationHolder.get().getActiviti().getJdbcPassword())
         .setJdbcDriver(ConfigurationHolder.get().getActiviti().getJdbcDriver())
         .setAsyncExecutorEnabled(true)
         .setAsyncExecutorActivate(false)
         .buildProcessEngine();
 
       return processEngine;
    }
}

REST resource

I’ve injected Activiti Process Engine, deployed test flow, run it and returned its id:

 @Path("activiti")
 public class DwActivitResource {
 
    @Inject
    @StandaloneBinding
    private ProcessEngine processEngine;
 
    @GET
    @Path("hello")
    @Produces(MediaType.TEXT_PLAIN)
    public String justTextMessage() {
 
       RepositoryService repositoryService = processEngine.getRepositoryService();
       RuntimeService runtimeService = processEngine.getRuntimeService();
 
       Deployment deployment = repositoryService
          .createDeployment()
          .addClasspathResource("helloWorldGroovy.bpmn20.xml").deploy();
 
       ProcessInstance processInstance = runtimeService
          .startProcessInstanceByKey("helloWorld");
       return processInstance.getId();
    }
    ...
}

Now when you call http://localhost:9999/v2/activiti/hello you should see the process instance id and “hello world” in the console.

That’s it, pretty simple. The entire code is on the Github: https://github.com/mpevec/dwtest-activiti.

Advertisement