    001    /*
    002     * Licensed to the Apache Software Foundation (ASF) under one or more
    003     * contributor license agreements. See the NOTICE file distributed with
    004     * this work for additional information regarding copyright ownership.
    005     * The ASF licenses this file to You under the Apache license, Version 2.0
    006     * (the "License"); you may not use this file except in compliance with
    007     * the License. You may obtain a copy of the License at
    008     *
    009     *      http://www.apache.org/licenses/LICENSE-2.0
    010     *
    011     * Unless required by applicable law or agreed to in writing, software
    012     * distributed under the License is distributed on an "AS IS" BASIS,
    013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014     * See the license for the specific language governing permissions and
    015     * limitations under the license.
    016     */
    017    package org.apache.logging.log4j.core.async;
    018
    019    import com.lmax.disruptor.Sequence;
    020    import com.lmax.disruptor.SequenceReportingEventHandler;
    021
    022    /**
    023     * This event handler gets passed messages from the RingBuffer as they become
    024     * available. Processing of these messages is done in a separate thread,
    025     * controlled by the {@code Executor} passed to the {@code Disruptor}
    026     * constructor.
    027     */
    028    public class RingBufferLogEventHandler implements
    029            SequenceReportingEventHandler<RingBufferLogEvent> {
    030
    031        private static final int NOTIFY_PROGRESS_THRESHOLD = 50;
    032        private Sequence sequenceCallback;
    033        private int counter;
    034
    035        @Override
    036        public void setSequenceCallback(final Sequence sequenceCallback) {
    037            this.sequenceCallback = sequenceCallback;
    038        }
    039
    040        @Override
    041        public void onEvent(final RingBufferLogEvent event, final long sequence,
    042                final boolean endOfBatch) throws Exception {
    043            event.execute(endOfBatch);
    044            event.clear();
    045
    046            // notify the BatchEventProcessor that the sequence has progressed.
    047            // Without this callback the sequence would not be progressed
    048            // until the batch has completely finished.
    049            if (++counter > NOTIFY_PROGRESS_THRESHOLD) {
    050                sequenceCallback.set(sequence);
    051                counter = 0;
    052            }
    053        }
    054
    055    }