private long value;
public long getValue() {
return value;
}
public void setValue(long value) {
this.value = value;
}
}
public class LongEventFactory implements EventFactory {
public LongEvent newInstance() {
return new LongEvent();
}
}
public class LongEventHandler implements EventHandler {
public void onEvent(LongEvent longEvent, long l, boolean b) throws Exception {
System.out.println(longEvent.getValue());
}
}
public class LongEventProducer {
private final RingBuffer ringBuffer;
public LongEventProducer(RingBuffer ringBuffer) {
this.ringBuffer = ringBuffer;
}
public void onData(ByteBuffer buffer) {
long sequence = ringBuffer.next();
try {
LongEvent event = ringBuffer.get(sequence);
event.setValue(buffer.getLong(0));
} finally {
ringBuffer.publish(sequence);
}
}
上为声明准备类,下为主方法:
public static void main(String[] args) {
//Executor executor = Executors.newCachedThreadPool();
LongEventFactory factory = new LongEventFactory();
//must be power of 2
int ringBufferSize = 1024;
Disruptor disruptor = new Disruptor(factory, ringBufferSize, Executors.defaultThreadFactory());
disruptor.handleEventsWith(new LongEventHandler());
disruptor.start();
RingBuffer ringBuffer = disruptor.getRingBuffer();
LongEventProducer producer = new LongEventProducer(ringBuffer);
ByteBuffer bb = ByteBuffer.allocate(8);
for(long l = 0; l<100; l++) {
bb.putLong(0, l);
producer.onData(bb);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
disruptor.shutdown();
}
4. 生产者线程模式:
ProducerType生产者线程模式一共有2种:
ProducerType有两种模式 Producer.MULTI和Producer.SINGLE
默认是MULTI,表示在多线程模式下产生sequence
如果确认是单线程生产者,那么可以指定SINGLE,效率会提升
如果是多个生产者(多线程),但模式指定为SINGLE,会出什么问题呢?
5. 等待策略
①,(常用)BlockingWaitStrategy:通过线程阻塞的方式,等待生产者唤醒,被唤醒后,再循环检查依赖的sequence是否已经消费
② BusySpinWaitStrategy:线程一直自旋等待,可能比较耗cpu
③ LiteBlockingWaitStrategy:线程阻塞等待生产者唤醒,与BlockingWaitStrategy相比,区别在signalNeeded.getAndSet,如果两个线程同时访问一个访问waitfor,一个访问signalAll时,可以减少lock加锁次数
④ LiteTimeoutBlockingWaitStrategy:与LiteBlockingWaitStrategy相比,设置了阻塞时间,超过时间后抛异常
⑤ PhasedBackoffWaitStrategy:根据时间参数和传入的等待策略来决定使用哪种等待策略
⑥ TimeoutBlockingWaitStrategy:相对于BlockingWaitStrategy来说,设置了等待时间,超过后抛异常
⑦ (常用)YieldingWaitStrategy:尝试100次,然后Thread.yield()让出cpu
⑧ (常用)SleepingWaitStrategy : sleep
6. 多个消费者模式
LongEventHandler h1 = new LongEventHandler();
LongEventHandler h2 = new LongEventHandler();
disruptor.handleEventsWith(h1, h2…)//传入多个消费者(多线程)
7. 出异常情况处理
EventHandler h1 = (event, sequence, end) -> {
sout(event);
throw new Exception(“消费者出异常”);
}
disruptor.handleEventsWith(h1);
disruptor.handleExceptionsFor(h1).with(new ExcetionHandler() {
@Override
public void handleEventExceition(Throwable throwable, long l, longEvent longEvent) {
throwable.printStackTrace();
}
@Override
public void handleOnStartExceition(Throwable throwable) {
sout(“Exception Start to Hanled!”);
}
@Override
public void handleOnShutDownExceition(Throwable throwable) {
sout(“Exception shutDown to Hanled!”);
}
最后总结
ActiveMQ+Kafka+RabbitMQ学习笔记PDF
关于分布式,限流+缓存+缓存,这三大技术(包含:ZooKeeper+Nginx+MongoDB+memcached+Redis+ActiveMQ+Kafka+RabbitMQ)等等。这些相关的面试也好,还有手写以及学习的笔记PDF,都是啃透分布式技术必不可少的宝藏。以上的每一个专题每一个小分类都有相关的介绍,并且小编也已经将其整理成PDF啦
笔记
[外链图片转存中…(img-21uxzLqf-1721836020077)]
[外链图片转存中…(img-tjb9l6L5-1721836020077)]
关于分布式,限流+缓存+缓存,这三大技术(包含:ZooKeeper+Nginx+MongoDB+memcached+Redis+ActiveMQ+Kafka+RabbitMQ)等等。这些相关的面试也好,还有手写以及学习的笔记PDF,都是啃透分布式技术必不可少的宝藏。以上的每一个专题每一个小分类都有相关的介绍,并且小编也已经将其整理成PDF啦