Getting the session inside a event

Hello! I have implemented a scheduler associated with an event. I would like to search documents inside it with the session. Unfortunately when I try to get the session from the event it returns null. There's the code of my scheduler:

 <?xml version="1.0"?>
<component name="fr.sword.bdf.arche.distribution.core.schedule">
    <extension target="org.nuxeo.ecm.core.scheduler.SchedulerService"
        point="schedule">
        <schedule id="embargoSchedule">
            <eventId>EmbargoEvent</eventId>
            <eventCategory>default</eventCategory>
            <!-- Every 2 minutes -->
            <cronExpression>0 0/2 * 1/1 * ? *</cronExpression>
        </schedule>
    </extension>
</component>

The code of my event contrib:

<?xml version="1.0"?>
<component name="fr.sword.bdf.arche.distribution.core.event">

    <extension target="org.nuxeo.ecm.core.event.EventServiceComponent"
        point="listener">

        <listener name="EmbargoEvent" async="false" postCommit="true"
            priority="140" class="fr.sword.bdf.arche.distribution.core.event.EmbargoEvent">
            <event>EmbargoEvent</event>
        </listener>

    </extension>

</component>

And the code of the event listener:

public class EmbargoEvent implements EventListener {

    private static final Log LOG = LogFactory.getLog(EmbargoEvent.class);

    public static final String EVENT_NAME = "EmbargoEvent";

    protected Lock lock = new ReentrantLock();

    @Override
    public void handleEvent(Event event) throws ClientException {
        if (event != null && EVENT_NAME.equals(event.getName())) {
            final EventContext context = event.getContext();
            final CoreSession session = context.getCoreSession();
            List<DocumentModel> docs = getDocListEmbargo(session, event);
            LOG.error("Size docs: " + docs.size());
        }
    }

    public static List<DocumentModel> getDocListEmbargo(CoreSession session, Event event) throws ClientException {
        try {
            List<DocumentModel> results = new ArrayList<DocumentModel>();

            // Requête de récupération des documents statut
            final StringBuilder query = new StringBuilder("SELECT * FROM ");
            query.append(DocumentConstants.DOCUMENT_EXTERNE_DOC_TYPE);
            query.append(" WHERE ");
            query.append(DocumentConstants.DOCUMENT_EXTERNE_SCHEMA_PREFIX);
            query.append(" AND ");
            query.append("ecm:isProxy = 0 ");
            query.append(" AND ");
            query.append("ecm:currentLifeCycleState != 'deleted' ");
            query.append(" AND ");
            query.append("(dext:niveauConfidentialite = 2 OR dext:niveauConfidentialite = 3) ");

            final DocumentModelList docs = session.query(query.toString());

            for (int i = 0; i < docs.size(); i++) {
                results.add(docs.get(i));
            }

            return results;
        } catch (Exception e) {
            event.markRollBack(null, e);
            throw new ClientException(e);
        }
    }
}

Thank you in advance.

0 votes

1 answers

2500 views

ANSWER



Forget about it I found the solution thanks to this link: https://github.com/easysoa/EasySOA/wiki/Nuxeo-CoreSession-and-Transactions

0 votes



pmbroca i've tried this code ``` EventContext ctx = event.getContext();

    if (!(ctx instanceof DocumentEventContext)) {
        return;
    CoreSession session = ctx.getCoreSession();
but the condition ` if (!(ctx instanceof DocumentEventContext))` is true so the rest of code is not executed, can you tell more how did you succeed getting the session from the event
01/20/2016

just need a help…just need to know if you have created the scheduler as a nuxeo component and the listener as a document listener?
06/06/2016