This commit is contained in:
Jonathan Cook
2019-10-23 15:01:44 +02:00
parent db85c8f275
commit 684ec0d2e3
20486 changed files with 1642483 additions and 0 deletions
+7
View File
@@ -0,0 +1,7 @@
## Disruptor
This module contains articles about LMAX Disruptor
### Relevant articles:
- [Concurrency with LMAX Disruptor An Introduction](https://www.baeldung.com/lmax-disruptor-concurrency)
+124
View File
@@ -0,0 +1,124 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>disruptor</artifactId>
<version>0.1.0-SNAPSHOT</version>
<name>disruptor</name>
<packaging>jar</packaging>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<dependencies>
<!-- utils -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${commons-lang3.version}</version>
</dependency>
<dependency>
<groupId>com.lmax</groupId>
<artifactId>disruptor</artifactId>
<version>${disruptor.version}</version>
</dependency>
</dependencies>
<build>
<finalName>disruptor</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>${maven-jar-plugin.version}</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>libs/</classpathPrefix>
<mainClass>org.baeldung.executable.ExecutableMavenJar</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<archiveBaseDirectory>${project.basedir}</archiveBaseDirectory>
<archive>
<manifest>
<mainClass>org.baeldung.executable.ExecutableMavenJar</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>${maven-shade-plugin.version}</version>
<executions>
<execution>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<shadedArtifactAttached>true</shadedArtifactAttached>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>org.baeldung.executable.ExecutableMavenJar</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>com.jolira</groupId>
<artifactId>onejar-maven-plugin</artifactId>
<version>${onejar-maven-plugin.version}</version>
<executions>
<execution>
<configuration>
<mainClass>org.baeldung.executable.ExecutableMavenJar</mainClass>
<attachToBuild>true</attachToBuild>
<filename>${project.build.finalName}-onejar.${project.packaging}</filename>
</configuration>
<goals>
<goal>one-jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<properties>
<!-- util -->
<disruptor.version>3.3.6</disruptor.version>
<!-- testing -->
<maven-shade-plugin.version>2.4.3</maven-shade-plugin.version>
<maven-jar-plugin.version>3.0.2</maven-jar-plugin.version>
<onejar-maven-plugin.version>1.4.4</onejar-maven-plugin.version>
</properties>
</project>
@@ -0,0 +1,34 @@
package com.baeldung.disruptor;
import com.lmax.disruptor.RingBuffer;
public class DelayedMultiEventProducer implements EventProducer {
@Override
public void startProducing(final RingBuffer<ValueEvent> ringBuffer, final int count) {
final Runnable simpleProducer = () -> produce(ringBuffer, count, false);
final Runnable delayedProducer = () -> produce(ringBuffer, count, true);
new Thread(simpleProducer).start();
new Thread(delayedProducer).start();
}
private void produce(final RingBuffer<ValueEvent> ringBuffer, final int count, final boolean addDelay) {
for (int i = 0; i < count; i++) {
final long seq = ringBuffer.next();
final ValueEvent valueEvent = ringBuffer.get(seq);
valueEvent.setValue(i);
ringBuffer.publish(seq);
if (addDelay) {
addDelay();
}
}
}
private void addDelay() {
try {
Thread.sleep(1000);
} catch (InterruptedException interruptedException) {
// No-Op lets swallow it
}
}
}
@@ -0,0 +1,13 @@
package com.baeldung.disruptor;
import com.lmax.disruptor.EventHandler;
/**
* Consumer that consumes event from ring buffer.
*/
public interface EventConsumer {
/**
* One or more event handler to handle event from ring buffer.
*/
public EventHandler<ValueEvent>[] getEventHandler();
}
@@ -0,0 +1,15 @@
package com.baeldung.disruptor;
import com.lmax.disruptor.RingBuffer;
/**
* Producer that produces event for ring buffer.
*/
public interface EventProducer {
/**
* Start the producer that would start producing the values.
* @param ringBuffer
* @param count
*/
public void startProducing(final RingBuffer<ValueEvent> ringBuffer, final int count);
}
@@ -0,0 +1,23 @@
package com.baeldung.disruptor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.lmax.disruptor.EventHandler;
public class MultiEventPrintConsumer implements EventConsumer {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
@Override
@SuppressWarnings("unchecked")
public EventHandler<ValueEvent>[] getEventHandler() {
final EventHandler<ValueEvent> eventHandler = (event, sequence, endOfBatch) -> print(event.getValue(), sequence);
final EventHandler<ValueEvent> otherEventHandler = (event, sequence, endOfBatch) -> print(event.getValue(), sequence);
return new EventHandler[] { eventHandler, otherEventHandler };
}
private void print(final int id, final long sequenceId) {
logger.info("Id is " + id + " sequence id that was used is " + sequenceId);
}
}
@@ -0,0 +1,22 @@
package com.baeldung.disruptor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.lmax.disruptor.EventHandler;
public class SingleEventPrintConsumer implements EventConsumer {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
@Override
@SuppressWarnings("unchecked")
public EventHandler<ValueEvent>[] getEventHandler() {
final EventHandler<ValueEvent> eventHandler = (event, sequence, endOfBatch) -> print(event.getValue(), sequence);
return new EventHandler[] { eventHandler };
}
private void print(final int id, final long sequenceId) {
logger.info("Id is " + id + " sequence id that was used is " + sequenceId);
}
}
@@ -0,0 +1,22 @@
package com.baeldung.disruptor;
import com.lmax.disruptor.RingBuffer;
public class SingleEventProducer implements EventProducer {
@Override
public void startProducing(RingBuffer<ValueEvent> ringBuffer, int count) {
final Runnable producer = () -> produce(ringBuffer, count);
new Thread(producer).start();
}
private void produce(final RingBuffer<ValueEvent> ringBuffer, final int count) {
for (int i = 0; i < count; i++) {
final long seq = ringBuffer.next();
final ValueEvent valueEvent = ringBuffer.get(seq);
valueEvent.setValue(i);
ringBuffer.publish(seq);
}
}
}
@@ -0,0 +1,25 @@
package com.baeldung.disruptor;
import org.apache.commons.lang3.builder.ToStringBuilder;
import com.lmax.disruptor.EventFactory;
public final class ValueEvent {
private int value;
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public final static EventFactory<ValueEvent> EVENT_FACTORY = () -> new ValueEvent();
@Override
public String toString() {
return ToStringBuilder.reflectionToString(this);
}
}
+13
View File
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
</pattern>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="STDOUT" />
</root>
</configuration>
@@ -0,0 +1,84 @@
package com.baeldung.disruptor;
import com.lmax.disruptor.BusySpinWaitStrategy;
import com.lmax.disruptor.RingBuffer;
import com.lmax.disruptor.WaitStrategy;
import com.lmax.disruptor.dsl.Disruptor;
import com.lmax.disruptor.dsl.ProducerType;
import com.lmax.disruptor.util.DaemonThreadFactory;
import org.junit.Before;
import org.junit.Test;
import java.util.concurrent.ThreadFactory;
public class DisruptorIntegrationTest {
private Disruptor<ValueEvent> disruptor;
private WaitStrategy waitStrategy;
@Before
public void setUp() throws Exception {
waitStrategy = new BusySpinWaitStrategy();
}
private void createDisruptor(final ProducerType producerType, final EventConsumer eventConsumer) {
final ThreadFactory threadFactory = DaemonThreadFactory.INSTANCE;
disruptor = new Disruptor<>(ValueEvent.EVENT_FACTORY, 16, threadFactory, producerType, waitStrategy);
disruptor.handleEventsWith(eventConsumer.getEventHandler());
}
private void startProducing(final RingBuffer<ValueEvent> ringBuffer, final int count, final EventProducer eventProducer) {
eventProducer.startProducing(ringBuffer, count);
}
@Test
public void whenMultipleProducerSingleConsumer_thenOutputInFifoOrder() {
final EventConsumer eventConsumer = new SingleEventPrintConsumer();
final EventProducer eventProducer = new DelayedMultiEventProducer();
createDisruptor(ProducerType.MULTI, eventConsumer);
final RingBuffer<ValueEvent> ringBuffer = disruptor.start();
startProducing(ringBuffer, 32, eventProducer);
disruptor.halt();
disruptor.shutdown();
}
@Test
public void whenSingleProducerSingleConsumer_thenOutputInFifoOrder() {
final EventConsumer eventConsumer = new SingleEventConsumer();
final EventProducer eventProducer = new SingleEventProducer();
createDisruptor(ProducerType.SINGLE, eventConsumer);
final RingBuffer<ValueEvent> ringBuffer = disruptor.start();
startProducing(ringBuffer, 32, eventProducer);
disruptor.halt();
disruptor.shutdown();
}
@Test
public void whenSingleProducerMultipleConsumer_thenOutputInFifoOrder() {
final EventConsumer eventConsumer = new MultiEventConsumer();
final EventProducer eventProducer = new SingleEventProducer();
createDisruptor(ProducerType.SINGLE, eventConsumer);
final RingBuffer<ValueEvent> ringBuffer = disruptor.start();
startProducing(ringBuffer, 32, eventProducer);
disruptor.halt();
disruptor.shutdown();
}
@Test
public void whenMultipleProducerMultipleConsumer_thenOutputInFifoOrder() {
final EventConsumer eventConsumer = new MultiEventPrintConsumer();
final EventProducer eventProducer = new DelayedMultiEventProducer();
createDisruptor(ProducerType.MULTI, eventConsumer);
final RingBuffer<ValueEvent> ringBuffer = disruptor.start();
startProducing(ringBuffer, 32, eventProducer);
disruptor.halt();
disruptor.shutdown();
}
}
@@ -0,0 +1,27 @@
package com.baeldung.disruptor;
import static org.junit.Assert.assertEquals;
import com.lmax.disruptor.EventHandler;
public class MultiEventConsumer implements EventConsumer {
private int expectedValue = -1;
private int otherExpectedValue = -1;
@Override
@SuppressWarnings("unchecked")
public EventHandler<ValueEvent>[] getEventHandler() {
final EventHandler<ValueEvent> eventHandler = (event, sequence, endOfBatch) -> assertExpectedValue(event.getValue());
final EventHandler<ValueEvent> otherEventHandler = (event, sequence, endOfBatch) -> assertOtherExpectedValue(event.getValue());
return new EventHandler[] { eventHandler, otherEventHandler };
}
private void assertExpectedValue(final int id) {
assertEquals(++expectedValue, id);
}
private void assertOtherExpectedValue(final int id) {
assertEquals(++otherExpectedValue, id);
}
}
@@ -0,0 +1,21 @@
package com.baeldung.disruptor;
import static org.junit.Assert.assertEquals;
import com.lmax.disruptor.EventHandler;
public class SingleEventConsumer implements EventConsumer {
private int expectedValue = -1;
@Override
@SuppressWarnings("unchecked")
public EventHandler<ValueEvent>[] getEventHandler() {
final EventHandler<ValueEvent> eventHandler = (event, sequence, endOfBatch) -> assertExpectedValue(event.getValue());
return new EventHandler[] { eventHandler };
}
private void assertExpectedValue(final int id) {
assertEquals(++expectedValue, id);
}
}