First, the Environment preparation
1, official website http://activemq.apache.org/Download the latest version of ACTIVEMQ, and unzip
2, open the corresponding directory, in the MAC environment, you can generally run the command:
cd/users/***/downloads/apache-activemq-***/bin/macosx
./activemq Start
3, after successful startup, log in http://localhose:8161/admin/, login account and password are admin, create a queue named Jyqueue.
Ii. Creating an Eclipse project
1. Create a new project and import the corresponding jar file as shown in
2,sender.java and Receiver.java
1 PackageCom.simple.activemq;2 3 Importjavax.jms.Connection;4 Importjavax.jms.ConnectionFactory;5 ImportJavax.jms.DeliveryMode;6 Importjavax.jms.Destination;7 ImportJavax.jms.MessageProducer;8 Importjavax.jms.Session;9 ImportJavax.jms.TextMessage;Ten Importorg.apache.activemq.ActiveMQConnection; One Importorg.apache.activemq.ActiveMQConnectionFactory; A - Public classSender { - Private Static Final intSend_number = 5; the - Public Static voidMain (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 A Session session; at //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 inConnectionFactory =NewActivemqconnectionfactory ( - Activemqconnection.default_user, to Activemqconnection.default_password, +"tcp://localhost:61616"); - Try { the //construction Gets the connection object from the factory *Connection =connectionfactory.createconnection (); $ //StartPanax Notoginseng Connection.start (); - //Get Operation Connection theSession =connection.createsession (Boolean.true, + Session.auto_acknowledge); A //Get session Note parameter value Xingbo.xu-queue is a server queue that must be configured in the ACTIVEMQ console theDestination = Session.createqueue ("Jyqueue"); + //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); - //constructs a message, writes dead here, the item is a parameter, or method gets - SendMessage (session, producer); the session.commit (); -}Catch(Exception e) {Wuyi e.printstacktrace (); the}finally { - Try { Wu if(NULL!=connection) - connection.close (); About}Catch(Throwable ignore) { $ } - } - } - A Public Static voidSendMessage (Session session, MessageProducer producer) + throwsException { the for(inti = 1; I <= Send_number; i++) { -TextMessage message =Session $. Createtextmessage ("Messages sent by ActiveMq" +i); the //send message to Destination place theSYSTEM.OUT.PRINTLN ("Send message:" + "ActiveMq message sent" +i); the producer.send (message); the } - } in}
View Code
1 PackageCom.simple.activemq;2 3 Importjavax.jms.Connection;4 Importjavax.jms.ConnectionFactory;5 Importjavax.jms.Destination;6 ImportJavax.jms.MessageConsumer;7 Importjavax.jms.Session;8 ImportJavax.jms.TextMessage;9 Importorg.apache.activemq.ActiveMQConnection;Ten Importorg.apache.activemq.ActiveMQConnectionFactory; One A Public classReceiver { - Public Static voidMain (string[] args) { - //ConnectionFactory: Connection Factory, JMS uses it to create a connection the 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; A //consumer, message recipient at Messageconsumer consumer; -ConnectionFactory =NewActivemqconnectionfactory ( - Activemqconnection.default_user, - Activemqconnection.default_password, -"tcp://localhost:61616"); - Try { in //construction Gets the connection object from the factory -Connection =connectionfactory.createconnection (); to //Start + Connection.start (); - //Get Operation Connection theSession =connection.createsession (Boolean.false, * Session.auto_acknowledge); $ //Get session Note parameter value Xingbo.xu-queue is a server queue that must be configured in the ACTIVEMQ consolePanax NotoginsengDestination = Session.createqueue ("Jyqueue"); -Consumer =Session.createconsumer (destination); the while(true) { + //set the receiver to receive the message time, in order to facilitate testing, here who set the 100s ATextMessage message = (textmessage) consumer.receive (100000); the if(NULL!=message) { +SYSTEM.OUT.PRINTLN ("Message received" +Message.gettext ()); -}Else { $ Break; $ } - } -}Catch(Exception e) { the e.printstacktrace (); -}finally {Wuyi Try { the if(NULL!=connection) - connection.close (); Wu}Catch(Throwable ignore) { - } About } $ } -}
View Code
3, successively run Sender.java and Receiver.java
ACTIVEMQ instances of simple send and receive messages