Skip to content

Commit

Permalink
Added unit-test-based full example with embeded active MQ.
Browse files Browse the repository at this point in the history
  • Loading branch information
x14764 committed Jul 10, 2013
1 parent 90e2f85 commit f9db7a7
Show file tree
Hide file tree
Showing 4 changed files with 170 additions and 1 deletion.
3 changes: 2 additions & 1 deletion .gitignore
Expand Up @@ -2,4 +2,5 @@
mq/data/localhost
*.class
*.jar

*.idea
all_in/activemq-data
41 changes: 41 additions & 0 deletions all_in/all_in.iml
@@ -0,0 +1,41 @@
<?xml version="1.0" encoding="UTF-8"?>
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="false">
<output url="file://$MODULE_DIR$/target/classes" />
<output-test url="file://$MODULE_DIR$/target/test-classes" />
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/test/resources" isTestSource="true" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" scope="TEST" name="Maven: junit:junit:4.11" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.hamcrest:hamcrest-core:1.3" level="project" />
<orderEntry type="library" name="Maven: javax.jms:jms:1.1" level="project" />
<orderEntry type="library" name="Maven: org.apache.activemq:activemq-core:5.7.0" level="project" />
<orderEntry type="library" name="Maven: org.slf4j:slf4j-api:1.6.6" level="project" />
<orderEntry type="library" name="Maven: org.apache.geronimo.specs:geronimo-jms_1.1_spec:1.1.1" level="project" />
<orderEntry type="library" name="Maven: org.apache.activemq:kahadb:5.7.0" level="project" />
<orderEntry type="library" name="Maven: org.apache.activemq.protobuf:activemq-protobuf:1.1" level="project" />
<orderEntry type="library" name="Maven: org.fusesource.mqtt-client:mqtt-client:1.3" level="project" />
<orderEntry type="library" name="Maven: org.fusesource.hawtdispatch:hawtdispatch-transport:1.11" level="project" />
<orderEntry type="library" name="Maven: org.fusesource.hawtdispatch:hawtdispatch:1.11" level="project" />
<orderEntry type="library" name="Maven: org.fusesource.hawtbuf:hawtbuf:1.9" level="project" />
<orderEntry type="library" name="Maven: org.apache.geronimo.specs:geronimo-j2ee-management_1.1_spec:1.0.1" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-context:3.0.7.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-aop:3.0.7.RELEASE" level="project" />
<orderEntry type="library" name="Maven: aopalliance:aopalliance:1.0" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-asm:3.0.7.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-beans:3.0.7.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-core:3.0.7.RELEASE" level="project" />
<orderEntry type="library" name="Maven: commons-logging:commons-logging:1.1.1" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-expression:3.0.7.RELEASE" level="project" />
<orderEntry type="library" name="Maven: commons-net:commons-net:3.1" level="project" />
<orderEntry type="library" name="Maven: org.jasypt:jasypt:1.9.0" level="project" />
</component>
</module>

47 changes: 47 additions & 0 deletions all_in/pom.xml
@@ -0,0 +1,47 @@
<?xml version="1.0"?>
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>shoe</groupId>
<artifactId>all_in</artifactId>
<packaging>jar</packaging>
<version>0.1</version>
<name>producer</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.jms</groupId>
<artifactId>jms</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-core</artifactId>
<version>5.7.0</version>
</dependency>
</dependencies>
<repositories>
<repository>
<id>repository.jboss.org-public</id>
<name>JBoss repository</name>
<url>https://repository.jboss.org/nexus/content/groups/public</url>
</repository>
<repository>
<id>jboss</id>
<url>http://repository.jboss.com/maven2</url>
<releases>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
</project>
80 changes: 80 additions & 0 deletions 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();
}
}
}
}

0 comments on commit f9db7a7

Please sign in to comment.