From f9db7a7620d7e57db8dcdd3537331331ccde9b1b Mon Sep 17 00:00:00 2001 From: x14764 Date: Wed, 10 Jul 2013 08:07:40 -0500 Subject: [PATCH] Added unit-test-based full example with embeded active MQ. --- .gitignore | 3 +- all_in/all_in.iml | 41 ++++++++++++++ all_in/pom.xml | 47 ++++++++++++++++ all_in/src/test/java/shoe/AllIn.java | 80 ++++++++++++++++++++++++++++ 4 files changed, 170 insertions(+), 1 deletion(-) create mode 100644 all_in/all_in.iml create mode 100644 all_in/pom.xml create mode 100644 all_in/src/test/java/shoe/AllIn.java diff --git a/.gitignore b/.gitignore index 4983efb..5413d5b 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,5 @@ mq/data/localhost *.class *.jar - +*.idea +all_in/activemq-data diff --git a/all_in/all_in.iml b/all_in/all_in.iml new file mode 100644 index 0000000..aed73e9 --- /dev/null +++ b/all_in/all_in.iml @@ -0,0 +1,41 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/all_in/pom.xml b/all_in/pom.xml new file mode 100644 index 0000000..f003731 --- /dev/null +++ b/all_in/pom.xml @@ -0,0 +1,47 @@ + + + 4.0.0 + shoe + all_in + jar + 0.1 + producer + http://maven.apache.org + + + junit + junit + 4.11 + test + + + javax.jms + jms + 1.1 + + + org.apache.activemq + activemq-core + 5.7.0 + + + + + repository.jboss.org-public + JBoss repository + https://repository.jboss.org/nexus/content/groups/public + + + jboss + http://repository.jboss.com/maven2 + + + + false + + + + diff --git a/all_in/src/test/java/shoe/AllIn.java b/all_in/src/test/java/shoe/AllIn.java new file mode 100644 index 0000000..eebcc5e --- /dev/null +++ b/all_in/src/test/java/shoe/AllIn.java @@ -0,0 +1,80 @@ +package shoe; + +import org.apache.activemq.broker.BrokerService; +import org.apache.activemq.ActiveMQConnectionFactory; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import javax.jms.*; + +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; + +public class AllIn { + public static final String BROKER_URL = "tcp://localhost:61616"; + public static final String QUEUE_NAME = "test"; + public static final int MESSAGES_TO_SEND = 10; + private BrokerService broker; + private volatile boolean shutdown; + private int messagesReceived; + + @Before + public void startBroker() throws Exception { + broker = new BrokerService(); + broker.addConnector(BROKER_URL); + broker.start(); + } + + @Test + public void smoke() throws Exception { + produce(); + + ConnectionFactory factory = new ActiveMQConnectionFactory(BROKER_URL); + Connection connection = factory.createConnection(); + connection.start(); + Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); + Destination destination = session.createQueue(QUEUE_NAME); + MessageConsumer consumer = session.createConsumer(destination); + consumer.setMessageListener(new SimpleConsumer()); + while(!shutdown) { + Thread.sleep(10); + } + assertThat(messagesReceived, is(MESSAGES_TO_SEND + 1)); + } + + private void produce() throws JMSException { + ConnectionFactory factory = new ActiveMQConnectionFactory(BROKER_URL); + Connection connection = factory.createConnection(); + connection.start(); + Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); + Destination destination = session.createQueue(QUEUE_NAME); + MessageProducer producer = session.createProducer(destination); + + for(int i = 1; i <= MESSAGES_TO_SEND; ++i) { + Message message = session.createTextMessage("Hello World!" + i); + producer.send(message); + } + producer.send(session.createTextMessage("terminate")); + } + + @After + public void stopBroker() throws Exception { + broker.stop(); + } + + class SimpleConsumer implements MessageListener { + @Override + public void onMessage(Message message) { + try { + String body = ((TextMessage)message).getText(); + ++messagesReceived; + if("terminate".equals(body)) { + shutdown = true; + } + } catch (JMSException e) { + e.printStackTrace(); + } + } + } +}