Skip to content

Instantly share code, notes, and snippets.

@cmadsen
Last active August 12, 2019 09:39
Show Gist options
  • Save cmadsen/0d0348b6449837097507f856a124b9dd to your computer and use it in GitHub Desktop.
Save cmadsen/0d0348b6449837097507f856a124b9dd to your computer and use it in GitHub Desktop.
Disruptor consumer invocation issue
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
<camelContext id="camel" xmlns="http://camel.apache.org/schema/spring">
<route id="testRoute">
<from uri="disruptor:in?multipleConsumers=true" />
<to uri="disruptor:out?multipleConsumers=true" />
</route>
</camelContext>
</beans>
package camel.routing.test;
import static java.util.concurrent.TimeUnit.*;
import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.MatcherAssert.*;
import javax.annotation.PostConstruct;
import org.apache.camel.CamelContext;
import org.apache.camel.ConsumerTemplate;
import org.apache.camel.Exchange;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.component.mock.MockEndpoint;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@ContextConfiguration(classes = { TestDisruptor.NestedConfiguration.class })
public class TestDisruptor {
private static final Logger log = LoggerFactory
.getLogger(TestDisruptor.class);
public static final String IN_QUEUE = "disruptor:in?multipleConsumers=true";
public static final String OUT_QUEUE = "disruptor:out?multipleConsumers=true";
static public class BarEventConsumer {
private static final Logger log = LoggerFactory
.getLogger(BarEventConsumer.class);
@Autowired
private CamelContext context;
public BarEventConsumer() {
}
@PostConstruct
void postConstruct() {
new Thread(this::receiveMessage).start();
}
void receiveMessage() {
try {
// context.start();
ConsumerTemplate consumer = context.createConsumerTemplate();
consumer.start();
Exchange exchange = consumer
.receive(TestDisruptor.OUT_QUEUE);
ProducerTemplate destination = context.createProducerTemplate();
destination.sendBody("mock:result",
"bar" + exchange.getIn().getBody());
} catch (Exception e) {
e.printStackTrace();
}
}
}
@Autowired
private CamelContext camelContext;
@Configuration
@ImportResource({ "classpath:/disruptor-test.xml" })
static class NestedConfiguration {
@Bean
public BarEventConsumer barEventConsumer() {
return new BarEventConsumer();
}
}
@Test
public void testDisruptor() throws Exception {
MockEndpoint resultEndpoint = camelContext.getEndpoint("mock:result",
MockEndpoint.class);
resultEndpoint.expectedMessageCount(1);
String dataToSend = "HELLO WORLD";
ProducerTemplate producer = camelContext.createProducerTemplate();
producer.sendBody(IN_QUEUE, dataToSend);
SECONDS.sleep(1);
resultEndpoint.assertIsSatisfied();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment