activemq startup guide · 2020-01-07 · activemq startup guide 1 activemq basic guide activemq is...
TRANSCRIPT
ActiveMQ Startup Guide 1
ActiveMQ Basic Guide
ActiveMQ is a low level messaging services that internally uses JMS Java
Messaging Services and many additional features so It is JMS + MQ. Its
basic benefit is its is language independent i.e. You can use ActiveMQ
Services with c, c++, c#, Ruby, Pearl, PHP, Java etc It provides API that
supports multiple clients and this feature make ActiveMQ robust in use.
Apart from it, it supports a large number of protocols OpenWire, Stomp
etc and also It supports various transport protocol like Http, Https, SSL,
NIO, TCP UDP and more
We don’t need to write the code from base for consumer and producer
from basic we get preformed API and methods so that we can directly
utilize them and swing our requirements out of them easily.
First Download ActiveMQ from the following URL
http://activemq.apache.org/download.html
2 ActiveMQ Startup Guide
Figure 1 : Download ActiveMQ
Download the latest stable release as it contains patches for all
previous bugs
Extract the zip file to c:\ Drive
Set the PATH in Environment variable
ACTIVEMQ_HOME = C:\apache-activemq-5.9
PATH = %ACTIVEMQ_HOME%\bin
Now starting ActiveMQ server
Goto /bin directory of ActiveMQ unzipped folder here is the complete
path
C:\apache-activemq-5.9\bin
ActiveMQ Startup Guide 3
Figure 2 : activemq.exe in directory
Execute activemq from terminal and you will see following output
Figure 3 : Starting ActiveMQ Server
As shown in terminal the activemq server with the KahaDb instance for
Message storage purpose is already started, KahaDB is the message storage
4 ActiveMQ Startup Guide
used by default, You can also use file based message storage and JDBC
message storage as per your requirement. Those all we will see in later
books since they are all individual chapters with their own specific
methodology and properties.
Creating a Producer Class
To create a Consumer Class we need to follow the following steps
Create ConnectionFactory using URL
Create connection using that connection factory
Create a session that builds connectivity for that particular
instance
Create a destination Queue or Topic that you want to use for that
session
Create Producer and set the destination so that producer produces
message for that destination
Create a message that you want to send to destination
Send the message using Session
import javax.jms.*;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import org.apache.log4j.BasicConfigurator;
public class Producer {
public static String subject = “MYQUEUE”;
private static String url = ActiveMQConnection.DEFAULT_BROKER_URL; public Producer() throws JMSException, NamingException {
// Obtain a JNDI connection
// Look up a JMS connection factory
ActiveMQ Startup Guide 5
ConnectionFactory connectionFactory
= new ActiveMQConnectionFactory(url);
// Getting JMS connection from the server and starting it
Connection connection = connectionFactory.createConnection(); try {
connection.start();
// JMS messages are sent and received using a Session. We will
// create here a non-transactional session object. If you want
// to use transactions you should set the first parameter to 'true'
Session session = connection.createSession(false,
Session.AUTO_ACKNOWLEDGE);
Destination destination = session.createQueue(subject);
// MessageProducer is used for sending messages (as opposed
// to MessageConsumer which is used for receiving them)
MessageProducer producer = session.createProducer(destination);
// We will send a small text message saying 'Hello World!'
TextMessage message = session.createTextMessage("Hello World!");
// Here we are sending the message!
producer.send(message);
System.out.println("Sent message '" + message.getText() + "'");
} finally {
connection.close();
}
}
public static void main(String[] args) throws JMSException {
try {
BasicConfigurator.configure();
new Producer();
} catch (NamingException e) {
e.printStackTrace();
}
}
}
Creating a Consumer Class
BasicConfigurator is used to put logs in data folder in ActiveMQ
Create ConnectionFactory using URL
6 ActiveMQ Startup Guide
Create connection using that connection factory
Create a session that builds connectivity for that particular instance
Create Consumer and set Destination for it
Now Consumer thread listens for the message in that particular
session with given Queue name.
import javax.jms.*;
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.log4j.BasicConfigurator;
public class Consumer {
// URL of the JMS server
private static String url = ActiveMQConnection.DEFAULT_BROKER_URL;
// Name of the queue we will receive messages from
private static String subject = "MYQUEUE";
public static void main(String[] args) throws JMSException {
BasicConfigurator.configure();
// Getting JMS connection from the server
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);
Connection connection = connectionFactory.createConnection();
connection.start();
// Creating session for seding messages
Session session = connection.createSession(false,
Session.AUTO_ACKNOWLEDGE);
// Getting the queue
Destination destination = session.createQueue(subject);
// MessageConsumer is used for receiving (consuming) messages
MessageConsumer consumer = session.createConsumer(destination);
// Here we receive the message.
// By default this call is blocking, which means it will wait
// for a message to arrive on the queue.
Message message = consumer.receive();
// There are many types of Message and TextMessage
// is just one of them. Producer sent us a TextMessage
// so we must cast to it to get access to its .getText()
// method.
if (message instanceof TextMessage) {
TextMessage textMessage = (TextMessage) message;
System.out.println("Received message '" + textMessage.getText()
+ "'");
ActiveMQ Startup Guide 7
}
connection.close();
}
}
How To Execute Consumer and Producer
Since we have confirmed that we already had set the environment variable
JAVA_HOME in environment Variable. Since for compiling the
Consumer and Producer we need the Java and JDK installed and
configured in our system.
To install JDK
http://www.oracle.com/technetwork/java/javase/downloads/index.html
Set Environment Variable Both Java_Home and Path.
8 ActiveMQ Startup Guide
Figure 4 : Setting Java_Home and classpath variables
Now goto folder ActiveMQ in C:\ Drive, copy the ActiveMQ-all-5.9 Jar
and paste it to your JDK lib folder.
Goto folder where you have created your classes via terminal
Suppose we have created classes to c:\classses folder then
Move to c:\classes using terminal now execute Javac
Consumer(Compile the class)
Execute Javac Producer (compile the class)
Again Java Consumer and Java Producer from terminal (Execute the
class)
ActiveMQ Startup Guide 9
Note: - First consumer class should be invoked and then producer
After executing it you can see following lines of code on terminal.
Figure 5: Console After Execution of Program
As we saw we already have send a successful Message from Producer to
Consumer.
Now to see whether how the queue message was processed between
Consumer and Producer, or in either case if we want to see the message
status in queue whether it is dequeued or not.
We need to open the admin console for ActriveMQ
Open the browser and type
http://localhost:8161
10 ActiveMQ Startup Guide
o Username: admin
o Password: admin
We will see the following thing:
From the given below figure we can easily analyze that the consumer
consumes a message
Figure 6: ActiveMQ Admin console
Executing ActiveMQ using Eclipse
ActiveMQ Startup Guide 11
Download Eclipse from
o http://www.eclipse.org/downloads/
Extract eclipse according to your convenience drive
Start Eclipse
Create a New Java Project as Shown in Figure
Figure 7: Creating new Project in Eclipse
12 ActiveMQ Startup Guide
Now Right click on the Project and Select properties
Figure 8: Going to Properties
Select Java Build Path
Goto Libraries Tab and select Add External Jar As shown in figure
ActiveMQ Startup Guide 13
Figure 9 : Adding external Jars
Goto Location where your ActiveMQ is extracted and select the
activemq-all-***.jar file so that it will allow all the classes to get
Created into it.
As show in below figure select the activemq-all-5.8.jar
14 ActiveMQ Startup Guide
Figure 10: Selecting ActiveMQ jar
So that finally it appears something like this in project
ActiveMQ Startup Guide 15
Now create a package under src/ folder and create two classes
Producer and Consumer
Producer Class
package com.test;
import javax.jms.*;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import org.apache.log4j.BasicConfigurator;
public class Producer {
public static String subject = “MYQUEUE”;
private static String url = ActiveMQConnection.DEFAULT_BROKER_URL; public Producer() throws JMSException, NamingException {
// Obtain a JNDI connection
// Look up a JMS connection factory
ConnectionFactory connectionFactory
= new ActiveMQConnectionFactory(url);
// Getting JMS connection from the server and starting it
Connection connection = connectionFactory.createConnection(); try {
connection.start();
// JMS messages are sent and received using a Session. We will
// create here a non-transactional session object. If you want
// to use transactions you should set the first parameter to 'true'
Session session = connection.createSession(false,
Session.AUTO_ACKNOWLEDGE);
Destination destination = session.createQueue(subject);
// MessageProducer is used for sending messages (as opposed
// to MessageConsumer which is used for receiving them)
MessageProducer producer = session.createProducer(destination);
// We will send a small text message saying 'Hello World!'
TextMessage message = session.createTextMessage("Hello World!");
// Here we are sending the message!
producer.send(message);
System.out.println("Sent message '" + message.getText() + "'");
} finally {
connection.close();
}
}
16 ActiveMQ Startup Guide
public static void main(String[] args) throws JMSException {
try {
BasicConfigurator.configure();
new Producer();
} catch (NamingException e) {
e.printStackTrace();
}
}
}
Consumer Class
import javax.jms.*;
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.log4j.BasicConfigurator;
public class Consumer {
// URL of the JMS server
private static String url = ActiveMQConnection.DEFAULT_BROKER_URL;
// Name of the queue we will receive messages from
private static String subject = "MYQUEUE";
public static void main(String[] args) throws JMSException {
BasicConfigurator.configure();
// Getting JMS connection from the server
ConnectionFactory connectionFactory = new
ActiveMQConnectionFactory(url);
Connection connection = connectionFactory.createConnection();
connection.start();
// Creating session for seding messages
Session session = connection.createSession(false,
Session.AUTO_ACKNOWLEDGE);
// Getting the queue
Destination destination = session.createQueue(subject);
// MessageConsumer is used for receiving (consuming) messages
MessageConsumer consumer = session.createConsumer(destination);
// Here we receive the message.
// By default this call is blocking, which means it will wait
// for a message to arrive on the queue.
Message message = consumer.receive();
// There are many types of Message and TextMessage
// is just one of them. Producer sent us a TextMessage
// so we must cast to it to get access to its .getText()
// method.
if (message instanceof TextMessage) {
ActiveMQ Startup Guide 17
TextMessage textMessage = (TextMessage) message;
System.out.println("Received message '" + textMessage.getText()
+ "'");
}
connection.close();
}
}
Figure 11: Producer and Consmer Class
Now start ActiveMQ server as shown previously before executing
Consumer and Producer once the server is started.
18 ActiveMQ Startup Guide
Figure 12: Executing ActiveMQ class in eclipse
Now Execute the consumer and in similar way execute the producer
you will see following things on console
After executing in eclipse, eclipse console shows following log
ActiveMQ Startup Guide 19
Now we need to check on console whether or not the message
has successfully processed or not
Open Browser and Type http://localhst:8161
We get Following Result
20 ActiveMQ Startup Guide
Figure 13: ActiveMQ Admin Console
Here we can see that Messages Enqueued and Messages Dequeued
contains ‘1 ’, indicates that our message was successfully produced
by Producer and successfully consumed by consumer.