Example of queue send and receive for ACTIVEMQ

Source: Internet
Author: User
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 Senderqueue {


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://ip_address:61616");
try {
Construction gets the connection object from the factory
Connection = Connectionfactory.createconnection ();
Start
Connection.start ();
Get Operation Connection
Session = Connection.createsession (true, Session.auto_acknowledge);
Get session
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);
Producer.setdeliverymode (deliverymode.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
Producer.send (message);
SYSTEM.OUT.PRINTLN ("Send message:" + "ActiveMq message sent" + i);
}


}


}






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 Receivequeue {
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;


Consumer, message recipient
Messageconsumer consumer;


ConnectionFactory = new Activemqconnectionfactory (


Activemqconnection.default_user,


Activemqconnection.default_password,


"tcp://ip_address:61616");


try {


Construction gets the connection object from the factory
Connection = Connectionfactory.createconnection ();


Start
Connection.start ();


Get Operation Connection
Session = Connection.createsession (False,


Session.auto_acknowledge);


Get session
Destination = 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 100s
TextMessage message = (textmessage) consumer.receive (100000);
if (null! = message) {
System.out.println ("received message" + Message.gettext ());
} else {
Break
}


}


} catch (Exception e) {


E.printstacktrace ();


} finally {


try {


if (null! = connection)


Connection.close ();


} catch (Throwable ignore) {


}


}


}

Some other information can be referred to ACTIVEMQ's website and its list of issues, as follows:

http://activemq.apache.org/

Http://activemq.apache.org/faq.html

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.