StateMachineResource { @POST @Consumes(MediaType.APPLICATION_JSON) @Timed public Response createStateMachine(StateMachineDefinition stateMachineDefinition) throws Exception { if (stateMachineDefinition == null) throw new IllegalRepresentationException("State machine definition is empty"); StateMachine stateMachine = null; final String stateMachineInstanceId; if (stateMachineDefinition.getCorrelationId() != null && !stateMachineDefinition.getCorrelationId().isEmpty()) { stateMachineInstanceId = stateMachineDefinition.getCorrelationId(); } else { stateMachineInstanceId = UUID.randomUUID().toString(); } if (stateMachineDefinition.getClientElbId() == null) { stateMachineDefinition.setClientElbId(DEFAULT_ELB_ID); } try { stateMachine = createAndInitStateMachine(stateMachineInstanceId, stateMachineDefinition); metricsClient.markMeter(new StringBuilder(). append("stateMachine."). append(stateMachine.getName()). append(".started").toString()); } catch (ConstraintViolationException ex) { return Response.status(Response.Status.CONFLICT.getStatusCode()).entity(ex.getCause() != null ? ex.getCause().getMessage() : null).build(); } catch (Exception ex) { logger.error("Failed During Creating or Initiating StateMachine with id {} {}", stateMachineInstanceId, ex.getStackTrace()); return Response.status(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()).entity(ex.getCause() != null ? ex.getCause().getMessage() : null).build(); } return Response.status(Response.Status.CREATED.getStatusCode()).entity(stateMachine.getId()).build(); } @Inject  StateMachineResource(EventsDAO eventsDAO, StateMachinePersistenceService stateMachinePersistenceService,
                                AuditDAO auditDAO, StateMachinesDAO stateMachinesDAO, StatesDAO statesDAO,
                                WorkFlowExecutionController workFlowExecutionController, MetricsClient metricsClient,
                                ParallelScatterGatherQueryHelper parallelScatterGatherQueryHelper,
                                EventSchedulerRegistry eventSchedulerRegistry,
                                EventProxyConnector eventProxyConnector,
                                @Named("eventProxyForMigration.enabled") String eventProxyEnabled); @POST @Consumes(MediaType.APPLICATION_JSON) @Timed Response createStateMachine(StateMachineDefinition stateMachineDefinition); @POST @Path("/{machineId}/context/events") @Timed Response submitEvent(@PathParam("machineId") String machineId,
                                @QueryParam("searchField") String searchField,
                                @QueryParam("triggerTime") Long triggerTime,
                                EventData eventData
    ); @POST @Path("/{machineId}/context/eventandstatus") @Timed Response submitEvent(@PathParam("machineId") String machineId,
                                EventAndExecutionData eventAndExecutionData
    ); @POST @Path("/{machineId}/context/eventupdate") @Timed Response updateEvent(@PathParam("machineId") String machineId,
                                EventData eventData
    ); boolean validateEventUpdate(List<Object[]> states); @POST @Path("/{machineId}/{stateId}/status") @Timed Response updateStatus(@PathParam("machineId") String machineId,
                                 @PathParam("stateId") Long stateId,
                                 ExecutionUpdateData executionUpdateData
    ); @POST @Path("/{machineId}/{stateId}/retries/inc") @Transactional @SelectDataSource(type = DataSourceType.READ_WRITE, storage = Storage.SHARDED) Response incrementRetry(@PathParam("machineId") String machineId,
                                   @PathParam("stateId") Long stateId
    ); @POST @Produces(MediaType.APPLICATION_JSON) @Path("/redrivetask/{machineId}/taskId/{taskId}") @Timed Response redriveTask(@PathParam("machineId") String machineId, @PathParam("taskId") Long taskId); @GET @Path("/{machineId}/fsmdata") @Produces(MediaType.APPLICATION_JSON) Response getFsmGraphData(@PathParam("machineId") String machineId); @GET @Path("/{stateMachineName}/states/errored") @Produces(MediaType.APPLICATION_JSON) Response getErroredStates(@PathParam("stateMachineName") String stateMachineName,
                                     @QueryParam("fromTime") String fromTime,
                                     @QueryParam("toTime") String toTime); @GET @Path("/{stateMachineName}/states/listbytime") @Produces(MediaType.APPLICATION_JSON) Response getStatesByTime(@PathParam("stateMachineName") String stateMachineName,
                                    @QueryParam("fromTime") String fromTime,
                                    @QueryParam("toTime") String toTime,
                                    @QueryParam("stateName") String stateName,
                                    @QueryParam("statuses") final List<String> statusStrings); @PUT @Path("/{stateMachineId}/{stateId}/unsideline") @Produces(MediaType.APPLICATION_JSON) @Transactional @SelectDataSource(type = DataSourceType.READ_WRITE, storage = Storage.SHARDED) Response unsidelineState(@PathParam("stateMachineId") String stateMachineId, @PathParam("stateId") Long stateId); @PUT @Path("/{stateMachineId}/cancel") @Produces(MediaType.APPLICATION_JSON) @Transactional @SelectDataSource(type = DataSourceType.READ_WRITE, storage = Storage.SHARDED) Response cancelWorkflow(@PathParam("stateMachineId") String stateMachineId,
                                   @QueryParam("searchField") String searchField); @GET @Path("/{stateMachineId}/info") @Produces(MediaType.APPLICATION_JSON) @Transactional @SelectDataSource(type = DataSourceType.READ_WRITE, storage = Storage.SHARDED) Response getStateMachine(@PathParam("stateMachineId") String stateMachineId,
                                    @QueryParam("searchField") String searchField);  }
@Test public void testFsmGraphCreation() throws Exception { final StateMachine stateMachine = stateMachinePersistenceService.createStateMachine(standardStateMachine, objectMapper.readValue(this.getClass().getClassLoader().getResource("state_machine_definition_fork_join.json"), StateMachineDefinition.class)); final HttpResponse<String> stringHttpResponse = Unirest.get(STATE_MACHINE_RESOURCE_URL + "/" + stateMachine.getId() + "/fsmdata").header("Content-Type", "application/json").asString(); assertThat(stringHttpResponse.getStatus()).isEqualTo(Response.Status.OK.getStatusCode()); }
@Test public void testCancelWorkflow_withCorrelationId() throws Exception { final StateMachine sm = stateMachinePersistenceService.createStateMachine("magic_number_1", objectMapper.readValue(this.getClass().getClassLoader().getResource("state_machine_definition.json"), StateMachineDefinition.class)); String stateMachineId = sm.getId(); Unirest.put(STATE_MACHINE_RESOURCE_URL + SLASH + stateMachineId + "/cancel?searchField=correlationId").asString(); Thread.sleep(200); StateMachine cancelledSM = stateMachinesDAO.findById(stateMachineId); assertThat(cancelledSM.getStatus()).isEqualTo(StateMachineStatus.cancelled); cancelledSM.getStates().forEach(st -> { assertThat(st.getStatus()).isEqualTo(Status.cancelled); }); }