Java Message Mechanism ActiveMQ entry instance, javaactivemq

Source: Internet
Author: User

Java Message Mechanism ActiveMQ entry instance, javaactivemq

1. Download ActiveMQ
Go to the official website download: http://activemq.apache.org/
I downloaded ActiveMQ 5.8.0 Release.

2. Run ActiveMQ
Uncompress apache-activemq-5.8.0-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 java project: ActiveMQ-5.8, new lib folder
Open apache-activemq-5.8.0 \ lib directory
Copy
Activemq-broker-5.8.0.jar
Activemq-client-5.8.0.jar
Geronimo-j2ee-management_1.1_spec-1.0.1.jar
Geronimo-jms_1.1_spec-1.1.1.jar
Slf4j-api-1.6.6.jar
Add these five jar files to the lib folder and Build Path-> Add to Build Path

Structure:

 


Sender. java

Package com. lm. activemq;/*** @ Header: Sender. java * class description: * @ author: lm * @ date 10:52:42 * @ Email * @ company Huan * @ addr Jinsong, Chaoyang District, Beijing */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; impo Rt 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 ConnectionFactory connectionFactory; // Connection: Connection connection Connection from the JMS client to the JMS // Provider = null; // Session: the Session session of a thread that sends or receives messages; // Destination: the Message Destination; to whom the message is sent. destination destination; // Messa GeProducer: message sender MessageProducer producer; // TextMessage message; // construct the ConnectionFactory instance object. The implementation of ActiveMq jarconnectionFactory = 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. You must configure destination = session on the ActiveMq console. 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 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 );}}}

  

 

Receiver. java

 

Package com. lm. activemq;/*** @ Header: Receiver. java * class description: * @ author: lm * @ date 10:52:58 * @ Email * @ company Huan * @ addr Jinsong, Chaoyang District, Beijing */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. activ EMQConnectionFactory; public class Receiver {public static void main (String [] args) {// ConnectionFactory: Connection factory, and JMS uses it to create a Connection ConnectionFactory connectionFactory; // Connection: connection connection from the JMS client to the JMS Provider = null; // Session: the Session session of the thread that sends or receives the message; // Destination: the Message Destination; to which the message is sent. destination destination; // consumer, Message Receiver MessageConsumer consumer; connectionFactory = new ActiveMQConnectionFa Ctory (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. 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 for the receiver to receive the message. To facilitate the test, who set this parameter to 100 sTextMessage 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 ){}}}}

  

 

5. Test process
Run: Er er. java
Run Sender. java again.

The result is displayed.
After Sender is run:
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

After the worker is running:
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

To view different output content, click the button as shown in.

 


In Receiver. java, you can set a time, such as receive (500000), as shown in the following code:

Java code
  1. 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.

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.