ACTIVEMQ Learning Summary Java Message Queuing--ACTIVEMQ combat

Source: Internet
Author: User
Tags ip number tomcat server

Ext: https://www.cnblogs.com/jaycekon/p/6225058.html Thank the author

ACTIVEMQ Official website: http://activemq.apache.org/download.html

ActiveMQ provides several versions of Windows and Linux, Unix, and so on, the landlord chose the Linux version for development.

After downloading the installation package, unzip the directory:

 

From its directory, it is still very simple:

      • The bin holds the script file
      • Conf storage is a basic configuration file
      • Data is stored in a log file
      • Docs is stored in the documentation
      • Examples storage is a simple example
      • Lib is storing the jar package required by ACTIVEMQ
      • WebApps directory for storing projects

2. Start ACTIVEMQ

Enter the bin directory to the ACTIVEMQ installation directory, under Linux input./activemq start starts the ACTIVEMQ service.

After entering the command, we are prompted to create a process IP number, which indicates that the service has successfully started.

  

ACTIVEMQ launches the built-in jetty server by default, providing an admin app for monitoring activemq.
admin:http://127.0.0.1:8161/admin/

We enter the account password after the browser opens the link (this is similar to the Tomcat server)

Default account: admin

Password: admin

  

So far, the ActiveMQ service has been started.

ActiveMQ the Stop command under Linux is./ACTIVEMQ stop

3. Create a ACTIVEMQ project

Project directory Structure:

  

When we download activemq on our website, we can see a jar package in the directory:

  

This jar is the dependency that we need to use in the development of the project.

3.1 Creating a producer
public class Producter {//activemq Default user name private static final String USERNAME = Activemqconnection.default_user;    ActiveMq Default login password private static final String PASSWORD = Activemqconnection.default_password;    ActiveMQ's link address private static final String Broken_url = Activemqconnection.default_broker_url;    Atomicinteger count = new Atomicinteger (0);    Link Factory connectionfactory connectionfactory;    Link Object Connection Connection;    Transaction management session session;    threadlocal<messageproducer> ThreadLocal = new threadlocal<> (); public void Init () {try {//Create a link factory connectionfactory = new Activemqconnectionfactory (Userna            Me,password,broken_url);            Create a link from the factory connection = Connectionfactory.createconnection ();            Open link connection.start ();        Create a transaction (here the level of the transaction can be set by parameters) session = Connection.createsession (true,session.session_transacted); } catch (JMSException E) {e.printstacktrace (); }} public void SendMessage (String disname) {try {//Create a Message Queuing queue queue = Session.cre            Atequeue (Disname);            Message producer MessageProducer messageproducer = null;            if (Threadlocal.get ()!=null) {messageproducer = Threadlocal.get ();                }else{messageproducer = session.createproducer (queue);            Threadlocal.set (MessageProducer);                } while (true) {thread.sleep (1000);                int num = Count.getandincrement ();                        Create a message TextMessage msg = Session.createtextmessage (Thread.CurrentThread (). GetName () + "Productor: I'm a handsome man, I'm making things now!"                , Count: "+num); System.out.println (Thread.CurrentThread (). GetName () + "Productor: I'm a stud, I'm making stuff now!"                , Count: "+num);                Send Message Messageproducer.send (msg);  Commit a transaction              Session.commit ();        }} catch (JMSException e) {e.printstacktrace ();        } catch (Interruptedexception e) {e.printstacktrace (); }    }}

3.2 Creating a consumer
public class Comsumer {private static final String USERNAME = Activemqconnection.default_user;    private static final String PASSWORD = Activemqconnection.default_password;    private static final String Broken_url = Activemqconnection.default_broker_url;    ConnectionFactory ConnectionFactory;    Connection Connection;    Session session;    threadlocal<messageconsumer> ThreadLocal = new threadlocal<> ();    Atomicinteger count = new Atomicinteger (); public void Init () {try {connectionfactory = new Activemqconnectionfactory (username,password,broken_url            );            Connection = Connectionfactory.createconnection ();            Connection.start ();        Session = Connection.createsession (False,session.auto_acknowledge);        } catch (JMSException e) {e.printstacktrace ();            }} public void GetMessage (String disname) {try {queue queue = Session.createqueue (disname); Messageconsumer Consumer= NULL;            if (Threadlocal.get ()!=null) {consumer = Threadlocal.get ();                }else{consumer = session.createconsumer (queue);            Threadlocal.set (consumer);                } while (true) {thread.sleep (1000);                TextMessage msg = (textmessage) consumer.receive ();                    if (msg!=null) {Msg.acknowledge (); System.out.println (Thread.CurrentThread (). GetName () + ": Consumer: I am a consumer, I am consuming msg" +msg.gettext () + "--->" +                Count.getandincrement ());                }else {break;        }}} catch (JMSException e) {e.printstacktrace ();        } catch (Interruptedexception e) {e.printstacktrace (); }    }}

4. Run ACTIVEMQ Project

4.1 Producers start producing messages
public class Testmq {public static void main (string[] args) {producter producter = new Producter ();        Producter.init ();        TESTMQ TESTMQ = new TESTMQ ();        try {thread.sleep (1000);        } catch (Interruptedexception e) {e.printstacktrace ();        }//thread 1 new Thread (testmq.new productormq (Producter)). Start ();        Thread 2 new Thread (Testmq.new productormq (Producter)). Start ();        Thread 3 new Thread (Testmq.new productormq (Producter)). Start ();        Thread 4 new Thread (Testmq.new productormq (Producter)). Start ();    Thread 5 new Thread (Testmq.new productormq (Producter)). Start ();        } Private class PRODUCTORMQ implements runnable{Producter Producter;        Public productormq (Producter producter) {this.producter = Producter; } @Override public void Run () {while (true) {try {producter.se Ndmessage ("Jaycekon-mq");                Thread.Sleep (10000);                } catch (Interruptedexception e) {e.printstacktrace (); }            }        }    }}

Operation Result:

INFO | Successfully connected to Tcp://localhost:61616thread-6productor: I am a handsome man, I am making things now! , Count:0thread-4productor: I'm a handsome man, I'm making things now! , Count:1thread-2productor: I'm a handsome man, I'm making things now! , Count:3thread-5productor: I'm a handsome man, I'm making things now! , Count:2thread-3productor: I'm a handsome man, I'm making things now! , Count:4thread-6productor: I'm a handsome man, I'm making things now! , Count:5thread-3productor: I'm a handsome man, I'm making things now! , Count:6thread-5productor: I'm a handsome man, I'm making things now! , Count:7thread-2productor: I'm a handsome man, I'm making things now! , Count:8thread-4productor: I'm a handsome man, I'm making things now! , Count:9thread-6productor: I'm a handsome man, I'm making things now! , Count:10thread-3productor: I'm a handsome man, I'm making things now! , Count:11thread-5productor: I'm a handsome man, I'm making things now! , Count:12thread-2productor: I'm a handsome man, I'm making things now! , Count:13thread-4productor: I'm a handsome man, I'm making things now! , Count:14thread-6productor: I'm a handsome man, I'm making things now! , Count:15thread-3productor: I'm a handsome man, I'm making things now! , Count:16thread-5productor: I'm a handsome man, I'm making things now! , Count:17thread-2productor: I'm a handsome man, I'm making things now! , Count:18thread-4productor: I'm a handsome man, I'm making things now! , count:19

  

4.2 Consumers start consuming messages
 public class Testconsumer {public static void main (string[] args) {Comsumer Comsumer = new Comsumer ();        Comsumer.init ();        Testconsumer Testconsumer = new Testconsumer ();        New Thread (Testconsumer.new consumermq (Comsumer)). Start ();        New Thread (Testconsumer.new consumermq (Comsumer)). Start ();        New Thread (Testconsumer.new consumermq (Comsumer)). Start ();    New Thread (Testconsumer.new consumermq (Comsumer)). Start ();        } Private class CONSUMERMQ implements runnable{Comsumer Comsumer;        Public consumermq (Comsumer comsumer) {this.comsumer = Comsumer; } @Override public void Run () {while (true) {try {Comsumer.get                    Message ("Jaycekon-mq");                Thread.Sleep (10000);                } catch (Interruptedexception e) {e.printstacktrace (); }            }        }    }}

Operation Result:

12345678910111213141516171819202122 INFO | Successfully connected to tcp://localhost:61616Thread-2: Consumer:我是消费者,我正在消费MsgThread-5productor:我是大帅哥,我现在正在生产东西!,count:4--->0Thread-3: Consumer:我是消费者,我正在消费MsgThread-4productor:我是大帅哥,我现在正在生产东西!,count:36--->1Thread-4: Consumer:我是消费者,我正在消费MsgThread-3productor:我是大帅哥,我现在正在生产东西!,count:38--->2Thread-5: Consumer:我是消费者,我正在消费MsgThread-6productor:我是大帅哥,我现在正在生产东西!,count:37--->3Thread-2: Consumer:我是消费者,我正在消费MsgThread-6productor:我是大帅哥,我现在正在生产东西!,count:2--->4Thread-3: Consumer:我是消费者,我正在消费MsgThread-5productor:我是大帅哥,我现在正在生产东西!,count:40--->5Thread-4: Consumer:我是消费者,我正在消费MsgThread-6productor:我是大帅哥,我现在正在生产东西!,count:42--->6Thread-5: Consumer:我是消费者,我正在消费MsgThread-4productor:我是大帅哥,我现在正在生产东西!,count:41--->7Thread-2: Consumer:我是消费者,我正在消费MsgThread-3productor:我是大帅哥,我现在正在生产东西!,count:1--->8Thread-3: Consumer:我是消费者,我正在消费MsgThread-2productor:我是大帅哥,我现在正在生产东西!,count:44--->9Thread-4: Consumer:我是消费者,我正在消费MsgThread-4productor:我是大帅哥,我现在正在生产东西!,count:46--->10Thread-5: Consumer:我是消费者,我正在消费MsgThread-5productor:我是大帅哥,我现在正在生产东西!,count:45--->11Thread-2: Consumer:我是消费者,我正在消费MsgThread-2productor:我是大帅哥,我现在正在生产东西!,count:3--->12Thread-3: Consumer:我是消费者,我正在消费MsgThread-3productor:我是大帅哥,我现在正在生产东西!,count:48--->13Thread-4: Consumer:我是消费者,我正在消费MsgThread-5productor:我是大帅哥,我现在正在生产东西!,count:50--->14Thread-5: Consumer:我是消费者,我正在消费MsgThread-2productor:我是大帅哥,我现在正在生产东西!,count:49--->15Thread-4: Consumer:我是消费者,我正在消费MsgThread-2productor:我是大帅哥,我现在正在生产东西!,count:54--->16Thread-2: Consumer:我是消费者,我正在消费MsgThread-5productor:我是大帅哥,我现在正在生产东西!,count:6--->17Thread-3: Consumer:我是消费者,我正在消费MsgThread-6productor:我是大帅哥,我现在正在生产东西!,count:52--->18Thread-5: Consumer:我是消费者,我正在消费MsgThread-3productor:我是大帅哥,我现在正在生产东西!,count:53--->19Thread-4: Consumer:我是消费者,我正在消费MsgThread-3productor:我是大帅哥,我现在正在生产东西!,count:58--->20

  

To view the results of the operation, we can do ACTIVEMQ server: http://127.0.0.1:8161/admin/inside the queues to view our production messages.

5, the characteristics of ACTIVEMQ Characteristics of 5.1 ActiveMq
    1. Multiple languages and protocols for writing clients. Languages: Java, C, C + +, C #, Ruby, Perl, Python, PHP. Application protocol: Openwire,stomp REST,WS NOTIFICATION,XMPP,AMQP
    2. Full support for JMS1.1 and the Java EE 1.4 specification (persistence, XA messages, transactions)
    3. With spring support, ACTIVEMQ can easily be embedded into a system that uses spring, and also supports Spring2.0 features
    4. Tested by common Java EE servers (such as Geronimo,jboss 4, glassfish,weblogic), where the configuration of the JCA 1.5 resource adaptors allows ACTIVEMQ to automatically deploy to any compatible Java EE 1.4 On commercial Server
    5. Supports multiple transfer protocols: In-vm,tcp,ssl,nio,udp,jgroups,jxta
    6. Support for high-speed message persistence through JDBC and journal
    7. Designed to ensure high-performance clustering, client-server, point-to-point
    8. Support Ajax
    9. Support for integration with axis
    10. It is easy to call the embedded JMS provider for testing
5.2 What is the use of ACTIVEMQ?
    1. Integration between multiple projects
      (1) Cross-platform
      (2) Multi-lingual
      (3) Multi-item
    2. Reduces coupling between modules in the system, decoupling
      (1) Software extensibility
    3. System front-end isolation
      (1) Isolation of front and rear end, shielding high safety zone

ActiveMq Learning Summary (GO) Java Message Queuing--activemq combat

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.