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"
        <schedule id="embargoSchedule">
            <!-- Every 2 minutes -->
            <cronExpression>0 0/2 * 1/1 * ? *</cronExpression>

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"

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



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();

    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(" WHERE ");
            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++) {

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

Thank you in advance.

0 votes

1 answers



Forget about it I found the solution thanks to this link:

0 votes

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

    if (!(ctx instanceof DocumentEventContext)) {
    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

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?