Activemq instance tutorial-entry level

Source: Internet
Author: User
Document directory
  • 3.1.sender.java
  • 3.2.receiver.java
Activemq entry instance
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 the activemq program.

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; Pu BLIC static void main (string [] ARGs) {// connectionfactory: Connection factory. JMS uses it to create a connection connectionfactory; // connection: connection connection from the JMS client to the JMS provider = NULL; // session: the session of the thread that sends or receives the message; // destination: the Message destination; to which the message is sent. destination destination; // messageproducer: message sender messageproducer producer; // textmessage; // construct the connectionfactory instance object. The activemq implementation J is used here. Ar connectionfactory = new activemqconnectionfactory (activemqconnection. default_user, activemqconnection. default_password, "TCP: // localhost: 61616"); try {// construct the connection object connection = connectionfactory from the factory. createconnection (); // start connection. start (); // get the operation connection session = connection. createsession (Boolean. true, session. auto_acknowledge); // obtains the xingbo parameter value of the session. xu-queue is a server's queue, which must be configured on the activemq Console Destination = session. createqueue ("firstqueue"); // producer = session. createproducer (destination); // The setting is not persistent. Learn here and decide the producer according to the project. setdeliverymode (deliverymode. non_persistent); // construct a message. The message is written to an end here. The project is a parameter, or the method used to obtain 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 ("message sent by activemq" + I); // send the message to the destination system. out. println ("Send message:" + "message sent by activemq" + 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 er {public static void main (string [] ARGs) {// connectionfactory: Connection factory, JMS Use it to create a connection connectionfactory; // connection: Connection connection from the JMS client to the JMS provider = NULL; // session: A session for sending or receiving messages; // destination: the destination of the message. destination destination; // consumer, Message Receiver messageconsumer consumer; connectionfactory = new activemqconnectionfactory (activemqconnection. default_user, activemqconnection. default_password, "TCP: // localhost: 61616"); tr Y {// construct the connection object connection = connectionfactory from the factory. createconnection (); // start connection. start (); // get the operation connection session = connection. createsession (Boolean. false, session. auto_acknowledge); // obtains the xingbo parameter value of the session. xu-queue is a server's queue. You must configure Destination = session on the activemq console. createqueue ("firstqueue"); consumer = session. createconsumer (destination); While (true) {// set the time when the receiver receives the message. To facilitate the test, who is set to 100 s textme? Ssage 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 shown in the following code:

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.