Java Message Queuing--ACTIVEMQ combat

Source: Internet
Author: User
Tags throwable tomcat server

1. Download and install ACTIVEMQ

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

ActiveMQ provides several versions of Windows and Linux, Unix, and so on, where the landlord chose to develop under the Windows version.

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

Go to the Bin directory of the ACTIVEMQ installation directory, select Win32/win64 according to the system, then click Activemq.bat

After the click, the ACTIVEMQ has started, this time the service has been successfully started, you can also see the access path

  

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.

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 classSender {

private static final intSend_number = 5;

private static final Stringqueue_name = "Xwj_queue";

Publicstatic 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://localhost:61616");
try {
Construction gets the connection object from the factory
Connection = Connectionfactory.createconnection ();
Start
Connection.start ();
Get Operation Connection
Session = Connection.createsession (Boolean.true, Session.auto_acknowledge);
Get session Note parameter value Queue_name is a server queue that must be configured in the ACTIVEMQ console
Destination = Session.createqueue (queue_name);
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);
Session.commit ();
} catch (Exception e) {
E.printstacktrace ();
} finally {
try {
if (null! = connection)
Connection.close ();
} catch (Throwable ignore) {
}
}
}

Publicstatic void SendMessage (Session session, MessageProducer producer) throws Exception {
for (int i = 1; I <= send_number; i++) {
TextMessage message = Session.createtextmessage ("XWJ sends messages:" + i);
Send a message to the destination party
SYSTEM.OUT.PRINTLN ("Send message:" + "ActiveMq message sent" + i);
Producer.send (message);
}
}

3.2 Creating a consumer
Public classReceiver {

private static final Stringqueue_name = "Xwj_queue";

Publicstatic 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://localhost:61616");
try {
Construction gets the connection object from the factory
Connection = Connectionfactory.createconnection ();
Start
Connection.start ();
Get Operation Connection
Session = 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 console
Destination = Session.createqueue (queue_name);
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 (10000);
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) {
}
}
}

4. Run ACTIVEMQ Project

4.1 Producers start producing messages

Executes the main method in sender, running the result:

Send message: Message sent by ActiveMq 1
Send message: Message sent by ActiveMq 2
Send message: Message sent by ActiveMq 3
Send message: Message sent by ActiveMq 4
Send message: Message sent by ActiveMq 5

  

4.2 Consumers start consuming messages

Execute the main method in receiver and run the result:

 < Span style= "COLOR: #000000" > < Span style= "COLOR: #000000" > < Span style= "COLOR: #000000" > send message: ActiveMq sent message 1 
Send message: ACTIVEMQ Send message 2
Send message: ACTIVEMQ Send message 3
Send message: ActiveMq Message 4
Send message: Message sent by ActiveMq 5

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. Characteristics of ActiveMq 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

Java Message Queuing--ACTIVEMQ combat

Related Article

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.