0

In AEM CQ , I am using asset manager api to write content(images uploaded from) in dam. This triggers out of the box Dam Update Asset workflow. I require to read renditions and asset properties that would be written available once the workflow is completed. My question is how to wait until the workflow is completed for reading the asset properties instead of thread.sleep.

I tried with a recursive function call to iterate while the asset property is present. This gave null pointer exception. But when I put a thread.sleep of 50 ms inside the iteration it works for me.

Another approach I tried to get the workflow object inside the service to read workflow status but found that it takes few milliseconds for the ootb workflow to start after the content is written. Here also had to give thread.sleep.

One more attempt to use a event handler to listen workflow events. We are able to enter the event type as workflow completed. How to notify the service or jsp that the workflow is completed and we can read the asset properties and renditions?

It would be great if someone can share their suggestions feedback on the approach. Thank you.

akashdeep-mishra
  • 343
  • 4
  • 19

1 Answers1

1

You have the wrong approach to solve this problem. In my eyes you have exactly 2 reasonable solutions on this.

Create workflow process/step and extend the Dam Update Asset Workflow with your custom step.

OR

Create JCR observation listener and listen for Event.PROPERTY_ADDED for example or use the higher sling APIs and create event handler with the appropriate topic and than execute your business logic as soon the property you look for is added or changed.

Why not to use Thread.sleep() or other similar solution:

  • you don't know when the workflow is executed exactly. it may be delayed if many asssets are uploaded or just get stuck
  • you cannot assure that your thread will be able to execute it's logic. the instance may be stopped for example
  • creating a new tread for every resource uploaded may be expensive task. you also waste resources when you create an infinite loop and put those threads to sleep, than wake them and check again and again ... and so on until eventually the thread is able to do it's job
d33t
  • 1,143
  • 6
  • 15
  • Thank you for the ans. I get the approaches to read persist data as explained by you. But still not sure, how it would fit in my problem. I have an image component that uses htmlsmartimage xtype and in its rendering script I use asset manager api to write content(image uploaded using browse option) in dam for its rendition to be created. As this renditions are then read in the image.jsp for the lighbox to function. With suggested approach now my question is how to transfer control from image.jsp to the jcr listener and back to jsp without breaking the flow. Please help me understand this. – akashdeep-mishra Feb 12 '17 at 06:56
  • With the listener/handler option you don't have to wait or synchronize with the image.jsp, you just write your data as soon other data (e.g. property) is available. With the workflow process step on the other side, you can make sure that your logic is executed right after the meta data is extracted or the renditions are created. – d33t Feb 13 '17 at 09:41
  • The writeContentToDam service is called from the image.jsp which triggers dam update asset workflow (workflow will write asset property and renditions). Next in image.jsp is to read asset properties and renditions. But the problem is the ootb workflow is triggered with slight milliseconds delay and also takes some time in completing. – akashdeep-mishra Feb 13 '17 at 10:30
  • It seems that I cannot follow you. What do you exactly mean by "I am writing a content". Please extend or modify your question to be clear enough, maybe provide also an example or flow diagram. – d33t Feb 13 '17 at 10:51
  • I am using asset manager api to programmatically upload image into dam for its renditions to be created by ootb workflow. Since image uploaded using browse option are just stored as dialog property with binary file and we do not have its renditions. Once the workflow is completed, the renditions are available and can be read in the subsequent line to provide url for lightbox of the image. All this coding is done in the image.jsp of custom image component using xtype htmlsmartimage. Workflow takes time to start and time in completing. How to make sure the properties are available before we read – akashdeep-mishra Feb 13 '17 at 12:55
  • OK I understand now what you try to do. Well if it is not an option to force the users to use/upload the images directly in the dam by turning the browse option off and then reference just them in the components, you have to do all of this job in the backend. But you are trying to do all of this job just before rendering the output (e.g. image.jsp).You should implement my suggestion, but as soon the user posts the dialog(you can implement Sling Filter for example and dispatch the post request or again a handler/listener listening for new/changed components of certain type and containing file) – d33t Feb 13 '17 at 13:12