Queues Robot Template

This robotic process template is ready to iterate through each pending item in the queue, change their status to FINISHED, and then close the queue.

This template is a more complex demonstration and contains a lot of lines of code. Read more about how to manage queues in the Appian RPA Console and the Queues module.

Workflow

The workflow contains seven actions: Start, Select queue, More items?, Log functional data, Update queue item, Close queue, and End.

queues-robot-workflow.png

Implementation

This section explains the RobotQueuesTemplate class included in the project.

Declaring variables and constants

For this robotic process, begin by defining the following objects to be used during the robotic process execution:

1
2
3
4
5
6
7
8
9
10
11
12
/** Server. */
private IJidokaServer<?> server;

/** The IQueueManager instance. */
private IQueueManager qmanager;

/** The current queue. */
private IQueue currentQueue;
/** The current item index. */
private int currentItemIndex;
/** The current item queue. */
private IQueueItem currentItemQueue;

startUp() method

The startUp() method initializes the Appian RPA modules the robotic process is going to use.

1
2
3
4
5
6
public boolean startUp() throws Exception {
	server = (IJidokaServer<?>) JidokaFactory.getServer();
	// intialize the QueueManager
	qmanager = server.getQueueManager();
	return IRobot.super.startUp();
}

selectQueue() method

The selectQueue() method retrieves the queue to use.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public void selectQueue() throws Exception {

	if (StringUtils.isBlank(qmanager.preselectedQueue())) {
		server.warn("No queue selected");
		throw new Exception("No queue selected");
	} else {
		String selectedQueueID = qmanager.preselectedQueue();
		server.info("Selected queue ID: " + selectedQueueID);
		currentQueue = getQueueFromId(selectedQueueID);

		if (currentQueue == null) {
			server.debug("Queue not found");
			return;
		}
		server.setNumberOfItems(currentQueue.pendingItems());
	}
}

The most important line is String selectedQueueID = qmanager.preselectedQueue();.

This will return the identifier of the selected process queue in the robotic process execution window:

rpa-select-queue-method.png

hasMoreItems() method

This method gets the next item to process. To iterate in a queue, the code uses the reserveItem() method.

1
2
3
4
5
6
7
8
9
10
11
public String hasMoreItems() throws Exception {
	// retrieve the next item in the queue
	currentItemQueue = getNextItem(currentQueue);
	
	if (currentItemQueue != null) {
		// set the stats for the current item
		server.setCurrentItem(currentItemIndex++, currentItemQueue.key());
		return "yes";
	}
	return "no";
}
1
2
3
4
5
6
7
8
9
10
11
12
private IQueueItem getNextItem(IQueue currentQueue) throws JidokaQueueException {
	try {
		if (currentQueue == null) {
			return null;
		}
		ReserveItemParameters reserveItemsParameters = new ReserveItemParameters();
		reserveItemsParameters.setUseOnlyCurrentQueue(true);
		return qmanager.reserveItem(reserveItemsParameters);
	} catch (Exception e) {
		throw new JidokaQueueException(e);
	}
}

updateItemQueue() method

Once finished processing the item, the robotic process calls the releaseItem() method to remove the reserve of the item and to change the status. In this example, the method also changes the functional data included in the item.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public void updateItemQueue() throws JidokaQueueException {

	// override the functional data in the queue item
	Map<String, String> funcData = currentItemQueue.functionalData();

	String firstKey = funcData.keySet().iterator().next();

	try {	
		funcData.put(firstKey, funcData.get(firstKey) + " - MODIFIED");
		// release the item. The queue item result will be the same
		// as the currenItem
		ReleaseItemWithOptionalParameters rip = new ReleaseItemWithOptionalParameters();
		rip.functionalData(funcData);
		rip.setProcess(EQueueItemReleaseProcess.FINISHED_OK);
			
		// Is mandatory to set the current item result before releasing the queue item
		server.setCurrentItemResultToOK(currentItemQueue.key());
		qmanager.releaseItem(rip);

	} catch (Exception e) {
		throw new JidokaQueueException(e);
	}
}

closeQueue() method

When the robotic process doesn't have items pending to process, it will try to close the queue. In this example, we suppose that could be several instances of the same robotic process running at the same time, but only the last one to finish will close the queue.

The first step is to get exclusive access to the queue with the reserveQueue() method.

After that, the robotic process checks if there are any pending items to finish processing:

  • If yes, there is another instance of the robotic process running.
  • If not, this is the last instance of the robotic process running, and we close the queue.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
public void closeQueue() {

	try {
		// First we reserve the queue (other robots can't reserve the queue at the same time)
		ReserveQueueParameters rqp = new ReserveQueueParameters();
		rqp.setQueueId(currentQueue.queueId());
		IReservedQueue reservedQueue = qmanager.reserveQueue(rqp);
			
		// Robot can't reserve the current queue
		if (reservedQueue == null) {
			server.debug("Can't reserve the queue with ID: " + currentQueue.queueId());
			return;
		}

		// check for pending items.
		int pendingItems = reservedQueue.queue().pendingItems();

		ReleaseQueueParameters releaseQP = new ReleaseQueueParameters();
		server.info(String.format("The queue has %s pending items", pendingItems));

		if (pendingItems > 0) {
			// release the queue without closing
			releaseQP.closed(false);
			server.info("Queue released without closing");
		} else {

			// release the queue closing it
			releaseQP.closed(true);
			server.info("Queue closed and released");
		}
		qmanager.releaseQueue(releaseQP);

	} catch (Exception e) {
		throw new JidokaFatalException(e.getMessage(), e);
	}
}
Open in Github

On This Page

FEEDBACK