Activemq entry instance

Source: Internet
Author: User
ArticleDirectory
    • 3.1.sender.java
    • 3.2.receiver.java
1. Download activemq

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

2. Run activemq

Uncompress apache-activemq-5.5.1-bin.zip, and double-click the apache-activemq-5.5.1 \ bin \ activemq. BAT to run activemqProgram.

After activemq is started, log on to: http: // localhost: 8161/admin/and create a queue named firstqueue.

3. Create and run an Eclipse project

Create a project: ActiveMQ-5.5 and import the jar files to be used under 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, which is used by JMS to create a connection
Connectionfactory;
// Connection: the connection from the JMS client to the JMS provider
Connection connection = Null ;
// Session: a thread that sends or receives messages.
Session session;
// Destination: the destination of the message.
Destination destination;
// Messageproducer: message sender
Messageproducer producer;
// Textmessage message;
// Construct the connectionfactory instance object. The activemq implementation jar is used here.
Connectionfactory = New Activemqconnectionfactory (
Activemqconnection. default_user,
Activemqconnection. default_password,
"TCP: // localhost: 61616 ");
Try {
// Construct a connection object from the factory
Connection = connectionfactory. createconnection ();
// Start
Connection. Start ();
// Get operation connection
Session = connection. createsession (Boolean. True,
Session. auto_acknowledge );
// Obtain the session parameter value. xingbo. Xu-queue is a server's queue and must be configured on the activemq console.
Destination = session. createqueue ("firstqueue ");
// Message generator [Sender]
Producer = session. createproducer (destination );
// The settings are not persistent. Learn here and decide based on the project.
Producer. setdeliverymode (deliverymode. non_persistent );
// Construct a message. The message is written to an end. The project is a parameter or a method to obtain the message.
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, messageproducer producer)
Throws Exception {
For ( Int I = 1; I <= send_number; I ++ ){
Textmessage message = session
. Createtextmessage ("messages sent by activemq" + I );
// Send a message to the destination
System. Out. println ("Send message:" + "activemq sent message" + I );
Producer. Send (Message );
}
}
}
3.2.receiver.java
 Package Com. xuwei. activemq;

Import Javax. JMS. connection;
Import Javax. JMS. connectionfactory;
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;

Public Class Explorer {
Public Static Void Main (string [] ARGs ){
// Connectionfactory: Connection factory, which is used by JMS to create a connection
Connectionfactory;
// Connection: the connection from the JMS client to the JMS provider
Connection connection = Null ;
// Session: a thread that sends or receives messages.
Session session;
// Destination: the destination of the message.
Destination destination;
// Consumer, message recipient
Messageconsumer consumer;
Connectionfactory = New Activemqconnectionfactory (
Activemqconnection. default_user,
Activemqconnection. default_password,
"TCP: // localhost: 61616 ");
Try {
// Construct a connection object from the factory
Connection = connectionfactory. createconnection ();
// Start
Connection. Start ();
// Get operation connection
Session = connection. createsession (Boolean. False,
Session. auto_acknowledge );
// Obtain the session parameter value. xingbo. Xu-queue is a server's queue and must be configured on the activemq console.
Destination = session. createqueue ("firstqueue ");
Consumer = session. createconsumer (destination );
While ( True ){
// Set the time for the receiver to receive the message. To facilitate the test, who is set to 100 s?
Textmessage 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. Notes
    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 that no version conflict occurs.
5. Test process

Because it is tested on a single machine, you need to enable two eclipse, each of which has its own workspace. We run the handler in eclipse1 and the sender in eclipse2.

The console interface does not have any information after the supervisor is run in eclipse1. After the sender is run in eclipse2, the console in eclipse2 displays the following information:

Send message: Message 1 sent by activemq
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

Return to eclipse1 and find the following information on the console interface:

Receive message 1 sent by activemq
Receive the message sent by activemq 2
3. receive the message sent by activemq
Receive the message sent by activemq 4
Receive the message sent by activemq 5

PS: 2012-2-27

Today, we found that you do not need to enable two eclipse for testing. You can start multiple programs and have multiple consoles on the next page of Eclipse. in Java, set a large time, such as receive (500000), as follows:CodeAs shown in:

 
Textmessage message = (textmessage) Consumer. Receive (500000 );

If you run ER er. Java at this time, it will keep the worker er. Java running for 500 seconds. In eclipse, you can find that:

Click the Red Square to manually stop running the program.

After running the worker er, we are running the sender. After running the sender, we need to switch to the worker er console, as shown in:

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.