Activemq Getting Started instance

Source: Internet
Author: User

1. Download ACTIVEMQ

Go to official website download: http://activemq.apache.org/

2. Running ACTIVEMQ

Unzip Apache-activemq-5.5.1-bin.zip, and then double-click Apache-activemq-5.5.1\bin\activemq.bat to run the ACTIVEMQ program.

After starting ACTIVEMQ, log in: http://localhost:8161/admin/, create a queue, named Firstqueue.

3. Create an Eclipse project and run

Create the project:activemq-5.5 and import the jar files that are needed in the Apache-activemq-5.5.1\lib directory, as shown in the project structure:

3.1.sender.java
Package com.xuwei.activemq;

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 Sender {
private static final int send_number = 5;

public static void Main (string[] args) {
ConnectionFactory: Connection Factory, JMS uses it to create a connection
ConnectionFactory ConnectionFactory;
CONNECTION:JMS client-to-JMS Provider connections
Connection Connection = null;
Session: A thread that sends or receives a message
Session session;
Destination: The destination of the message, to whom the message is sent.
Destination Destination;
MessageProducer: Message Sender
MessageProducer producer;
TextMessage message;
Constructs the ConnectionFactory instance object, where the ACTIVEMQ implementation jar is used
ConnectionFactory = new Activemqconnectionfactory (
Activemqconnection.default_user,
Activemqconnection.default_password,
"tcp://localhost:61616");
try {
Construction gets the connection object from the factory
Connection = Connectionfactory.createconnection ();
Start
Connection.start ();
Get Operation Connection
Session = Connection.createsession (Boolean.true,
Session.auto_acknowledge);
Get session Note parameter value Xingbo.xu-queue is a server queue that must be configured in the ACTIVEMQ console
Destination = Session.createqueue ("Firstqueue");
Get the message generator "sender"
Producer = Session.createproducer (destination);
Set not persisted, learn here, actually according to the project decision
Producer.setdeliverymode (deliverymode.non_persistent);
Constructs a message, writes dead here, the item is a parameter, or method gets
SendMessage (session, producer);
Session.commit ();
} catch (Exception e) {
E.printstacktrace ();
} finally {
try {
if (null! = connection)
Connection.close ();
} catch (Throwable ignore) {
}
}
}

public static void SendMessage (Session session, MessageProducer producer)
Throws Exception {
for (int i = 1; I <= send_number; i++) {
TextMessage message = Session
. Createtextmessage ("message sent by ActiveMq" + i);
Send a message to the destination party
SYSTEM.OUT.PRINTLN ("Send message:" + "ActiveMq message sent" + i);
Producer.send (message);
}
}
}
3.2.receiver.java
 PackageCom.xuwei.activemq;Importjavax.jms.Connection;Importjavax.jms.ConnectionFactory;Importjavax.jms.Destination;ImportJavax.jms.MessageConsumer;Importjavax.jms.Session;ImportJavax.jms.TextMessage;Importorg.apache.activemq.ActiveMQConnection;Importorg.apache.activemq.ActiveMQConnectionFactory; Public classReceiver { Public Static voidMain (string[] args) {//ConnectionFactory: Connection Factory, JMS uses it to create a connectionConnectionFactory ConnectionFactory; //CONNECTION:JMS Client-to-JMS Provider connectionsConnection Connection =NULL; //Session: A thread that sends or receives a messageSession session; //Destination: The destination of the message, to whom the message is sent.Destination Destination; //consumer, message recipientMessageconsumer Consumer; ConnectionFactory=Newactivemqconnectionfactory (Activemqconnection.default_user, Activemqconnection.default_ PASSWORD,"tcp://localhost:61616"); Try {            //construction Gets the connection object from the factoryConnection =connectionfactory.createconnection (); //StartConnection.start (); //Get Operation ConnectionSession =connection.createsession (Boolean.false, Session.auto_acknowledge); //Get session Note parameter value Xingbo.xu-queue is a server queue that must be configured in the ACTIVEMQ consoleDestination = Session.createqueue ("Firstqueue"); Consumer=Session.createconsumer (destination);  while(true) {                //set the receiver to receive the message time, in order to facilitate testing, here who set the 100sTextMessage message = (textmessage) consumer.receive (100000); if(NULL!=message) {System.out.println ("Receive Message" +Message.gettext ()); } Else {                     Break; }            }        } Catch(Exception e) {e.printstacktrace (); } finally {            Try {                if(NULL!=connection) Connection.close (); } Catch(Throwable ignore) {} }}}

4. Precautions
    1. Finally, the receiver and the sender are tested on different machines.
    2. The jar referenced by the project is finally found in Lib under Activemq, so there is no version conflict.
5. Test process

Because it is tested on a single machine, you need to open two eclipse, each of which has its own workspace. We run receiver in Eclipse1 and run sender in Eclipse2.

The console interface does not have any information after running receiver in Eclipse1, and after running sender in Eclipse2, the console in ECLIPSE2 displays the following information:

Send message: Message sent by ActiveMq 1
Send message: Message sent by ActiveMq 2
Send message: Message sent by ActiveMq 3
Send message: Message sent by ActiveMq 4
Send message: Message sent by ActiveMq 5

And back to ECLIPSE1, the console interface appears with the following information:

Receive Message ACTIVEMQ message sent 1
Receive Message ACTIVEMQ message sent 2
Receive Message ACTIVEMQ message sent 3
Receive Message ACTIVEMQ message sent 4
Receive Message ACTIVEMQ message sent 5

Ps:2012-2-27

Today the test does not need to open two eclipse, in an Eclipse next page can start multiple programs, and there are multiple console, in the above Receiver.java, set a larger time, such as receive (500000), as shown in the following code:

TextMessage message = (textmessage) consumer.receive (500000);

Running Receiver.java at this time will allow the Receiver.java to run for 500 seconds, which can be found in eclipse:

Click on the red block to stop running the program manually.

After running the receiver we are running sender, after we run sender, we want to switch to receiver's console, as shown in:

Activemq Getting Started instance

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.