001/* 002 * (C) Copyright 2006-2016 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * tiry 018 */ 019package org.nuxeo.ecm.core.event.pipe; 020 021import org.apache.commons.logging.Log; 022import org.apache.commons.logging.LogFactory; 023import org.nuxeo.ecm.core.event.EventBundle; 024import org.nuxeo.ecm.core.event.EventServiceAdmin; 025import org.nuxeo.ecm.core.event.impl.AsyncEventExecutor; 026import org.nuxeo.ecm.core.event.impl.EventListenerDescriptor; 027import org.nuxeo.ecm.core.event.impl.EventListenerList; 028import org.nuxeo.runtime.api.Framework; 029 030import java.util.List; 031import java.util.Map; 032 033/** 034 * Consumes {@link EventBundle} EventBundles by running asynchronous {@link org.nuxeo.ecm.core.event.EventListener} 035 * 036 * @since 8.4 037 */ 038public abstract class AbstractListenerPipeConsumer<T> extends AbstractPipeConsumer<T> { 039 040 private static final Log log = LogFactory.getLog(AbstractListenerPipeConsumer.class); 041 042 protected volatile AsyncEventExecutor asyncExec; 043 044 protected boolean stopping; 045 046 @Override 047 public void initConsumer(String name, Map<String, String> params) { 048 super.initConsumer(name, params); 049 asyncExec = new AsyncEventExecutor(); 050 if (Framework.getRuntime() == null) { 051 throw new RuntimeException("Nuxeo Runtime not initialized"); 052 } 053 } 054 055 @Override 056 public void shutdown() throws InterruptedException { 057 stopping = true; 058 waitForCompletion(1000L); 059 } 060 061 @Override 062 protected boolean processEventBundles(List<EventBundle> bundles) { 063 EventServiceAdmin eventService = Framework.getService(EventServiceAdmin.class);// 064 EventListenerList listeners = eventService.getListenerList(); 065 List<EventListenerDescriptor> postCommitAsync = listeners.getEnabledAsyncPostCommitListenersDescriptors(); 066 067 // could introduce bulk mode for EventListeners 068 for (EventBundle eventBundle : bundles) { 069 asyncExec.run(postCommitAsync, eventBundle); 070 } 071 return true; 072 } 073 074 @Override 075 public boolean waitForCompletion(long timeoutMillis) throws InterruptedException { 076 return asyncExec.waitForCompletion(timeoutMillis); 077 } 078}