Activemq Getting Started instance

Source: Internet
Author: User

Download ACTIVEMQ 5.8.0 release version

http://download.csdn.net/download/xisdo/5744107

Get

And then into the bin package

Starting the service will see

Login: http://localhost:8161/admin/, we will see a login account and login password, respectively, admin,admin, how to see this login account and password?

We should first find the Jetty.xml configuration file, modify

class= "Org.eclipse.jetty.util.security.Constraint" >        <property name= "name" value= "BASIC"/>        <property name= "Roles" value= "admin"/>        <property name= "Authenticate" value= "true"/>    </ Bean>
Authenticate defaults to False, we should change to true and then view the Jetty-realm.properties file before we can see

After you log in successfully

Create a queue, named Firstqueue

3. Create an Eclipse project and run
Create a Java project:activemq-5.8, new Lib folder
Open the 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
These 5 jar files are in the Lib folder and build Path->add to build Path

The structure is as follows

Sender.java

 Packagecom.llmj.ActiveMQ.test;Importjavax.jms.Connection;Importjavax.jms.ConnectionFactory;ImportJavax.jms.DeliveryMode;Importjavax.jms.Destination;ImportJavax.jms.MessageProducer;Importjavax.jms.Session;ImportJavax.jms.TextMessage;Importorg.apache.activemq.ActiveMQConnection;Importorg.apache.activemq.ActiveMQConnectionFactory; Public classSender {Private Static Final intSend_number = 5;  Public Static voidMain (string[] args) {//ConnectionFactory: Connection Factory, JMS uses it to create a connectionConnectionFactory ConnectionFactory;//CONNECTION:JMS client to JMS//connection to the ProviderConnection Connection =NULL;//Session: A thread that sends or receives a messageSession session;//Destination: The destination of the message, to whom the message is sent. Destination Destination;//messageproducer: Message senderMessageProducer producer;//TextMessage message; //constructs the ConnectionFactory instance object, where the ACTIVEMQ implementation jar is usedConnectionFactory =Newactivemqconnectionfactory (Activemqconnection.default_user, Activemqconnection.defa Ult_password,"tcp://localhost:61616"); Try{//construction Gets the connection object from the factoryConnection =connectionfactory.createconnection (); //StartConnection.start (); //Get Operation ConnectionSession =connection.createsession (Boolean.true, Session.auto_acknowledge); //Get session Note parameter value Xingbo.xu-queue is a server queue that must be configured in the ACTIVEMQ consoleDestination = Session.createqueue ("Firstqueue"); //get the message generator "sender"Producer =session.createproducer (destination); //set not persisted, learn here, actually according to the project decisionProducer.setdeliverymode (deliverymode.non_persistent); //constructs a message, writes dead here, the item is a parameter, or method getsSendMessage (session, producer);          Session.commit (); } Catch(Exception e) {e.printstacktrace (); } finally {              Try {                  if(NULL!=connection) Connection.close (); } Catch(Throwable ignore) {}}}  Public Static voidSendMessage (Session session, MessageProducer producer)throwsException { for(inti = 1; I <= Send_number; i++) {TextMessage message= Session.createtextmessage ("message sent by ActiveMq" +i); //send message to Destination placeSystem.out.println (i+ ". Send message:" + "ACTIVEMQ messages sent" +i);          Producer.send (message); }      }  }

Receiver.java

 Packagecom.llmj.ActiveMQ.test;Importjavax.jms.Connection;Importjavax.jms.ConnectionFactory;Importjavax.jms.Destination;ImportJavax.jms.MessageConsumer;Importjavax.jms.Session;ImportJavax.jms.TextMessage;Importorg.apache.activemq.ActiveMQConnection;Importorg.apache.activemq.ActiveMQConnectionFactory; Public classReceiver { Public Static voidMain (string[] args) {//ConnectionFactory: Connection Factory, JMS uses it to create a connectionConnectionFactory ConnectionFactory; //CONNECTION:JMS Client-to-JMS Provider connectionsConnection Connection =NULL; //Session: A thread that sends or receives a messageSession session; //Destination: The destination of the message, to whom the message is sent. Destination Destination; //consumer, message recipientMessageconsumer Consumer; ConnectionFactory=Newactivemqconnectionfactory (Activemqconnection.default_user, Activemqconnection.defa Ult_password,"tcp://localhost:61616"); Try {              //construction Gets the connection object from the factoryConnection =connectionfactory.createconnection (); //StartConnection.start (); //Get Operation ConnectionSession =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 consoleDestination = 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 100sTextMessage 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 First: Receiver.java
Re-run: Sender.java

can see the results
After sender runs:
1. Send message: Message sent by ActiveMq 1
2. Send message: Message sent by ActiveMq 2
3. Send message: Message sent by ActiveMq 3
4. Send message: Message sent by ActiveMq 4
5. Send message: Message sent by ActiveMq 5

After receiver runs:
Receive Message ACTIVEMQ message sent 1
Receive Message ACTIVEMQ message sent 2
Receive Message ACTIVEMQ message sent 3
Receive Message ACTIVEMQ message sent 4
Receive Message ACTIVEMQ message sent 5

To see different output, switch the console by clicking the button like

We set the time in the code to 10S, if we want to end it early.

Click on the red block to stop the program from running manually.

Activemq Getting Started instance

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.