本文整理匯總了Java中com.lmax.disruptor.dsl.Disruptor.getRingBuffer方法的典型用法代碼示例。如果您正苦於以下問題:Java Disruptor.getRingBuffer方法的具體用法?Java Disruptor.getRingBuffer怎麽用?Java Disruptor.getRingBuffer使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類com.lmax.disruptor.dsl.Disruptor
的用法示例。
在下文中一共展示了Disruptor.getRingBuffer方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: offer
import com.lmax.disruptor.dsl.Disruptor; //導入方法依賴的package包/類
/**
* add the data obj to the disruptor named the field name
*
* @param name
* @param obj
*/
public void offer(String name, Object obj) {
Disruptor<DisruptorEvent> disruptor = DISRUPTOR_MAP.get(name);
AssertUtil.canNotEmpty(disruptor, "the disruptor is not exist!name:" + name);
RingBuffer<DisruptorEvent> ringBuffer = disruptor.getRingBuffer();
//請求下一個事件序號;
long sequence = ringBuffer.next();
try {
//獲取該序號對應的事件對象;
DisruptorEvent event = ringBuffer.get(sequence);
event.setData(obj);
} finally {
//發布事件;
ringBuffer.publish(sequence);
}
}
示例2: main
import com.lmax.disruptor.dsl.Disruptor; //導入方法依賴的package包/類
public static void main(String[] args) throws InterruptedException
{
Disruptor<ObjectBox> disruptor = new Disruptor<ObjectBox>(
ObjectBox.FACTORY, RING_SIZE, Executors.newCachedThreadPool(), ProducerType.MULTI,
new BlockingWaitStrategy());
disruptor.handleEventsWith(new Consumer()).then(new Consumer());
final RingBuffer<ObjectBox> ringBuffer = disruptor.getRingBuffer();
Publisher p = new Publisher();
IMessage message = new IMessage();
ITransportable transportable = new ITransportable();
String streamName = "com.lmax.wibble";
System.out.println("publishing " + RING_SIZE + " messages");
for (int i = 0; i < RING_SIZE; i++)
{
ringBuffer.publishEvent(p, message, transportable, streamName);
Thread.sleep(10);
}
System.out.println("start disruptor");
disruptor.start();
System.out.println("continue publishing");
while (true)
{
ringBuffer.publishEvent(p, message, transportable, streamName);
Thread.sleep(10);
}
}
示例3: Source
import com.lmax.disruptor.dsl.Disruptor; //導入方法依賴的package包/類
/**
* Create a new source.
* <p>This method will prepare the instance with some needed variables
* in order to be started later with the start method (implemented by children).
*
* @param parsersManager Instance of ParserManager that will serve parsers to this source instance
* @param eventHandler Instance of EventHandler that will receive the events generated by this source instance
* @param properties Map of properties associated with this source
*/
public Source(ParsersManager parsersManager, EventHandler eventHandler, Map<String, Object> properties) {
// Save the references for later use
this.parsersManager = parsersManager;
this.properties = properties;
// Create the ring buffer for this topic and start it
Disruptor<MapEvent> disruptor = new Disruptor<>(new MapEventFactory(), ConfigData.getRingBufferSize(), Executors.newCachedThreadPool());
disruptor.handleEventsWith(eventHandler);
disruptor.start();
// Create the event producer that will receive the events produced by
// this source instance
eventProducer = new EventProducer(disruptor.getRingBuffer());
prepare();
}
示例4: PersonHelper
import com.lmax.disruptor.dsl.Disruptor; //導入方法依賴的package包/類
public PersonHelper() {
//參數1 事件
//參數2 單線程使用
//參數3 等待策略
EventFactory<PersonEvent> eventFactory = PersonEvent.EVENT_FACTORY;
ExecutorService executor = Executors.newSingleThreadExecutor();
int ringBufferSize = 4; // RingBuffer 大小,必須是 2 的 N 次方;
Disruptor<PersonEvent> disruptor = new Disruptor<>(eventFactory,
ringBufferSize, executor, ProducerType.SINGLE,
new YieldingWaitStrategy());
ringBuffer = disruptor.getRingBuffer();
//獲取生產者的位置信息
sequenceBarrier = ringBuffer.newBarrier();
//消費者
handler = new PersonEventHandler();
//事件處理器,監控指定ringBuffer,有數據時通知指定handler進行處理
batchEventProcessor = new BatchEventProcessor<>(ringBuffer, sequenceBarrier, handler);
//傳入所有消費者線程的序號
// ringBuffer.setGatingSequences(batchEventProcessor.getSequence());
}
示例5: post
import com.lmax.disruptor.dsl.Disruptor; //導入方法依賴的package包/類
public void post(Object event) {
Class<?> eventClass = event.getClass();
Disruptor<BaseEvent> eventDisruptor = disruptorPool.get(eventClass);
if(eventDisruptor == null) {
List<RegistedEventHandler> registedEventHandlers = handlesMap.get(eventClass);
if(registedEventHandlers == null || registedEventHandlers.size() == 0) {
throw new RuntimeException("The " + eventClass.getSimpleName() + " event dosen't regist any eventHandler.");
}
eventDisruptor = createDisruptor(eventClass, registedEventHandlers);
disruptorPool.put(eventClass, eventDisruptor);
}
// check whether has event Repository definition.
if(eventRepository != null) {
eventRepository.save(event);
}
RingBuffer<BaseEvent> ringBuffer = eventDisruptor.getRingBuffer();
BaseEventProducer producer = new BaseEventProducer(ringBuffer);
producer.onData(event);
}
示例6: 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
}
示例7: lessonStdCountUpdateCmdProcessor
import com.lmax.disruptor.dsl.Disruptor; //導入方法依賴的package包/類
@Bean
public ItemAmountUpdateCommandProcessor lessonStdCountUpdateCmdProcessor() {
CommandEventProducer<ItemAmountUpdateCommand>[] commandEventProducerList = new CommandEventProducer[conf.getNum()];
for (int i = 0; i < conf.getNum(); i++) {
ItemAmountUpdateCommandBuffer cmdBuffer = new ItemAmountUpdateCommandBuffer(conf.getSqlBufferSize());
ItemAmountUpdateCommandExecutor cmdExecutor = new ItemAmountUpdateCommandExecutor(jdbcTemplate);
Disruptor<CommandEvent<ItemAmountUpdateCommand>> disruptor = new Disruptor<>(
new CommandEventFactory(),
conf.getQueueSize(),
Executors.defaultThreadFactory());
disruptor
.handleEventsWith(new CommandEventDbHandler(cmdBuffer, cmdExecutor))
.then(new CommandEventGcHandler())
;
// disruptor 的異常處理是這樣的,
// 不論這種形式 A->B, 還是這種形式 A,B->C,D, 隻有拋出異常的那個handler會中斷執行
disruptor.setDefaultExceptionHandler(new CommandEventExceptionHandler());
commandEventProducerList[i] = new CommandEventProducer<>(disruptor.getRingBuffer());
BeanRegisterUtils.registerSingleton(
applicationContext,
"CommandEvent<ItemAmountUpdateCommand>_DisruptorLifeCycleContainer_" + i,
new DisruptorLifeCycleContainer("CommandEvent<ItemAmountUpdateCommand>_Disruptor_" + i, disruptor,
StartupOrderConstants.DISRUPTOR_ITEM_UPDATE));
}
ItemAmountUpdateCommandProcessor cmdProcessor = new ItemAmountUpdateCommandProcessor(commandEventProducerList);
commandDispatcher.registerCommandProcessor(cmdProcessor);
return cmdProcessor;
}
開發者ID:bighector,項目名稱:-artemis-disruptor-miaosha,代碼行數:41,代碼來源:ItemAmountUpdateProcessorConfiguration.java
示例8: create
import com.lmax.disruptor.dsl.Disruptor; //導入方法依賴的package包/類
/**
* 得到返回的結果後, 必須執行 {@link DisruptorJmsMessageSender#getDisruptor()}.start() 才可以使用
*
* @param session
* @param messageProducer
* @param dupMessageDetectStrategy
* @param ringBufferSize 必須是2的次方
* @return
* @throws JMSException
*/
public static DisruptorJmsMessageSender create(
Session session,
MessageProducer messageProducer,
DupMessageDetectStrategy dupMessageDetectStrategy,
int ringBufferSize
) throws JMSException {
Disruptor<PayloadEvent> disruptor = new Disruptor<>(
PayloadEvent::new,
ringBufferSize,
Executors.defaultThreadFactory()
);
PayloadEventProducer payloadEventProducer = new PayloadEventProducer(disruptor.getRingBuffer());
DisruptorJmsMessageSender messageSender = new DisruptorJmsMessageSender();
messageSender.setSession(session);
messageSender.setMessageProducer(messageProducer);
messageSender.setPayloadEventProducer(payloadEventProducer);
if (dupMessageDetectStrategy != null) {
messageSender.setDupMessageDetectStrategy(dupMessageDetectStrategy);
}
disruptor.handleEventsWith(messageSender);
messageSender.setDisruptor(disruptor);
return messageSender;
}
示例9: loop
import com.lmax.disruptor.dsl.Disruptor; //導入方法依賴的package包/類
default void loop(Disruptor disruptor, PcapHandle handle) throws Exception {
RingBuffer<PacketContainer> ringBuffer = disruptor.getRingBuffer();
handle.loop(0, (RawPacketListener) packet -> {
long sequence = ringBuffer.next();
PacketContainer container = ringBuffer.get(sequence);
container.clear();
container.setDlt(handle.getDlt());
container.setTimestamp(handle.getTimestamp());
container.setRawPacket(packet);
ringBuffer.publish(sequence);
});
}
示例10: main
import com.lmax.disruptor.dsl.Disruptor; //導入方法依賴的package包/類
public static void main(String[] args) throws Exception {
Executor executor = Executors.newCachedThreadPool();
LongEventFactory eventFactory = new LongEventFactory();
int bufferSize = 1024;
// Disruptor<LongEvent> disruptor = new Disruptor<LongEvent>(eventFactory, bufferSize, executor);
// disruptor.handleEventsWith(new LongEventHandler());
// disruptor.start();
Disruptor<LongEvent> disruptor = new Disruptor<>(LongEvent::new, bufferSize, executor);
disruptor.handleEventsWith((event, sequence, endOfBatch) -> {System.out.println("Event: " + event);
System.out.println("CurrentThreadName:" + Thread.currentThread().getName());
});
disruptor.start();
RingBuffer<LongEvent> ringBuffer = disruptor.getRingBuffer();
LongEventProducer producer = new LongEventProducer(ringBuffer);
ByteBuffer bb = ByteBuffer.allocate(8);
for (long l = 0; true; l++) {
bb.putLong(0, l);
ringBuffer.publishEvent((event, sequence, buffer) -> event.set(buffer.getLong(0)), bb);
// producer.onData(bb);
//Thread.sleep(1000);
}
}
示例11: publishBufferedExchanges
import com.lmax.disruptor.dsl.Disruptor; //導入方法依賴的package包/類
private void publishBufferedExchanges(Disruptor<ExchangeEvent> newDisruptor) {
//now empty out all buffered Exchange if we had any
final List<Exchange> exchanges = new ArrayList<Exchange>(temporaryExchangeBuffer.size());
while (!temporaryExchangeBuffer.isEmpty()) {
exchanges.add(temporaryExchangeBuffer.remove());
}
RingBuffer<ExchangeEvent> ringBuffer = newDisruptor.getRingBuffer();
//and offer them again to our new ringbuffer
for (final Exchange exchange : exchanges) {
publishExchangeOnRingBuffer(exchange, ringBuffer);
}
}
示例12: main
import com.lmax.disruptor.dsl.Disruptor; //導入方法依賴的package包/類
public static void main(String[] args) throws Exception
{
// Executor that will be used to construct new threads for consumers
Executor executor = Executors.newCachedThreadPool();
// Specify the size of the ring buffer, must be power of 2.
int bufferSize = 1024;
// Construct the Disruptor
Disruptor<LongEvent> disruptor = new Disruptor<>(LongEvent::new, bufferSize, executor);
// Connect the handler
disruptor.handleEventsWith(DisruptorTests::handleEvent);
EventHandlerGroup<LongEvent> g = disruptor.handleEventsWith(DisruptorTests::handleEvent);
// Start the Disruptor, starts all threads running
disruptor.start();
// Get the ring buffer from the Disruptor to be used for publishing.
RingBuffer<LongEvent> ringBuffer = disruptor.getRingBuffer();
//LongEventProducer producer = new LongEventProducer(ringBuffer);
ByteBuffer bb = ByteBuffer.allocate(8);
for (long l = 0; true; l++)
{
bb.putLong(0, l);
ringBuffer.publishEvent(DisruptorTests::translate, bb);
Thread.sleep(1000);
}
}
示例13: produceEvents
import com.lmax.disruptor.dsl.Disruptor; //導入方法依賴的package包/類
private static void produceEvents(Disruptor<ObjectEvent> disruptor) throws InterruptedException {
RingBuffer<ObjectEvent> ringBuffer = disruptor.getRingBuffer();
for (long l = 0; true; l++) {
String obj = "Test-" + l;
ringBuffer.publishEvent((event, sequence) -> event.setObject(obj));
Thread.sleep(1000);
}
}
示例14: main
import com.lmax.disruptor.dsl.Disruptor; //導入方法依賴的package包/類
public static void main(String[] args) throws Exception
{
// Executor that will be used to construct new threads for consumers
Executor executor = Executors.newCachedThreadPool();
// The factory for the event
LongEventFactory factory = new LongEventFactory();
// Specify the size of the ring buffer, must be power of 2.
int bufferSize = 1024;
// Construct the Disruptor
Disruptor<LongEvent> disruptor = new Disruptor<>(factory, bufferSize, executor);
// Connect the handler
disruptor.handleEventsWith(new LongEventHandler());
// Start the Disruptor, starts all threads running
disruptor.start();
// Get the ring buffer from the Disruptor to be used for publishing.
RingBuffer<LongEvent> ringBuffer = disruptor.getRingBuffer();
LongEventProducer producer = new LongEventProducer(ringBuffer);
ByteBuffer bb = ByteBuffer.allocate(8);
for (long l = 0; true; l++)
{
bb.putLong(0, l);
producer.onData(bb);
Thread.sleep(1000);
}
}
示例15: 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
}