Disruptorフレームワークを学びたいです。Javaプログラム言語を使用してmainメソッドで実行できるhelloworldの例を教えてくれる人はいますか?
3 に答える
以下は、Disruptor ライブラリの使用方法を示す簡単で実行可能な例です。例は、Disruptor ライブラリのバージョン 2.10.4 を使用して記述されています。
https://github.com/trevorbernard/disruptor-examples
このスレッドにも相互投稿しました: The simplest and actual example code of LMAX Disruptor
ここで私の側からもう1つ。オープン ソースの Lmax ライブラリを使用して、ディスラプターの例を 1 つ試しました。
lmax ディスラプター (ディスラプターの内部ではありません) の使用の背後にあるアイデアは、メッセージ ディスパッチャーを作成し、コンシューマーのようなイベント リスナーを登録することだと思います。
メッセージタイプを指定して Disruptor を作成します。
Disruptor<Message> disruptor = new Disruptor<Message>(Message.EVENT_FACTORY, 2048, exec);`
ハンドラーを作成する
final EventHandler<Message> handler = new EventHandler<Message>() {
// event will eventually be recycled by the Disruptor after it wraps
public void onEvent(final Message event, final long sequence, final boolean endOfBatch) throws Exception {
Integer value = event.getMsg();
if(value % 10000 == 0){
System.out.println("ValueEvent: " + value + " Sequence: " + sequence);
double timeINnanos = (System.nanoTime()-startTime);
double timetaken = (timeINnanos/1e9);
System.out.println("Time Taken till now in sec " + timetaken );
}
}
};
ハンドラーをディスラプターに登録する
disruptor.handleEventsWith(handler);
そのディスラプターを開始し、返された RingBuffer をプロデューサーに渡します
RingBuffer<Message> ringBuffer = disruptor.start();
Producer producer = new Producer(ringBuffer);
完全なコードはここで見つけることができます Github リンク
LMAX コードLMAX Source Code Test Directory の test ディレクトリを確認することをお勧めします。私の意見では、LMAX でできるあらゆることの最良の情報源です。簡単な例については、次のリンクの簡単な例をご覧ください。
また、 DSL の例を参照することをお勧めします。