In the previous example we tested JMS Message producer and Message listener in Jboss application server. In this example i am going to use Apache ActiveMQ for messaging.
Goals
In this example we are using windows so please follow the below steps to start ActiveMQ messaging system.
1. Download activemq from http://activemq.apache.org/download-archives.html
2. Extract activemq zip file and start ActiveMQ (Click on activemq.bat file)
3. Check activemq console using http://localhost:8161/admin (Use default credentials admin/admin to login)

2. Create a Message Producer
Add below dependency in your pom.xml
Once activemq started, then start Message producer first and push the messages in the queue after that run the consumer..

Console output after running Message consumer
https://github.com/kkvinodkumaran/myrepository/tree/master/vinod-jms
Goals
- Setup ActiveMQ
- Create a JMS Message Producer
- Create a JMS Message Consumer
In this example we are using windows so please follow the below steps to start ActiveMQ messaging system.
1. Download activemq from http://activemq.apache.org/download-archives.html
2. Extract activemq zip file and start ActiveMQ (Click on activemq.bat file)
3. Check activemq console using http://localhost:8161/admin (Use default credentials admin/admin to login)
2. Create a Message Producer
Add below dependency in your pom.xml
<dependency>
<groupId>javax.jms</groupId>
<artifactId>javax.jms-api</artifactId>
<version>2.0.1</version>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-all</artifactId>
<version>5.7.0</version>
</dependency>
Message Producer<groupId>javax.jms</groupId>
<artifactId>javax.jms-api</artifactId>
<version>2.0.1</version>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-all</artifactId>
<version>5.7.0</version>
</dependency>
package com.vinod.jms;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.DeliveryMode;
import javax.jms.Destination;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
public class MessageProducerExample {
public static void main(String[] args) {
try {
// Create a ConnectionFactory
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("admin", "admin",
ActiveMQConnection.DEFAULT_BROKER_URL);
// Create a Connection
Connection connection = connectionFactory.createConnection();
connection.start();
// Create a Session
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
// Create the destination
Destination destination = session.createQueue("testQ");
// Create a MessageProducer from the Session to the Queue
MessageProducer producer = session.createProducer(destination);
producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
// Create a messages
TextMessage message = session.createTextMessage("Helloworld..by vinod");
producer.send(message);
session.close();
connection.close();
System.out.println("Message sent");
} catch (Exception e) {
e.printStackTrace();
}
}
}
3. Create a Message Consumerimport javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.DeliveryMode;
import javax.jms.Destination;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
public class MessageProducerExample {
public static void main(String[] args) {
try {
// Create a ConnectionFactory
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("admin", "admin",
ActiveMQConnection.DEFAULT_BROKER_URL);
// Create a Connection
Connection connection = connectionFactory.createConnection();
connection.start();
// Create a Session
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
// Create the destination
Destination destination = session.createQueue("testQ");
// Create a MessageProducer from the Session to the Queue
MessageProducer producer = session.createProducer(destination);
producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
// Create a messages
TextMessage message = session.createTextMessage("Helloworld..by vinod");
producer.send(message);
session.close();
connection.close();
System.out.println("Message sent");
} catch (Exception e) {
e.printStackTrace();
}
}
}
package com.vinod.jms;
import javax.jms.ConnectionFactory;
import javax.jms.Connection;
import javax.jms.Destination;
import javax.jms.MessageConsumer;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
import javax.jms.Message;
public class MessageConsumerExample {
public static void main(String[] args) {
try {
// Create a ConnectionFactory
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("admin", "admin",
ActiveMQConnection.DEFAULT_BROKER_URL);
// Create a Connection
Connection connection = connectionFactory.createConnection();
connection.start();
// Create a Session
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
// Create the destination
Destination destination = session.createQueue("testQ");
// Create a MessageConsumer from the Session to the Queue
MessageConsumer consumer = session.createConsumer(destination);
// Wait for a message
Message message = consumer.receive(1000);
if (message instanceof TextMessage) {
TextMessage textMessage = (TextMessage) message;
String text = textMessage.getText();
System.out.println("Text Message is " + text);
} else {
System.out.println(message);
}
consumer.close();
session.close();
connection.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
4. Run itimport javax.jms.ConnectionFactory;
import javax.jms.Connection;
import javax.jms.Destination;
import javax.jms.MessageConsumer;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
import javax.jms.Message;
public class MessageConsumerExample {
public static void main(String[] args) {
try {
// Create a ConnectionFactory
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("admin", "admin",
ActiveMQConnection.DEFAULT_BROKER_URL);
// Create a Connection
Connection connection = connectionFactory.createConnection();
connection.start();
// Create a Session
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
// Create the destination
Destination destination = session.createQueue("testQ");
// Create a MessageConsumer from the Session to the Queue
MessageConsumer consumer = session.createConsumer(destination);
// Wait for a message
Message message = consumer.receive(1000);
if (message instanceof TextMessage) {
TextMessage textMessage = (TextMessage) message;
String text = textMessage.getText();
System.out.println("Text Message is " + text);
} else {
System.out.println(message);
}
consumer.close();
session.close();
connection.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Once activemq started, then start Message producer first and push the messages in the queue after that run the consumer..
Console output after running Message consumer
Text Message is Helloworld..by vinod
5. Download examplehttps://github.com/kkvinodkumaran/myrepository/tree/master/vinod-jms
No comments:
Post a Comment