Http request plugin or message broker plugin #875
-
So i just want to utilize only imixs rest api and i want to ask is there any plugin that support http call after some process or subscribe to some message broker like kafka or google pubsub that we can use in model? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 15 replies
-
Hi @rizalalfarizi123, And yes, you can integrate Plugins or Adapters into your workflow to let the workflow engine itself call external micro services via Rest API. So your plugin code would look something like this: public class MyPlugin implements Plugin {
public void init(WorkflowContext actx) throws Exception {
}
public ItemCollection run(ItemCollection workitem, ItemCollection event) throws Exception {
try {
Client client = ClientBuilder.newClient();
WebTarget myResource = client.target("http://example.com/webapi/read");
String response = myResource.request(MediaType.TEXT_PLAIN).get(String.class);
// you business logic goes here....
} catch (Exception e) {
// signal exception....
throw new PluginExeption(e);
}
// signal success
return workitem;
}
public void close(boolean rollbackTransaction) throws Exception {
// close resources
}
}
The other point is your question about a message broker. You can find an implementation for Apache Kafka here. This is a kind of generic adapter to connect Imixs Workflow with a Kafka Queue. In both cases - whether rest client or message broker - communication can be bidirectional. Imix-Workflow can fetch data from an external service in order to supplement its own process instance with this data (e.g. address of a customer based on the existing customer number) or send data to a service itself (e.g. the release for an order from a workflow) In all cases you can configure the communication via your BPMN model. For example you can place the service endpoint or request type as configuration data into your BPMN Event. This could be similar like in the SplitAndJoinPlugin that provides a configuration layer to be defined in the Event-Workflow-Result. Let me know if this answers your question. |
Beta Was this translation helpful? Give feedback.
-
ok - understand. Look, this is how your plugin code looks in Imixs - there is no need for a special connector - you can use Jax-rs native: public class RestConnectorPlugin extends AbstractPlugin {
private static final Logger logger = Logger.getLogger(RestConnectorPlugin.class.getName());
@Inject
private WorkflowService workflowService;
@Override
public ItemCollection run(ItemCollection workitem, ItemCollection event)
throws PluginException {
logger.info("...connecting rest api...");
// read configuration from event
List<ItemCollection> apiDefinitions = workflowService.evalWorkflowResultXML(event, "api-connector",
"GET", workitem, false);
if (apiDefinitions != null) {
for (ItemCollection apiDefinition : apiDefinitions) {
String endpoint = apiDefinition.getItemValueString("endpoint");
String item = apiDefinition.getItemValueString("item");
// Create a JAX-RS client
Client client = ClientBuilder.newClient();
WebTarget target = client.target(endpoint);
Response response = target.request().get();
if (response.getStatus() == 200) {
// Parse the JSON response
String jsonResponse = response.readEntity(String.class);
JsonReader jsonReader = Json.createReader(new StringReader(jsonResponse));
JsonObject jsonObject = jsonReader.readObject();
String itemValue = jsonObject.getString(item);
// Update workitem
workitem.setItemValue(item, itemValue);
jsonReader.close();
} else {
throw new PluginException(RestConnectorPlugin.class.getSimpleName(), "API-ERROR",
"Failed to fetch data: " + response.getStatus());
}
response.close();
client.close();
}
}
return workitem;
}
} And this example allows you to configure the plugin by your BPMN Event: You can copy the code from here. Typically you will often have a more complicated Rest API. So the important part is the part with parsing the json result. I do not think that a generic plugin will help much here as you data will be more complex. For example you may want to joint data values or change the values somehow. The nice thing is, that you have in this example also a configuration layer. You can put the following example config into your workflow result in the modeller:
And you can easily extend the configuration to your own needs. I hope this helps you to get startet. |
Beta Was this translation helpful? Give feedback.
ok - understand.
Look, this is how your plugin code looks in Imixs - there is no need for a special connector - you can use Jax-rs native: