ActiveMQ & #183; basic, activemq

Source: Internet
Author: User
Tags amq

Become a fat student to learn ActiveMQ · basics, become a fat activemq

After the Spring Festival, I went back to my company and turned my company into a fat man. However, you should never think that all of his big belly is filled with fat meat, and there is a lot of extra ink in it. After all, Cheng xiaopang spent half a month in Spring Festival studying and studying ActiveMQ, hey hey ......
This is not the case. In order to test your learning achievements, Tom went to the architect's Old Wang to exchange ActiveMQ knowledge on the first day of work and asked Old Wang for a red envelope, i'm so happy to be so fat.
"Let's talk about what ActiveMQ is based on your understanding.."

"This is simple. ActiveMQ is a MOM, specifically a message proxy that implements remote communication between systems with JMS specifications. It ......"

"Wait. Let's first explain what MOM is.."

"Okay. MOM is Message-oriented middleware (Message-oriented middleware). It is a type of software used for asynchronous, loosely coupled, reliable, scalable, and secure communication in distributed applications or systems. The overall idea of MOM is that it serves as a message intermediary between the message sender and the message receiver, which provides a completely new level of loose coupling ."

"What about JMS?"

Cheng xiaopang is a person pursuing perfection. In order to make it easier to understand, he simply moved to a whiteboard and painted it.

"JMS is called Java Message Service and is a technical specification for MOM on the Java platform, it aims to simplify the development of enterprise applications by providing standard APIs for generating, sending, receiving, and processing messages, similar to the abstraction of JDBC and relational database communication methods."


"Well, good. You need to understand the following concepts.":

  • Provider: Java-only JMS interface implementation (for example, ActiveMQ)
  • Domains: message transmission methods, including P2P and Pub/Sub
  • Connection factory: the client uses the Connection factory to create a Connection with the JMS provider.
  • Destination: the objects to which messages are addressed, sent, and received.

"Let's talk about the difference between P2P and Pub/Sub.", Old Wang threw a problem to Cheng xiaopang.

Being fat is not vegetarian. After all, if it is vegetarian, he will not be so fat ...... These basic concepts are minor issues for him:

P2P (point-to-point) Message domains use queue as Destination. messages can be sent and received synchronously or asynchronously. Each message is sent only once to a Consumer.

Consumer can use MessageConsumer. receive () to receive messages synchronously, or register a MessageListener using MessageConsumer. setMessageListener () to receive messages asynchronously.

Multiple consumers can be registered on the same queue, but one message can only be received by one Consumer and then confirmed by the Consumer. In this case, the Provider sends messages to all registered consumers in polling mode.

The Pub/Sub (Publish/Subscribe, Publish/Subscribe) Message domain uses the topic as the Destination. The publisher sends messages to the topic, and the subscriber registers to receive messages from the topic. Any message sent to the topic is automatically transmitted to all subscribers. The receiving method (synchronous and asynchronous) is the same as the P2P domain.
Unless explicitly specified, the topic does not retain messages for the subscriber. Of course, this can save messages through Durable subscription. In this case, when the subscriber is disconnected from the Provider, the Provider stores messages for it. When a persistent subscriber reconnects, it will receive all messages that are not consumed during the disconnection period.

"Well, the summary is very good. The above knowledge is the theoretical basis for learning ActiveMQ and must be mastered ."

"Since JMS is a general specification,There must be a general procedure to use it to create an application.?" Asked Lao Wang.

"Yes. Do you want to talk about this general step? I will test you, haha !" Cheng xiaopang pretended to be smart and thought that Lao Wang, as an architect, would not pay attention to these specific implementation details.

However, Old Wang was personally involved in his work on weekdays. So far, he often wrote code. How can he be overwhelmed by such a small case? So Lao Wang gave the answer in minutes:

  • Get connection Factory
  • Use a connection factory to create a connection
  • Start connection
  • Create a session from a connection
  • Get Destination
  • Create a Producer, or
    • Create Producer
    • Create message
  • Create a Consumer, or send or receive a message to send or receive a message
    • Create a Consumer
    • Register a message listener (optional)
  • Send or receive message
  • Close resources (connection, session, producer, consumer, etc)

"66666, amazing! My brother Wang !" The fat little cleverness is crushed by the Old Wang!

"You have enough to talk about it. Let's do more.Now you need to write the code implementation corresponding to the above steps.", Old Wang gave Cheng xiaopang a look and made him feel more and more ......

It's not a fuel-saving Lamp, So I immediately wiped the whiteboard and rolled it up on site (it's the ghost code, and important thing to say three times ):

Public class JMSDemo {ConnectionFactory connectionFactory; Connection connection; Session session; Destination destination; MessageProducer; MessageConsumer consumer; Message message; boolean useTransaction = false; try {Context ctx = new InitialContext (); connectionFactory = (ConnectionFactory) ctx. lookup ("ConnectionFactoryName"); // when ActiveMQ is used: connectionFactory = new ActiveMQConnection Factory (user, password, getOptimizeBrokerUrl (broker); connection = connectionFactory. createConnection (); connection. start (); session = connection. createSession (useTransaction, Session. AUTO_ACKNOWLEDGE); destination = session. createQueue ("TEST. QUEUE "); // producer sends the message producer = session. createProducer (destination); message = session. createTextMessage ("this is a test"); // The consumer synchronously receives the consumer = session. creat EConsumer (destination); message = (TextMessage) consumer. receive (1000); System. out. println ("Received message:" + message); // The consumer asynchronously receives the consumer. setMessageListener (new MessageListener () {@ Override public void onMessage (Message message) {if (message! = Null) {doMessageEvent (message) ;}});} catch (JMSException e ){...} finally {producer. close (); session. close (); connection. close ();}}

Old Wang nodded with satisfaction: "It's not good ~ The general JMS specifications are all over. Next we will talk about more specific things of ActiveMQ ."

"Okay.Otherwise, I will first talk about ActiveMQ Storage Based on my own learning. You can see where I am wrong or missing.?" Cheng xiaopang played his active style. Of course, he still wanted the praise of Lao Wang.

"Okay, let's get started ."

ActiveMQ uses FIFO to store messages in queue. A Message is distributed to a single consumer at the same time, and can be deleted from the storage only when the Message is consumed and confirmed.

For persistent subscribers, each consumer obtains a copy of the Message. To save storage space, providers only store one copy of messages. Persistent subscribers maintain the pointer to the next Message and distribute its copy to the consumer. Message storage is implemented in this way, because each persistent subscriber may consume messages at different rates, or they may not all run simultaneously. In addition, because each Message may have multiple consumers, it cannot be deleted from the storage until it is successfully passed to all persistent subscribers.

"Good. The above knowledge is very important. In fact, we can use tables to display them more clearly.", Lao Wang added, and drew the following table on the whiteboard:

Message Type Persistent or not Durable subscriber? Whether the message is retained when the consumer delay starts Whether the message is retained when the Broker is restarted
Queue N - Y N
Queue Y - Y Y
Topic N N N N
Topic N Y Y N
Topic Y N N N
Topic Y Y Y Y

Although Cheng xiaopang has compared the above features, he did not want to draw a table to make the comparison clearer and easier to understand. Especially when he saw Lao Wang drawing the table at any time, he was amazed and shouted: "You're a good old man. You love me !"

After hearing this, my colleagues around me looked at it.

In this situation, Lao Wang is also embarrassed: "Hey, please be careful when talking. Don't let people think we're working on the ground.Back to the question, let's talk about the storage methods commonly used by ActiveMQ."

He nodded shyly and quickly returned to the original state.

1. KahaDB

Default storage method from ActiveMQ 5.3. KahaDB storage is a file-based fast message storage designed to be easy to use and as fast as possible. It uses a file-based message database, which means there are no prerequisites for a third-party database.

To enable the KahaDB storage, you must configure activemq. xml as follows:

<broker brokerName="broker" persistent="true" useShutdownHook="false">        <persistenceAdapter>                <kahaDB directory="${activemq.data}/kahadb" journalMaxFileLength="16mb"/>        </persistenceAdapter></broker>

2. AMQ

Like KahaDB storage, AMQ Storage enables users to quickly start and run because it does not depend on third-party databases. The AMQ message store is a combination of reliable persistence and high-performance indexing transaction logs. This storage is the best choice when message throughput is the main requirement of applications. But because it uses two separate files for each index and each Destination has an index, you should not use it when you plan to use thousands of queues in the proxy.

<persistenceAdapter>        <amqPersistenceAdapter                directory="${activemq.data}/kahadb"                syncOnWrite="true"                indexPageSize="16kb"                indexMaxBinSize="100"                maxFileLength="10mb" /></persistenceAdapter>

 3. JDBC

The reason for choosing a relational database is that enterprises already have the expertise to manage relational data, but it is definitely superior to the above message storage implementation in terms of performance. The fact is that many enterprises use relational databases as storage because they prefer to make full use of these database resources.

<beans>        <broker brokerName="test-broker" persistent="true" xmlns="http://activemq.apache.org/schema/core">                <persistenceAdapter>                        <jdbcPersistenceAdapter dataSource="#mysql-ds"/>                </persistenceAdapter>        </broker>        <bean id="mysql-ds" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">                <property name="driverClassName" value="com.mysql.jdbc.Driver"/>                <property name="url" value="jdbc:mysql://localhost/activemq?relaxAutoCommit=true"/>                <property name="username" value="activemq"/>                <property name="password" value="activemq"/>                <property name="maxActive" value="200"/>                <property name="poolPreparedStatements" value="true"/>        </bean></beans>

 4. Memory storage

In-memory message storage stores all persistent messages in the memory. Memory Message storage is useful when only a limited number of messages are stored, because messages are usually quickly consumed. Set the persistent attribute on the broker element to false in activema. xml.

<broker brokerName="test-broker" persistent="false" xmlns="http://activemq.apache.org/schema/core">        <transportConnectors>                <transportConnector uri="tcp://localhost:61635"/>        </transportConnectors></broker>

After hearing this, Old Wang smiled with a thumbs up smile: "Even the configuration can be written in such details. It seems that you have done a lot of homework and I would like to give you a thumbs up ." After all, Old Wang will not be stingy with his praise. He also understands what these compliments mean for being fat.

Cheng xiaopang received the praise of Lao Wang, and his heart also suffered from honey.

Before talking to Tom, Old Wang picked up the pen and went to the whiteboard and said,"The following describes the deployment mode of ActiveMQ based on my work experience."

1. Singleton Mode

This is not too long. Skip it.

2. No shared master-slave mode

This is the simplest high availability solution for providers. The master and slave nodes store messages respectively. The slave node needs to be configured to connect to the master node, and its status needs to be configured specially.

All message commands (message, confirmation, subscription, transaction, etc.) are copied from the master node to the slave node. This replication occurs before the master node takes effect for any commands it receives. In addition, when the master node receives a persistent message, it will wait for the processing of the message from the slave node (usually persistent to storage), and then complete the Message Processing (such as persistent to storage) by itself, then return the receipt of the Producer.

The slave node does not start any transmission or accept any client or network connection, unless the master node fails. When the master node fails, the slave node automatically becomes the master node, and the transmission is enabled and the connection is accepted. This is because the client that uses the failover transmission will connect to the new master node.

The Broker connection configuration is as follows:

failover://(tcp://masterhost:61616,tcp://slavehost:61616)?randomize=false

However, this deployment mode has some limitations,

  • The master node only copies its active status when the slave node is connected to the master node. Therefore, before the slave node is not connected to the master node, any Message or Message confirmation processed by the master node will be lost after the master node fails. However, you can avoid this by setting waitForSlave on the master node. This forces the master node to accept the connection without any slave node connection.
  • That is, the master node can only have one slave node, and other slave nodes are not allowed.
  • When you configure a running Singleton as a non-shared master-slave instance or configure a new slave node, you must stop the current service and modify the configuration before restarting the instance to take effect.

This mode can be used when some downtime is acceptable.

Slave node configuration:

<services>        <masterConnector remoteURI="tcp://remotehost:62001" userName="Rob" password="Davies"/></services>

In addition, you can configure the shutdownOnMasterFailure item, which means that the master node is safely closed after it becomes invalid, so that no message is lost. The Administrator is allowed to maintain a new slave node.

3. shared storage Master/Slave Mode

Multiple proxies are allowed to share the storage, but only one is active at any time. In this case, when the master node fails, no manual intervention is required to maintain the application integrity. Another advantage is that there is no limit on the number of slave nodes.

There are two subdivision modes:

(1) database-based

It acquires an exclusive lock on a table to ensure that no other ActiveMQ proxy can access the database at the same time. Other proxies that have not obtained the lock are in the polling status, and will be treated as slave nodes, without enabling transmission or accepting connections.


(2) file system-based

To obtain the distributed shared file lock, GFS2 is recommended in linux.

Seeing these dry goods, Cheng Xiaopeng was excited and listened to it while remembering it. After he finished speaking, he still did not finish recording it. Old Wang took the opportunity to drink a cup of Tieguanyin runyun.

After recording what Old Wang said about the deployment model, he was embarrassed to let Old Wang continue to talk about it. After all, he knew that old Wang had to work overtime and stand up for a long time.

"You sit down and have a rest,I will tell you more about the ActiveMQ network connection that I understand.?"

. It's okay. I'm in good health ~" Old Wang knows that Cheng xiaopang is worried about his waist, but he still has a bad temper. Cheng xiaopang did not dare to delay the time, and started the lecture immediately.

1. Proxy Network

ActiveMQ message proxy can be linked to different topologies, which is a well-known proxy network.

ActiveMQ networks use the concept of storage and forwarding, where messages are always stored in the local proxy and then forwarded to another Proxy through the network.


After a connection is established, the remote proxy will pass information containing all its persistent and active consumer destinations to the local proxy, the local proxy determines the Message that the remote proxy is interested in based on the information and sends it to the remote proxy.

If you want the network to be bidirectional, you can use the network connector to direct the remote proxy to a local proxy, or configure the network connector to duplex to send messages in two directions.

<networkConnectors>        <networkConnector uri="static://(tcp://backoffice:61617)"                              name="bridge"                              duplex="true"                              conduitSubscriptions="true"                              decreaseNetworkConsumerPriority="false">        </networkConnector></networkConnectors>

Note that the configuration sequence is important:

1. Network Connection-a connection needs to be established before message storage, corresponding to the networkConnectors Element
2. Message storage-it must be configured before transmission, corresponding to the persistenceAdapter Element
3. message transmission-the final configuration, corresponding to the transportors Element

2. network discovery

(1) Dynamic discovery

Use multicast to support dynamic network discovery. The configuration is as follows:

<networkConnectors>    <networkConnector uri="multicast://default"/></networkConnectors>

The default name in multicast: // indicates the group to which the proxy belongs. Therefore, we strongly recommend that you use a unique group name to prevent your proxy from connecting to other unrelated proxies.

(2) Static discovery

Static discovery accepts the proxy URI list and connects the list to the remote proxy in the order specified in the list.

<networkConnectors>    <networkConnector uri="static:(tcp://remote-master:61617,tcp://remote-slave:61617)"/></networkConnectors>

The configuration is as follows:

  • InitialReconnectDelay: The default value is 1000, indicating the latency before the connection attempt.
  • MaxReconnectDelay: The default value is 30000, indicating the delay between the connection and the connection establishment. This parameter takes effect only when useExponentialBackOff is enabled.
  • UseExponentialBackOff: The default value is true. If it is enabled, the delay of re-connection is increased after each failure.
  • BackOffMultiplier: The default value is 2, indicating that after useExponentialBackOff is enabled, you must note that the network connection will always try to establish a connection to the remote proxy.

Note that the network connection will always try to establish a connection to the remote proxy.

(3) Multi-connection scenarios


When the network load is high, it makes sense to use multiple connections. However, you need to ensure that messages are not repeatedly transmitted, which can be achieved through filters.

<networkConnectors>    <networkConnector uri="static://(tcp://remotehost:61617)"                              name="queues_only"                              duplex="true"        <excludedDestinations>            <topic physicalName=">"/>        </excludedDestinations>    </networkConnector>    <networkConnector uri="static://(tcp://remotehost:61617)"                              name="topics_only"                              duplex="true"        <excludedDestinations>            <queue physicalName=">"/>        </excludedDestinations>    </networkConnector></networkConnectors>

After the lecture, he was relieved by the feeling that although these knowledge points seem to be few, he spent a lot of time reading a lot of English documents and understanding them through repeated practices.

During the time when he was talking about Cheng xiaopang, Lao Wang has been sitting in a swivel chair, and his waist is quite comfortable now. Old Wang stood up and patted him on a fat shoulder: "Although this knowledge point is a bit obscure, you have a good explanation. Through today's communication, we can see that you have a good grasp of the basic knowledge of ActiveMQ. In the future, we still need to do more in-depth practices so that we can use it to provide high-quality services ."

Old Wang's cell phone suddenly rang, and he was going to a meeting: "Let's be here today. I have a meeting to attend ."

"Okay, thank you for your patience and guidance. I hope you will pay more attention to your health ."

Old Wang smiled and said, "Well, don't worry. There is a young man next door who is just getting married. I heard them say they want a baby recently, so I will definitely maintain my health ."

Cheng xiaopang: "..."

"Haha, joke !"

"……"

 

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.