Processing Chained Work Items— Service Connector API Example (Java)
An example is provided that illustrates processing chained work items using method calls available in the Service Connector API.
The step numbers in the comments correspond to the steps shown in Chained Work Items.
private void completeChainedWorkItem() { try { // Step 1: Allocate the work item // First we setup arrays containing the items to allocate and the resources to allocate to // The items to allocate ManagedObjectID[] itemsToAllocate = new ManagedObjectID[1]; ManagedObjectID itemToAllocate = ManagedObjectID.Factory.newInstance(); itemToAllocate.setId(84); itemsToAllocate[0] = itemToAllocate; // The resource to allocate to String[] resourcesToAllocate = new String[]{"tibco-admin"}; // Do the allocation com.tibco.n2.brm.api.WorkItem[] itemsAllocated = serviceConnectorInstance.allocateWorkItem(itemsToAllocate, resourcesToAllocate); // Step 2: Open the work item // Create a request to open the work item we are interested in BaseWorkRequest openRequest = BaseWorkRequest.Factory.newInstance(); WorkItem item = openRequest.addNewWorkItem(); // Use the ID we got back from the allocateWorkItem call, in this case we only have 1 item.setId(itemsAllocated[0].getId().getId()); openRequest.setResourceId("tibco-admin"); // Open the item WorkResponse openResponse = serviceConnectorInstance.openWorkItem(openRequest); // Step 3: Complete the item ( continue until no more items are available ) // Setup variable to hold details of the item we will complete, initially this will be // the item we opened, then it will be the item returned by the complete call for each item in the chain. long itemIdToComplete = openResponse.getWorkItem().getId(); long itemVersionToComplete = openResponse.getWorkItem().getVersion(); String itemWTUidToComplete = openResponse.getWorkTypeDetail().getUid(); String itemWTVersionToComplete = openResponse.getWorkTypeDetail().getVersion(); // Flag to indicate whether we have run out of items and should break out of the loop boolean nextItemAvailable = true; while (nextItemAvailable) { // Create a request WorkRequest completeRequest = WorkRequest.Factory.newInstance(); // Add the item to the request item = completeRequest.addNewWorkItem(); item.setId(itemIdToComplete); item.setVersion(itemVersionToComplete); completeRequest.setResourceId("tibco-admin"); // Add the work type to the request WorkTypeDetail detail = WorkTypeDetail.Factory.newInstance(); completeRequest.setWorkTypeDetail(detail); detail.setUid(itemWTUidToComplete); detail.setVersion(itemWTVersionToComplete); // Add the data to the request DataPayload payload = DataPayload.Factory.newInstance(); payload.setPayloadMode(PayloadModeType.JSON); payload.setSerializedPayload(""); completeRequest.setPayloadDetails(payload); // Complete the item WorkResponse completeResponse = serviceConnectorInstance.completeWorkItem(completeRequest); // Check the response to see if another item is available if (completeResponse.getWorkItem() == null) { // If no item available break out of the loop - chaining is now finished nextItemAvailable = false; } else { // Update the details for the item we will complete next itemIdToComplete = completeResponse.getWorkItem().getId(); itemVersionToComplete = completeResponse.getWorkItem().getVersion(); itemWTUidToComplete = completeResponse.getWorkTypeDetail().getUid(); itemWTVersionToComplete = completeResponse.getWorkTypeDetail().getVersion(); } } } catch (InvalidWorkRequestFault e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (WorkItemUnavailableFault e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (com.tibco.n2.wp.services.InternalServiceFault e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (WorkProcessingFault e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ChainedTimeOutFault e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InvalidWorkItemFault e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InvalidEntityFault e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InvalidVersionFault e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (com.tibco.n2.brm.services.InternalServiceFault e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (com.tibco.n2.brm.services.SecurityFault e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
Copyright © Cloud Software Group, Inc. All rights reserved.