private void determineDefaultPartition() { final Topology topology = client.newTopologyRequest().send().join(); defaultPartition = -1; final List<BrokerInfo> topologyBrokers = topology.getBrokers(); for (final BrokerInfo leader : topologyBrokers) { final List<PartitionInfo> partitions = leader.getPartitions(); for (final PartitionInfo brokerPartitionState : partitions) { if (brokerPartitionState.isLeader()) { defaultPartition = brokerPartitionState.getPartitionId(); break; } } } if (defaultPartition < 0) { throw new RuntimeException("Could not detect leader for default partition"); } }
/** * Publishes a new message to the broker. * * @param messageName name of the message * @param correlationKey correlation key */ public void publishMessage(String messageName, String correlationKey) { clientRule .getClient() .newPublishMessageCommand() .messageName(messageName) .correlationKey(correlationKey) .send() .join(); }
public static void main(final String[] args) { final String broker = "127.0.0.1:26500"; final ZeebeClientBuilder builder = ZeebeClient.newClientBuilder().brokerContactPoint(broker); try (ZeebeClient client = builder.build()) { System.out.println("Requesting topology with initial contact point " + broker); final Topology topology = client.newTopologyRequest().send().join(); System.out.println("Topology:"); topology .getBrokers() .forEach( b -> { System.out.println(" " + b.getAddress()); b.getPartitions() .forEach( p -> System.out.println( " " + p.getPartitionId() + " - " + p.getRole())); }); System.out.println("Done."); } } }
/** * Creates a workflow instance for the given process ID, with the given payload. * * @param processId BPMN process ID * @param payload initial payload for the instance * @return unique ID used to interact with the instance */ public long createWorkflowInstance(String processId, Map<String, Object> payload) { return clientRule .getClient() .newCreateInstanceCommand() .bpmnProcessId(processId) .latestVersion() .payload(payload) .send() .join() .getWorkflowInstanceKey(); }
@StreamListener(target = Sink.INPUT, condition = "(headers['messageType']?:'')=='PaymentReceivedEvent'") @Transactional public void paymentReceived(String messageJson) throws Exception { Message<PaymentReceivedEventPayload> message = new ObjectMapper().readValue(messageJson, new TypeReference<Message<PaymentReceivedEventPayload>>() {}); PaymentReceivedEventPayload event = message.getPayload(); // TODO: Read something from it? zeebe.workflowClient().newPublishMessageCommand() // .messageName(message.getMessageType()) .correlationKey(message.getCorrelationId()) .payload("{\"paymentInfo\": \"YeahWeCouldAddSomething\"}") .send().join(); System.out.println("Correlated " + message ); }
public static void main(final String[] args) { final String broker = "127.0.0.1:26500"; final String bpmnProcessId = "demoProcess"; final ZeebeClientBuilder builder = ZeebeClient.newClientBuilder().brokerContactPoint(broker); try (ZeebeClient client = builder.build()) { System.out.println("Creating workflow instance"); final WorkflowInstanceEvent workflowInstanceEvent = client .newCreateInstanceCommand() .bpmnProcessId(bpmnProcessId) .latestVersion() .send() .join(); System.out.println( "Workflow instance created with key: " + workflowInstanceEvent.getWorkflowInstanceKey()); } } }
private void executeJob(ActivatedJob job, Runnable doneCallback) { try { handler.handle(jobClient, job); } catch (Exception e) { LOG.warn( "Worker {} failed to handle job with key {} of type {}, sending fail command to broker", job.getWorker(), job.getKey(), job.getType(), e); jobClient .newFailCommand(job.getKey()) .retries(job.getRetries() - 1) .errorMessage(e.getMessage()) .send(); } finally { doneCallback.run(); } } }
public static void main(final String[] args) { final String broker = "localhost:26500"; final ZeebeClientBuilder clientBuilder = ZeebeClient.newClientBuilder().brokerContactPoint(broker); try (ZeebeClient client = clientBuilder.build()) { final DeploymentEvent deploymentEvent = client.newDeployCommand().addResourceFromClasspath("demoProcess.bpmn").send().join(); System.out.println("Deployment created with key: " + deploymentEvent.getKey()); } } }
public static void main(final String[] args) { final String broker = "localhost:26500"; final ZeebeClientBuilder clientBuilder = ZeebeClient.newClientBuilder().brokerContactPoint(broker); try (ZeebeClient client = clientBuilder.build()) { final Workflows workflows = client.newWorkflowRequest().send().join(); System.out.println("Printing all deployed workflows:"); workflows .getWorkflows() .forEach( wf -> { System.out.println("Workflow resource for " + wf + ":"); final WorkflowResource resource = client.newResourceRequest().workflowKey(wf.getWorkflowKey()).send().join(); System.out.println(resource); }); System.out.println("Done"); } } }
@Override public void handle(final JobClient client, final ActivatedJob job) { // read the payload of the job final Order order = job.getPayloadAsType(Order.class); System.out.println("new job with orderId: " + order.getOrderId()); // update the payload and complete the job order.setTotalPrice(46.50); client.newCompleteCommand(job.getKey()).payload(order).send(); } }
/** * Deploys the given workflow to the broker. Note that the filename must have the "bpmn" file * extension, e.g. "resource.bpmn". * * @param workflow workflow to deploy * @param filename resource name, e.g. "workflow.bpmn" */ public void deployWorkflow(BpmnModelInstance workflow, String filename) { clientRule.getClient().newDeployCommand().addWorkflowModel(workflow, filename).send().join(); }
/** * Creates a workflow instance for the given process ID, with the given payload. * * @param processId BPMN process ID * @param payload initial payload for the instance * @return unique ID used to interact with the instance */ public long createWorkflowInstance(String processId, Map<String, Object> payload) { return clientRule .getClient() .newCreateInstanceCommand() .bpmnProcessId(processId) .latestVersion() .payload(payload) .send() .join() .getWorkflowInstanceKey(); }
private void determineDefaultPartition() { final Topology topology = client.newTopologyRequest().send().join(); defaultPartition = -1; final List<BrokerInfo> topologyBrokers = topology.getBrokers(); for (final BrokerInfo leader : topologyBrokers) { final List<PartitionInfo> partitions = leader.getPartitions(); for (final PartitionInfo brokerPartitionState : partitions) { if (brokerPartitionState.isLeader()) { defaultPartition = brokerPartitionState.getPartitionId(); break; } } } if (defaultPartition < 0) { throw new RuntimeException("Could not detect leader for default partition"); } }
@StreamListener(target = Sink.INPUT, condition = "(headers['messageType']?:'')=='GoodsFetchedEvent'") @Transactional public void goodsFetchedReceived(String messageJson) throws Exception { Message<GoodsFetchedEventPayload> message = new ObjectMapper().readValue(messageJson, new TypeReference<Message<GoodsFetchedEventPayload>>() {}); String pickId = message.getPayload().getPickId(); zeebe.workflowClient().newPublishMessageCommand() // .messageName(message.getMessageType()) .correlationKey(message.getCorrelationId()) .payload("{\"pickId\":\"" + pickId + "\"}") // .send().join(); System.out.println("Correlated " + message ); }
/** * Publishes a new message to the broker. * * @param messageName name of the message * @param correlationKey correlation key */ public void publishMessage(String messageName, String correlationKey) { clientRule .getClient() .newPublishMessageCommand() .messageName(messageName) .correlationKey(correlationKey) .send() .join(); }
private void executeJob(ActivatedJob job, Runnable doneCallback) { try { handler.handle(jobClient, job); } catch (Exception e) { LOG.warn( "Worker {} failed to handle job with key {} of type {}, sending fail command to broker", job.getWorker(), job.getKey(), job.getType(), e); jobClient .newFailCommand(job.getKey()) .retries(job.getRetries() - 1) .errorMessage(e.getMessage()) .send(); } finally { doneCallback.run(); } } }
@Bean public ZeebeClient zeebe() { System.out.println("Connect to Zeebe at '" + zeebeBrokerContactPoint + "'"); // Cannot yet use Spring Zeebe in current alpha ZeebeClient zeebeClient = ZeebeClient.newClientBuilder() // .brokerContactPoint(zeebeBrokerContactPoint) // .build(); // Trigger deployment zeebeClient.workflowClient().newDeployCommand() // .addResourceFromClasspath("order-kafka.bpmn") // .send().join(); return zeebeClient; }
/** * Deploys the given workflow to the broker. Note that the filename must have the "bpmn" file * extension, e.g. "resource.bpmn". * * @param workflow workflow to deploy * @param filename resource name, e.g. "workflow.bpmn" */ public void deployWorkflow(BpmnModelInstance workflow, String filename) { clientRule.getClient().newDeployCommand().addWorkflowModel(workflow, filename).send().join(); }
public static void main(final String[] args) { final String broker = "127.0.0.1:26500"; final ZeebeClientBuilder builder = ZeebeClient.newClientBuilder().brokerContactPoint(broker); try (ZeebeClient client = builder.build()) { final Order order = new Order(); order.setOrderId(31243); client .newCreateInstanceCommand() .bpmnProcessId("demoProcess") .latestVersion() .payload(order) .send() .join(); client.newWorker().jobType("foo").handler(new DemoJobHandler()).open(); // run until System.in receives exit command waitUntilSystemInput("exit"); } }
@StreamListener(target = Sink.INPUT, condition = "(headers['messageType']?:'')=='GoodsShippedEvent'") @Transactional public void goodsShippedReceived(String messageJson) throws Exception { Message<GoodsShippedEventPayload> message = new ObjectMapper().readValue(messageJson, new TypeReference<Message<GoodsShippedEventPayload>>() {}); String shipmentId = message.getPayload().getShipmentId(); zeebe.workflowClient().newPublishMessageCommand() // .messageName(message.getMessageType()) .correlationKey(message.getCorrelationId()) .payload("{\"shipmentId\":\"" + shipmentId + "\"}") // .send().join(); System.out.println("Correlated " + message ); } }