In our blog Activiti Message Signal Thrower, we showed how to implement a message thrower spring bean in Activiti and how to use Service Task to call that bean. But one workflow using a spring bean to throw a message signal while another workflow in a wait state is listening for a message signal might not be a very common scenario. The most common scenario would be that an external system would throw the message signal. So we thought it will be a good idea to build further on our previous blog about Message Signal and show how to leverage Activiti’s custom REST endpoints.
Activiti Custom REST Endpoints
As mentioned above, Activiti allows you to add custom REST endpoints to the BPM Suite. The REST API is built using Spring MVC and there are two types of REST API and these are as follows;
- Regular/Internal REST API – used by the BPM Suite html/JavaScript UI and uses cookies based authentication.
- Public REST API – uses basic authentication instead of cookies).
In our case since it’s an external system which is going to use out custom rest endpoint we will need to use the Public Rest API which is protected by basic authentication.
Implementing Message Signal Thrower REST Endpoint in Activiti
So we start by creating a java class called Message in the package com.activiti.extension.api. Note that putting the java class in that package makes it automatically available when Activiti is loaded and ensures it’s protected by basic authentication.
As shown in the snippet below the @RequestMapping notation is used to define the url and its mandatory to have /enterprise as first element in the url, as this is configured in the SecurityConfiguration to be protected with basic authentication (more specific, the api/enterprise/* is).
We are using @Autowired to leverage our existing Message Thrower bean (see Activiti Message Signal Thrower blog for details) but we could have implemented whole class or logic in our Message class as well.
And finally we call the sendMessage method with the parameters passed in from the request
import com.activiti.extension.bean.MessageThrower; @RestController @RequestMapping("/enterprise/message") public class Message { @Autowired private MessageThrower thrower; @RequestMapping(method = RequestMethod.GET) public String message(@RequestParam(value = "message") String messageId, @RequestParam(value = "name") String name, @RequestParam(value = "email") String email, @RequestParam(value = "phone") String phone) { thrower.sendMessage(messageId, name, email, phone); return "Message sent!"; } }
Calling our custom Public REST API
Since we defined the rest API HTTP method as get we can use the browser with the following sample url or use a REST client like postman
- Request
- http://seed.activiti/activiti-app/api/enterprise/message?message=workshopMessage&name=xxx&email=kk@seed&phone=91919191
- Response
- Message Sent!
Note since the api is protected by basic authentication, a valid username/password is required.