AEM CQ wait for workflow to finish

Viewed 1220

For my current AEM 6.0 project I need to create a custom workflow to create renditions. Were working on a shared platform with other AEM projects. Thats the reason why we arent allowed to adapt the original DAMUpdateAsset workflow (which renders the AEM specific renditions, thumbnails and so on).

My custom rendition workflow renders different renditions. The problem is, sometimes i recive exceptions like IllegalOakState when my workflow runs in parallel to the DAMUpdateAsset workflow.

What I need is a solution, to wait with my workflow until the DAMUpdateAsset workflow is finished.

Currently I am trying to get that done with an oak query

public static final String QUERY_STRING = "SELECT * FROM [nt:base] AS s WHERE ISDESCENDANTNODE([/etc/workflow/instances]) and CONTAINS('status', 'COMPLETED') and contains('modelId', '/etc/workflow/models/dam/update_asset/jcr:content/model') and [data/payload/path]='%s' and endTime > CAST('%s' AS DATE)";
private void waitForDamUpdateImage(Session session, String path) throws WorkflowException, InterruptedException
{
    final DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX", Locale.GERMAN);
    final Date now = new Date();
    final Calendar calendar = Calendar.getInstance();
    calendar.setTime(new Date(now.getTime() - 2 * 60 * 1000));

    String queryString = String.format(QUERY_STRING, path, formatter.format(calendar.getTime()));

    Iterator iterator;

    do
    {
        //wait(100); this wait will produce IllegalMonitor... Exception
        iterator = QueryUtils.query(session, queryString);
    }
    while (!iterator.hasNext());
}

I dont think that this is a optimal solution, i think there will be a much easier way to solve this problem.

Hopefully you got an idea of what my problem is and maybe help me solve this.

Thank you anyway ;)

2 Answers
Related