Skip to content

Commit 94c22f4

Browse files
committed
Example of ActiveMQ with JMS API
0 parents  commit 94c22f4

File tree

8 files changed

+259
-0
lines changed

8 files changed

+259
-0
lines changed

camel/pom.xml

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<groupId>nl.marktplaats</groupId>
6+
<artifactId>camel</artifactId>
7+
<version>1.0-SNAPSHOT</version>
8+
<packaging>jar</packaging>
9+
10+
<name>camel</name>
11+
<url>http://maven.apache.org</url>
12+
13+
<parent>
14+
<groupId>nl.marktplaats</groupId>
15+
<artifactId>activemq-example1</artifactId>
16+
<version>1.0-SNAPSHOT</version>
17+
</parent>
18+
19+
<properties>
20+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
21+
</properties>
22+
23+
<dependencies>
24+
<dependency>
25+
<groupId>junit</groupId>
26+
<artifactId>junit</artifactId>
27+
<version>4.8.1</version>
28+
<scope>test</scope>
29+
</dependency>
30+
<dependency>
31+
<groupId>org.apache.activemq</groupId>
32+
<artifactId>activemq-camel</artifactId>
33+
<version>5.4.1</version>
34+
</dependency>
35+
</dependencies>
36+
</project>

consumer/pom.xml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<groupId>nl.marktplaats</groupId>
6+
<artifactId>consumer</artifactId>
7+
<version>1.0-SNAPSHOT</version>
8+
<packaging>jar</packaging>
9+
10+
<name>consumer</name>
11+
<url>http://maven.apache.org</url>
12+
13+
<parent>
14+
<groupId>nl.marktplaats</groupId>
15+
<artifactId>activemq-example1</artifactId>
16+
<version>1.0-SNAPSHOT</version>
17+
</parent>
18+
19+
<properties>
20+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
21+
</properties>
22+
23+
<dependencies>
24+
<dependency>
25+
<groupId>junit</groupId>
26+
<artifactId>junit</artifactId>
27+
<version>4.8.1</version>
28+
<scope>test</scope>
29+
</dependency>
30+
</dependencies>
31+
</project>
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package nl.marktplaats.consumer;
2+
3+
import javax.jms.Connection;
4+
import javax.jms.JMSException;
5+
import javax.jms.Message;
6+
import javax.jms.MessageConsumer;
7+
import javax.jms.MessageListener;
8+
import javax.jms.Session;
9+
import javax.jms.TextMessage;
10+
11+
import org.apache.activemq.spring.ActiveMQConnectionFactory;
12+
13+
public class Consumer {
14+
public Consumer() throws JMSException {
15+
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory();
16+
connectionFactory.setBrokerURL("tcp://localhost:61616");
17+
18+
Connection connection = connectionFactory.createConnection();
19+
connection.start();
20+
21+
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
22+
// MessageConsumer consumer = session.createConsumer(session.createQueue("Testqueue"));
23+
MessageConsumer consumer = session.createConsumer(session.createTopic("Testtopic"));
24+
consumer.setMessageListener(new HelloMessageListener());
25+
26+
}
27+
28+
private static class HelloMessageListener implements MessageListener {
29+
30+
@Override
31+
public void onMessage(Message message) {
32+
TextMessage textMessage = (TextMessage) message;
33+
try {
34+
System.out.println("Consumer " + Thread.currentThread().getName() + " received message: " + textMessage.getText());
35+
} catch (JMSException e) {
36+
e.printStackTrace();
37+
}
38+
}
39+
40+
}
41+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package nl.marktplaats.consumer;
2+
3+
import javax.jms.JMSException;
4+
5+
public class Main {
6+
public static void main(String... args) throws JMSException {
7+
System.out.println("Starting consumer...");
8+
Consumer consumer = new Consumer();
9+
}
10+
}

pom.xml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<groupId>nl.marktplaats</groupId>
6+
<artifactId>activemq-example1</artifactId>
7+
<version>1.0-SNAPSHOT</version>
8+
<packaging>pom</packaging>
9+
10+
<name>activemq-example1</name>
11+
<url>http://maven.apache.org</url>
12+
13+
<modules>
14+
<module>producer</module>
15+
<module>consumer</module>
16+
<module>camel</module>
17+
</modules>
18+
<properties>
19+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
20+
</properties>
21+
22+
<dependencies>
23+
<dependency>
24+
<groupId>junit</groupId>
25+
<artifactId>junit</artifactId>
26+
<version>4.8.1</version>
27+
<scope>test</scope>
28+
</dependency>
29+
<dependency>
30+
<groupId>org.apache.activemq</groupId>
31+
<artifactId>activemq-core</artifactId>
32+
<version>5.4.1</version>
33+
</dependency>
34+
</dependencies>
35+
</project>

producer/pom.xml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<groupId>nl.marktplaats</groupId>
6+
<artifactId>producer</artifactId>
7+
<version>1.0-SNAPSHOT</version>
8+
<packaging>jar</packaging>
9+
<name>producer</name>
10+
<url>http://maven.apache.org</url>
11+
12+
<parent>
13+
<groupId>nl.marktplaats</groupId>
14+
<artifactId>activemq-example1</artifactId>
15+
<version>1.0-SNAPSHOT</version>
16+
</parent>
17+
18+
<properties>
19+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
20+
</properties>
21+
22+
<dependencies>
23+
<dependency>
24+
<groupId>junit</groupId>
25+
<artifactId>junit</artifactId>
26+
<version>4.8.1</version>
27+
<scope>test</scope>
28+
</dependency>
29+
</dependencies>
30+
</project>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package nl.marktplaats.producer;
2+
3+
import org.apache.activemq.broker.BrokerService;
4+
5+
public class Main {
6+
public static void main(String... args) throws Exception {
7+
// start embedded broker
8+
BrokerService broker = new BrokerService();
9+
broker.addConnector("tcp://localhost:61616");
10+
broker.start();
11+
12+
Producer producer = new Producer();
13+
int x = 0;
14+
while(true) {
15+
Thread.sleep(2000);
16+
producer.produceMessage(x);
17+
x++;
18+
}
19+
}
20+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package nl.marktplaats.producer;
2+
3+
import javax.jms.Connection;
4+
import javax.jms.DeliveryMode;
5+
import javax.jms.Destination;
6+
import javax.jms.JMSException;
7+
import javax.jms.MessageProducer;
8+
import javax.jms.Session;
9+
import javax.jms.TextMessage;
10+
11+
import org.apache.activemq.spring.ActiveMQConnectionFactory;
12+
13+
public class Producer {
14+
15+
private Connection connection;
16+
17+
public Producer() throws JMSException {
18+
// Create a ConnectionFactory
19+
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory();
20+
connectionFactory.setBrokerURL("tcp://localhost:61616");
21+
22+
connection = connectionFactory.createConnection();
23+
connection.start();
24+
25+
// Clean up
26+
// connection.close();
27+
28+
}
29+
30+
public void produceMessage(int x) {
31+
try {
32+
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
33+
34+
// Create the destination
35+
// Destination destination = session.createQueue("Testqueue");
36+
Destination destination = session.createTopic("Testtopic");
37+
38+
MessageProducer producer = session.createProducer(destination);
39+
producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
40+
41+
// Create a messages
42+
String text = "Hello world " + x + "! From: " + Thread.currentThread().getName() + " : " + this.hashCode();
43+
TextMessage message = session.createTextMessage(text);
44+
45+
// Tell the producer to send the message
46+
System.out.println("Sent message: "+ message.hashCode() + " : " + Thread.currentThread().getName());
47+
48+
producer.send(message);
49+
session.close();
50+
}
51+
catch (Exception e) {
52+
System.out.println("Caught: " + e);
53+
e.printStackTrace();
54+
}
55+
}
56+
}

0 commit comments

Comments
 (0)