本文整理匯總了Java中com.lmax.disruptor.dsl.Disruptor.shutdown方法的典型用法代碼示例。如果您正苦於以下問題:Java Disruptor.shutdown方法的具體用法?Java Disruptor.shutdown怎麽用?Java Disruptor.shutdown使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類com.lmax.disruptor.dsl.Disruptor
的用法示例。
在下文中一共展示了Disruptor.shutdown方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: qndSingleThread
import com.lmax.disruptor.dsl.Disruptor; //導入方法依賴的package包/類
@SuppressWarnings("unchecked")
static void qndSingleThread(int numItems) {
final AtomicLong COUNTER_RECEIVED = new AtomicLong(0);
final Disruptor<LongEvent> disruptor = new Disruptor<>(LongEvent.FACTORY, 128,
Executors.defaultThreadFactory(), ProducerType.MULTI, new YieldingWaitStrategy());
disruptor.handleEventsWith(
(event, sequence, endOfBatch) -> COUNTER_RECEIVED.incrementAndGet());
disruptor.start();
final long t = System.currentTimeMillis();
for (int i = 0; i < numItems; i++) {
disruptor.publishEvent((event, seq) -> event.set(seq));
}
long d = System.currentTimeMillis() - t;
NumberFormat nf = NumberFormat.getInstance();
System.out.println("========== qndSingleThread:");
System.out.println("Sent: " + nf.format(numItems) + " / Received: "
+ nf.format(COUNTER_RECEIVED.get()) + " / Duration: " + d + " / Speed: "
+ NumberFormat.getInstance().format((numItems * 1000.0 / d)) + " items/sec");
disruptor.shutdown();
}
示例2: stop
import com.lmax.disruptor.dsl.Disruptor; //導入方法依賴的package包/類
public static void stop() {
final Disruptor<RingBufferLogEvent> temp = disruptor;
// Must guarantee that publishing to the RingBuffer has stopped
// before we call disruptor.shutdown()
disruptor = null; // client code fails with NPE if log after stop = OK
temp.shutdown();
// wait up to 10 seconds for the ringbuffer to drain
final RingBuffer<RingBufferLogEvent> ringBuffer = temp.getRingBuffer();
for (int i = 0; i < MAX_DRAIN_ATTEMPTS_BEFORE_SHUTDOWN; i++) {
if (ringBuffer.hasAvailableCapacity(ringBuffer.getBufferSize())) {
break;
}
try {
// give ringbuffer some time to drain...
Thread.sleep(HALF_A_SECOND);
} catch (final InterruptedException e) {
// ignored
}
}
executor.shutdown(); // finally, kill the processor thread
}
示例3: shouldProxy
import com.lmax.disruptor.dsl.Disruptor; //導入方法依賴的package包/類
@SuppressWarnings("StatementWithEmptyBody")
@Test
public void shouldProxy()
{
final Disruptor<ProxyMethodInvocation> disruptor = createDisruptor(Executors.newSingleThreadExecutor(), 1024);
final RingBufferProxyGeneratorFactory generatorFactory = new RingBufferProxyGeneratorFactory();
final RingBufferProxyGenerator ringBufferProxyGenerator = generatorFactory.newProxy(generatorType);
final ListenerImpl implementation = new ListenerImpl();
final Listener listener = ringBufferProxyGenerator.createRingBufferProxy(Listener.class, disruptor, OverflowStrategy.DROP, implementation);
disruptor.start();
for(int i = 0; i < 3; i++)
{
listener.onString("single string " + i);
listener.onFloatAndInt((float) i, i);
listener.onVoid();
listener.onObjectArray(new Double[]{(double) i});
listener.onMixedMultipleArgs(0, 1, "a", "b", 2);
}
RingBuffer<ProxyMethodInvocation> ringBuffer = disruptor.getRingBuffer();
while (ringBuffer.getMinimumGatingSequence() != ringBuffer.getCursor())
{
// Spin
}
disruptor.shutdown();
Executors.newSingleThreadExecutor().shutdown();
assertThat(implementation.getLastStringValue(), is("single string 2"));
assertThat(implementation.getLastFloatValue(), is((float) 2));
assertThat(implementation.getLastIntValue(), is(2));
assertThat(implementation.getVoidInvocationCount(), is(3));
assertThat(implementation.getMixedArgsInvocationCount(), is(3));
assertThat(implementation.getLastDoubleArray(), is(equalTo(new Double[] {(double) 2})));
}
示例4: main
import com.lmax.disruptor.dsl.Disruptor; //導入方法依賴的package包/類
public static void main(String[] args) {
ExecutorService executor = Executors.newCachedThreadPool();
int bufferSize = 1024;
WaitStrategy ws = new BlockingWaitStrategy();
Disruptor<CpuUsageEvent> disruptor = new Disruptor<>(factory, bufferSize, executor, ProducerType.SINGLE, ws);
disruptor.handleEventsWith(handler);
RingBuffer<CpuUsageEvent> ringBuffer = disruptor.start();
publishEvents(ringBuffer);
disruptor.shutdown();
executor.shutdown();
}
示例5: main
import com.lmax.disruptor.dsl.Disruptor; //導入方法依賴的package包/類
public static void main(String[] args) throws InterruptedException {
long beginTime=System.currentTimeMillis();
int bufferSize=1024;
ExecutorService executor=Executors.newFixedThreadPool(4);
//������캯�����������������˽�����2��demo֮��Ϳ��¾������ˣ���������~
Disruptor<TradeTransaction> disruptor=new Disruptor<TradeTransaction>(new EventFactory<TradeTransaction>() {
@Override
public TradeTransaction newInstance() {
return new TradeTransaction();
}
}, bufferSize, executor, ProducerType.SINGLE, new BusySpinWaitStrategy());
//ʹ��disruptor������������C1,C2
EventHandlerGroup<TradeTransaction> handlerGroup=disruptor.handleEventsWith(new TradeTransactionVasConsumer(),new TradeTransactionInDBHandler());
TradeTransactionJMSNotifyHandler jmsConsumer=new TradeTransactionJMSNotifyHandler();
//������C1,C2����֮��ִ��JMS��Ϣ���Ͳ��� Ҳ���������ߵ�C3
handlerGroup.then(jmsConsumer);
disruptor.start();//����
CountDownLatch latch=new CountDownLatch(1);
//��������
executor.submit(new TradeTransactionPublisher(latch, disruptor));
latch.await();//�ȴ�����������.
disruptor.shutdown();
executor.shutdown();
System.out.println("�ܺ�ʱ:"+(System.currentTimeMillis()-beginTime));
// long tt= System.currentTimeMillis();
// for (int i = 0; i < 1000; i++) {
// int j=i;
// }
// System.out.println("�ܺ�ʱ:"+(System.currentTimeMillis()-tt));
}
示例6: qndMultiThreads
import com.lmax.disruptor.dsl.Disruptor; //導入方法依賴的package包/類
@SuppressWarnings("unchecked")
static void qndMultiThreads(int numItems, int numThreads) throws InterruptedException {
final AtomicLong COUNTER_RECEIVED = new AtomicLong(0);
final Disruptor<LongEvent> disruptor = new Disruptor<>(LongEvent.FACTORY, 128,
Executors.defaultThreadFactory(), ProducerType.MULTI, new YieldingWaitStrategy());
disruptor.handleEventsWith(
(event, sequence, endOfBatch) -> COUNTER_RECEIVED.incrementAndGet());
disruptor.start();
final long t = System.currentTimeMillis();
final int numItemsPerThread = numItems / numThreads;
final Thread[] THREADS = new Thread[numThreads];
for (int i = 0; i < THREADS.length; i++) {
THREADS[i] = new Thread() {
public void run() {
for (int i = 0; i < numItemsPerThread; i++) {
disruptor.publishEvent((event, seq) -> event.set(seq));
}
}
};
THREADS[i].start();
}
for (Thread thread : THREADS) {
thread.join();
}
long d = System.currentTimeMillis() - t;
NumberFormat nf = NumberFormat.getInstance();
System.out.println("========== qndSingleThread:");
System.out.println("Sent: " + nf.format(numItems) + " / Received: "
+ nf.format(COUNTER_RECEIVED.get()) + " / Duration: " + d + " / Speed: "
+ NumberFormat.getInstance().format((numItems * 1000.0 / d)) + " items/sec");
disruptor.shutdown();
}
示例7: removeEventFlow
import com.lmax.disruptor.dsl.Disruptor; //導入方法依賴的package包/類
public void removeEventFlow(HandlerProcessor queryStreamProcessor) {
streamReceivers.remove(queryStreamProcessor);
if (disruptorEnabled) {
for (Disruptor disruptor : queryStreamProcessor.getDisruptors()) {
disruptor.shutdown();
}
}
}
示例8: removeEventFlows
import com.lmax.disruptor.dsl.Disruptor; //導入方法依賴的package包/類
public void removeEventFlows() {
for(StreamReceiver queryStreamProcessor:streamReceivers){
streamReceivers.remove(queryStreamProcessor);
if (disruptorEnabled) {
for (Disruptor disruptor : queryStreamProcessor.getDisruptors()) {
disruptor.shutdown();
}
}
}
}
示例9: release
import com.lmax.disruptor.dsl.Disruptor; //導入方法依賴的package包/類
/**
* Decreases the reference count. If the reference count reached zero, the
* Disruptor and its associated thread are shut down and their references
* set to {@code null}.
*/
synchronized static void release() {
if (--count > 0) {
LOGGER.trace("AsyncLoggerConfigHelper: not shutting down disruptor: ref count is {}.", count);
return;
}
final Disruptor<Log4jEventWrapper> temp = disruptor;
if (temp == null) {
LOGGER.trace("AsyncLoggerConfigHelper: disruptor already shut down: ref count is {}.", count);
return; // disruptor was already shut down by another thread
}
LOGGER.trace("AsyncLoggerConfigHelper: shutting down disruptor: ref count is {}.", count);
// Must guarantee that publishing to the RingBuffer has stopped
// before we call disruptor.shutdown()
disruptor = null; // client code fails with NPE if log after stop = OK
temp.shutdown();
// wait up to 10 seconds for the ringbuffer to drain
final RingBuffer<Log4jEventWrapper> ringBuffer = temp.getRingBuffer();
for (int i = 0; i < MAX_DRAIN_ATTEMPTS_BEFORE_SHUTDOWN; i++) {
if (ringBuffer.hasAvailableCapacity(ringBuffer.getBufferSize())) {
break;
}
try {
// give ringbuffer some time to drain...
Thread.sleep(HALF_A_SECOND);
} catch (final InterruptedException e) {
// ignored
}
}
executor.shutdown(); // finally, kill the processor thread
executor = null; // release reference to allow GC
}
示例10: run
import com.lmax.disruptor.dsl.Disruptor; //導入方法依賴的package包/類
/**
* Create a pool of consumers, all reading from a ring buffer to process events.
*
* @param NUM_THREADS total number of consumers to create in the pool
* @param totalEventsToPublish total events to publish to the queue
* @param ringConsumers the placeholder for creating consumers
* @throws InterruptedException if there is a problem talking to the blocking queue
*/
public void run(int NUM_THREADS, int totalEventsToPublish, RingEventHandlerConsumer<T>[] ringConsumers) throws InterruptedException {
final int RING_BUFFER_SIZE = 1024;
ExecutorService executor;
if(NUM_THREADS == 1) {
executor = Executors.newSingleThreadExecutor();
} else {
executor = Executors.newFixedThreadPool(NUM_THREADS);
}
Disruptor<T> disruptor =
new Disruptor<>(disruptorEventFactory, RING_BUFFER_SIZE, executor,
ProducerType.SINGLE,
new YieldingWaitStrategy());
for(int i = 0; i < NUM_THREADS; i++) {
ringConsumers[i] = new RingEventHandlerConsumer<T>(i, NUM_THREADS, consumerFactory.createConsumer(i, NUM_THREADS));
}
disruptor.handleEventsWith(ringConsumers);
RingBuffer<T> eventRingBuffer = disruptor.start();
final long startTime = System.currentTimeMillis();
publishEvents(totalEventsToPublish, eventRingBuffer);
disruptor.shutdown();
executor.shutdown();
final long endTime = System.currentTimeMillis();
logger.info("It took " + (endTime - startTime) + "ms to process " + totalEventsToPublish + " messages.");
for(RingEventHandlerConsumer consumer : ringConsumers) {
logger.info("Processed " + consumer.getMessagesProcessed() + " messages.");
}
}
示例11: shutdown
import com.lmax.disruptor.dsl.Disruptor; //導入方法依賴的package包/類
@Override
public void shutdown() {
LOG.info(format("shutting down the (Disruptor)ThreadBoundExecutor[%s]",threadFactory.toString()));
if (shuttingDown.compareAndSet(false, true)) {
for (Disruptor<ThreadBoundEventWrapper> disruptor : disruptors) {
// @todo: we may want to have a timeout here
disruptor.shutdown();
}
}
LOG.info(format("(Disruptor)ThreadBoundExecutor[%s] shut down completed",threadFactory.toString()));
}
示例12: testDisruptor
import com.lmax.disruptor.dsl.Disruptor; //導入方法依賴的package包/類
@SuppressWarnings("unchecked")
@Test
public void testDisruptor() {
ExecutorService exec = Executors.newCachedThreadPool();
// Preallocate RingBuffer with 1024 MetricEvents
Disruptor<ByteArrayHolder> disruptor = new Disruptor<ByteArrayHolder>(ByteArrayHolder.EVENT_FACTORY, 1024, exec);
final EventHandler<ByteArrayHolder> handler = new EventHandler<ByteArrayHolder>() {
// event will eventually be recycled by the Disruptor after it wraps
public void onEvent(final ByteArrayHolder event, final long sequence, final boolean endOfBatch) throws Exception {
System.out.println("Sequence: " + sequence);
System.out.println("MetricEvent: " + event.getValue());
}
};
// Build dependency graph
disruptor.handleEventsWith(handler);
RingBuffer<ByteArrayHolder> ringBuffer = disruptor.start();
for (long i = 10; i < 2000; i++) {
MetricPB metricToReport = MetricPB.newBuilder()
.setKey("test.metric")
.setTimestamp(123456789l)
.setValue(1.93f)
.build();
String uuid = UUID.randomUUID().toString();
// Two phase commit. Grab one of the 1024 slots
long seq = ringBuffer.next();
ByteArrayHolder valueEvent = ringBuffer.get(seq);
byte[] bytes = new byte[8];
//valueEvent.setValue(uuid);
valueEvent.setValue(metricToReport.toByteArray());
ringBuffer.publish(seq);
}
disruptor.shutdown();
exec.shutdown();
}
示例13: shouldNotifyOnPreAndPostPublish
import com.lmax.disruptor.dsl.Disruptor; //導入方法依賴的package包/類
@Test
public void shouldNotifyOnPreAndPostPublish() throws Exception
{
final Disruptor<ProxyMethodInvocation> disruptor = createDisruptor(Executors.newSingleThreadExecutor(), 1024);
final RingBufferProxyGeneratorFactory generatorFactory = new RingBufferProxyGeneratorFactory();
final RingBufferProxyGenerator ringBufferProxyGenerator = generatorFactory.newProxy(generatorType,
new ConfigurableValidator(NO_ANNOTATION, NOT_REQUIRED), dropListener, messagePublicationListener);
final ListenerImpl implementation = new ListenerImpl();
final Listener listener = ringBufferProxyGenerator.createRingBufferProxy(Listener.class, disruptor, OverflowStrategy.DROP, implementation);
disruptor.start();
for(int i = 0; i < 3; i++)
{
listener.onVoid();
}
RingBuffer<ProxyMethodInvocation> ringBuffer = disruptor.getRingBuffer();
while (ringBuffer.getMinimumGatingSequence() != ringBuffer.getCursor())
{
// Spin
}
disruptor.shutdown();
Executors.newSingleThreadExecutor().shutdown();
assertThat(messagePublicationListener.getPreCount(), is(3));
assertThat(messagePublicationListener.getPostCount(), is(3));
}
示例14: shouldDropMessagesIfRingBufferIsFull
import com.lmax.disruptor.dsl.Disruptor; //導入方法依賴的package包/類
@Test
public void shouldDropMessagesIfRingBufferIsFull() throws Exception
{
final Disruptor<ProxyMethodInvocation> disruptor = createDisruptor(Executors.newSingleThreadExecutor(), 4);
final RingBufferProxyGeneratorFactory generatorFactory = new RingBufferProxyGeneratorFactory();
final RingBufferProxyGenerator ringBufferProxyGenerator = generatorFactory.newProxy(generatorType, new ConfigurableValidator(false, true), dropListener);
final CountDownLatch latch = new CountDownLatch(1);
final BlockingOverflowTest implementation = new BlockingOverflowTest(latch);
final OverflowTest listener = ringBufferProxyGenerator.createRingBufferProxy(OverflowTest.class, disruptor, OverflowStrategy.DROP, implementation);
disruptor.start();
for(int i = 0; i < 8; i++)
{
listener.invoke();
}
latch.countDown();
Thread.sleep(250L);
disruptor.shutdown();
Executors.newSingleThreadExecutor().shutdown();
assertThat(implementation.getInvocationCount(), is(4));
assertThat(dropListener.getDropCount(), is(4));
}
示例15: shouldReportBatchSize
import com.lmax.disruptor.dsl.Disruptor; //導入方法依賴的package包/類
@Test
public void shouldReportBatchSize() throws Exception
{
final Disruptor<ProxyMethodInvocation> disruptor = createDisruptor(Executors.newSingleThreadExecutor(), 8);
final RingBufferProxyGeneratorFactory generatorFactory = new RingBufferProxyGeneratorFactory();
final RingBufferProxyGenerator ringBufferProxyGenerator =
generatorFactory.newProxy(generatorType, new ConfigurableValidator(false, true), dropListener);
final CountDownLatch latch1 = new CountDownLatch(1);
final CountDownLatch latch2 = new CountDownLatch(1);
final BatchSizeTrackingHandler batchSizeTracker = new BatchSizeTrackingHandler(latch1, latch2);
final OverflowTest listener = ringBufferProxyGenerator.createRingBufferProxy(OverflowTest.class, disruptor, OverflowStrategy.BLOCK, batchSizeTracker);
disruptor.start();
for(int i = 0; i < 8; i++)
{
listener.invoke();
latch1.await();
}
latch2.countDown();
Thread.sleep(250L);
disruptor.shutdown();
Executors.newSingleThreadExecutor().shutdown();
assertThat(batchSizeTracker.getInvocationCount(), is(8));
assertThat(batchSizeTracker.getMaxBatchSize(), is(7));
}