Calling an operation from an event listener: how to inject dependencies ?

Hi,

I need to call the “ExportStructureToFS” operation from an event listener in order to trigger a scheduled export mys documents.

I found this explanation about how to call operations from java code, but I can't get the AutomationService and OperationContext injected as suggested in the example.

I use this declarations:

@Context
protected AutomationService service;

@Context
protected OperationContext context;

Both service and context end being set to null using this technique.

What's wrong ?

Thanks

0 votes

1 answers

4169 views

ANSWER

Hello,

As I understood, those service and context are inside your custom operation "ExportStructureToFS".

Could you check if @Context is org.nuxeo.ecm.automation.core.annotations.Context?

If it is, can you communicate this operation and the contribution xml?

Thank you

06/04/2015

Hi,

I'm using this ready made operation https://github.com/nuxeo/nuxeo-fsexporter

I can get the descriptor with a get.

Dependency injection is used inside my event listener as follow:

import org.nuxeo.ecm.automation.AutomationService; import org.nuxeo.ecm.automation.OperationContext; import org.nuxeo.ecm.automation.core.annotations.Context;

public class FsExportListener implements EventListener {

private Log logger = LogFactory.getLog(FsExportListener.class);

@Context
protected AutomationService service;

@Context
protected OperationContext context;

private static final String FS_TARGET = "/home/nuxeo/exports";

private static final String[] ROOTS = { "/default-domain/workspaces/foo/bar/" };

@Override
public void handleEvent(Event event) throws ClientException {
    logger.debug("Handling an event...");
    logger.debug("Automation context: " + context); // null
    logger.debug("Automation service: " + service); // null
    Map<String, Object> parameters = new HashMap<String, Object>();
    parameters.put("File System Target", FS_TARGET);
    for (String root : ROOTS) {
        logger.debug("Root: " + root);
        parameters.put("Root Name", root);
        try {
            service.run(context, "ExportStructureToFS", parameters);
        } catch (OperationException e) {
            logger.error("Operation exception");
            logger.error(e.getMessage());
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
    }
}

}

Maybe I'm wrong ? Thanks btw

edit: I did not post the listener contrib, because the log file is recording debug but let me know if it's needed

06/04/2015

I've just read about the @context annotation and it seems to precisely be out of context here. If i'm right dependency injection is not available in this case except for unit testing.

I've just found this link

https://github.com/easysoa/EasySOA/wiki/Nuxeo-CoreSession-and-Transactions

So I will bet on using the event context and call the service with

Framework.getService(AutomationService.class)

06/04/2015



Dependency injection via the @context annotation works calling one operation from another.

In that case using:

AutomationService automationService = Framework.getService(AutomationService.class);

To get the service implementation

And

automationService.run(new OperationContext(), "ExportStructureToFS", parameters);

Solved my problem

0 votes