My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
* Copyright 2011 LMAX Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.lmax.disruptor;

import java.util.concurrent.atomic.AtomicBoolean;

/**
* Convenience class for handling the batching semantics of consuming entries from a {@link RingBuffer}
* and delegating the available events to a {@link EventHandler}.
*
* If the {@link EventHandler} also implements {@link LifecycleAware} it will be notified just after the thread
* is started and just before the thread is shutdown.
*
* @param <T> event implementation storing the data for sharing during exchange or parallel coordination of an event.
*/
public final class BatchEventProcessor<T>
implements EventProcessor
{
private final AtomicBoolean running = new AtomicBoolean(false);
private ExceptionHandler exceptionHandler = new FatalExceptionHandler();
private final RingBuffer<T> ringBuffer;
private final SequenceBarrier sequenceBarrier;
private final EventHandler<T> eventHandler;
private final Sequence sequence = new Sequence(Sequencer.INITIAL_CURSOR_VALUE);

/**
* Construct a {@link EventProcessor} that will automatically track the progress by updating its sequence when
* the {@link EventHandler#onEvent(Object, long, boolean)} method returns.
*
* @param ringBuffer to which events are published.
* @param sequenceBarrier on which it is waiting.
* @param eventHandler is the delegate to which events are dispatched.
*/
public BatchEventProcessor(final RingBuffer<T> ringBuffer,
final SequenceBarrier sequenceBarrier,
final EventHandler<T> eventHandler)
{
this.ringBuffer = ringBuffer;
this.sequenceBarrier = sequenceBarrier;
this.eventHandler = eventHandler;

if (eventHandler instanceof SequenceReportingEventHandler)
{
((SequenceReportingEventHandler<?>)eventHandler).setSequenceCallback(sequence);
}
}

@Override
public Sequence getSequence()
{
return sequence;
}

@Override
public void halt()
{
running.set(false);
sequenceBarrier.alert();
}

/**
* Set a new {@link ExceptionHandler} for handling exceptions propagated out of the {@link BatchEventProcessor}
*
* @param exceptionHandler to replace the existing exceptionHandler.
*/
public void setExceptionHandler(final ExceptionHandler exceptionHandler)
{
if (null == exceptionHandler)
{
throw new NullPointerException();
}

this.exceptionHandler = exceptionHandler;
}

/**
* It is ok to have another thread rerun this method after a halt().
*/
@Override
public void run()
{
if (!running.compareAndSet(false, true))
{
throw new IllegalStateException("Thread is already running");
}
sequenceBarrier.clearAlert();

notifyStart();

T event = null;
long nextSequence = sequence.get() + 1L;
while (true)
{
try
{
final long availableSequence = sequenceBarrier.waitFor(nextSequence);
while (nextSequence <= availableSequence)
{
event = ringBuffer.get(nextSequence);
eventHandler.onEvent(event, nextSequence, nextSequence == availableSequence);
nextSequence++;
}

sequence.set(nextSequence - 1L);
}
catch (final AlertException ex)
{
if (!running.get())
{
break;
}
}
catch (final Throwable ex)
{
exceptionHandler.handleEventException(ex, nextSequence, event);
sequence.set(nextSequence);
nextSequence++;
}
}

notifyShutdown();

running.set(false);
}

private void notifyStart()
{
if (eventHandler instanceof LifecycleAware)
{
try
{
((LifecycleAware)eventHandler).onStart();
}
catch (final Throwable ex)
{
exceptionHandler.handleOnStartException(ex);
}
}
}

private void notifyShutdown()
{
if (eventHandler instanceof LifecycleAware)
{
try
{
((LifecycleAware)eventHandler).onShutdown();
}
catch (final Throwable ex)
{
exceptionHandler.handleOnShutdownException(ex);
}
}
}
}

Change log

r494 by mikeb01 on Dec 10, 2011   Diff
Remove warnings
Go to: 
Project members, sign in to write a code review

Older revisions

r468 by mjpt777 on Nov 2, 2011   Diff
Improved ExceptionHandler to support
Throwable and notify of onStart and
onShutdown errors.
r466 by mjpt777 on Nov 1, 2011   Diff
Use instanceof rather than
isAssignableFrom to simplify code.
r427 by mjpt777 on Sep 19, 2011   Diff
Update of WorkerPool and code tidy up
based on feedback.
All revisions of this file

File info

Size: 5232 bytes, 168 lines
Powered by Google Project Hosting