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:61616
Thread-2: Consumer:我是消费者,我正在消费MsgThread-5productor:我是大帅哥,我现在正在生产东西!,count:4--->0
Thread-3: Consumer:我是消费者,我正在消费MsgThread-4productor:我是大帅哥,我现在正在生产东西!,count:36--->1
Thread-4: Consumer:我是消费者,我正在消费MsgThread-3productor:我是大帅哥,我现在正在生产东西!,count:38--->2
Thread-5: Consumer:我是消费者,我正在消费MsgThread-6productor:我是大帅哥,我现在正在生产东西!,count:37--->3
Thread-2: Consumer:我是消费者,我正在消费MsgThread-6productor:我是大帅哥,我现在正在生产东西!,count:2--->4
Thread-3: Consumer:我是消费者,我正在消费MsgThread-5productor:我是大帅哥,我现在正在生产东西!,count:40--->5
Thread-4: Consumer:我是消费者,我正在消费MsgThread-6productor:我是大帅哥,我现在正在生产东西!,count:42--->6
Thread-5: Consumer:我是消费者,我正在消费MsgThread-4productor:我是大帅哥,我现在正在生产东西!,count:41--->7
Thread-2: Consumer:我是消费者,我正在消费MsgThread-3productor:我是大帅哥,我现在正在生产东西!,count:1--->8
Thread-3: Consumer:我是消费者,我正在消费MsgThread-2productor:我是大帅哥,我现在正在生产东西!,count:44--->9
Thread-4: Consumer:我是消费者,我正在消费MsgThread-4productor:我是大帅哥,我现在正在生产东西!,count:46--->10
Thread-5: Consumer:我是消费者,我正在消费MsgThread-5productor:我是大帅哥,我现在正在生产东西!,count:45--->11
Thread-2: Consumer:我是消费者,我正在消费MsgThread-2productor:我是大帅哥,我现在正在生产东西!,count:3--->12
Thread-3: Consumer:我是消费者,我正在消费MsgThread-3productor:我是大帅哥,我现在正在生产东西!,count:48--->13
Thread-4: Consumer:我是消费者,我正在消费MsgThread-5productor:我是大帅哥,我现在正在生产东西!,count:50--->14
Thread-5: Consumer:我是消费者,我正在消费MsgThread-2productor:我是大帅哥,我现在正在生产东西!,count:49--->15
Thread-4: Consumer:我是消费者,我正在消费MsgThread-2productor:我是大帅哥,我现在正在生产东西!,count:54--->16
Thread-2: Consumer:我是消费者,我正在消费MsgThread-5productor:我是大帅哥,我现在正在生产东西!,count:6--->17
Thread-3: Consumer:我是消费者,我正在消费MsgThread-6productor:我是大帅哥,我现在正在生产东西!,count:52--->18
Thread-5: Consumer:我是消费者,我正在消费MsgThread-3productor:我是大帅哥,我现在正在生产东西!,count:53--->19
Thread-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
- 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
- Full support for JMS1.1 and the Java EE 1.4 specification (persistence, XA messages, transactions)
- With spring support, ACTIVEMQ can easily be embedded into a system that uses spring, and also supports Spring2.0 features
- 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
- Supports multiple transfer protocols: In-vm,tcp,ssl,nio,udp,jgroups,jxta
- Support for high-speed message persistence through JDBC and journal
- Designed to ensure high-performance clustering, client-server, point-to-point
- Support Ajax
- Support for integration with axis
- It is easy to call the embedded JMS provider for testing
5.2 What is the use of ACTIVEMQ?
- Integration between multiple projects
(1) Cross-platform
(2) Multi-lingual
(3) Multi-item
- Reduces coupling between modules in the system, decoupling
(1) Software extensibility
- System front-end isolation
(1) Isolation of front and rear end, shielding high safety zone
ActiveMq Learning Summary (GO) Java Message Queuing--activemq combat