Install activemq locally
Download gzip'd tar'd file from one of the mirror siteseg.
wget http://mirror.vorboss.net/apache/activemq/5.9.1/apache-activemq-5.9.1-bin.tar.gz
tar zxvf apache-activemq-5.9.1-bin.tar.gz
cd apache-activemq-5.9.1/bin
chmod 755 activemq
Start activemq
./activemq startCheck it is running:
netstat -an|grep 61616
You can go to the admin console in a web browser:
http://localhost:8161/admin/
Default username and password is admin/admin.
Create JMS project
I created a maven project from the command line:mvn archetype:generate -DgroupId=com.ojha.play -DartifactId=activemq -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
In the pom I added a dependency on active mq:
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-all</artifactId>
<version>5.9.0</version>
</dependency>
Implement producer and consumer
Producer:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.ojha.play; | |
import javax.jms.*; | |
import org.apache.activemq.ActiveMQConnection; | |
import org.apache.activemq.ActiveMQConnectionFactory; | |
public class Producer { | |
private static String url = ActiveMQConnection.DEFAULT_BROKER_URL; | |
private static String subject = "PuffQueue"; | |
public static void main(String[] args) throws JMSException { | |
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url); | |
Connection connection = connectionFactory.createConnection(); | |
connection.start(); | |
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); | |
// if queue doesnt exist it will be created | |
Destination destination = session.createQueue(subject); | |
MessageProducer producer = session.createProducer(destination); | |
TextMessage message = session.createTextMessage("RAWR"); | |
producer.send(message); | |
connection.close(); | |
} | |
} |
Consumer:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.ojha.play; | |
import javax.jms.*; | |
import org.apache.activemq.ActiveMQConnection; | |
import org.apache.activemq.ActiveMQConnectionFactory; | |
public class Consumer { | |
private static String url = ActiveMQConnection.DEFAULT_BROKER_URL; | |
private static String subject = "PuffQueue"; | |
public static void main(String[] args) throws JMSException { | |
ConnectionFactory connectionFactory | |
= new ActiveMQConnectionFactory(url); | |
Connection connection = connectionFactory.createConnection(); | |
connection.start(); | |
Session session = connection.createSession(false, | |
Session.AUTO_ACKNOWLEDGE); | |
Destination destination = session.createQueue(subject); | |
MessageConsumer consumer = session.createConsumer(destination); | |
// this method will block | |
Message message = consumer.receive(); | |
if (message instanceof TextMessage) { | |
TextMessage textMessage = (TextMessage) message; | |
System.out.println("I got mail! " + textMessage.getText()); | |
} | |
connection.close(); | |
} | |
} |
Then ran the consumer to dequeue the message.
No comments:
Post a Comment