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