ACTIVEMQ (6)

Source: Internet
Author: User
Tags prefetch

2. 6 Features
ACTIVEMQ contains a number of powerful features, and a few of them are briefly described below.
2. 6. 1 Exclusive Consumer
Messages in the queue are distributed to consumers in order. However, when you have multiple consumers to extract messages from the same queue at the same time, you will lose this assurance. Because these messages are processed concurrently by multiple threads. Sometimes it is important to ensure that messages are processed in order. For example, you may not want to perform an update of this order before the end of the insert order operation.
ACTIVEMQ supports exclusive Consumer (or exclusive queues) starting from the 4.x version. Broker selects a consumer from multiple consumers to handle all the messages in the queue, thus ensuring the orderly processing of the messages. If this consumer fails, then broker will automatically switch to the other consumer.
You can create a exclusive Consumer by Destination Options, as follows: Java code queue = new activemqqueue ("TEST".    Queue?consumer.exclusive=true "); Consumer = session.createconsumer (queue);

Queue = new Activemqqueue ("TEST". Queue?consumer.exclusive=true ");
Consumer = session.createconsumer (queue);
By the way, you can set priorities for consumer to optimize for network conditions such as network hops, as follows: Java code queue = NewActivemqqueue ("TEST.") Queue?consumer.exclusive=true &consumer.priority=10 ");
Queue = new Activemqqueue ("TEST". Queue?consumer.exclusive=true &consumer.priority=10 ");

2. 6. 2 Message Groups
In the official Apache document, message Groups Rock. It is the enhancement of exclusive consumer function. Logically, message Groups can be viewed as a concurrent exclusive Consumer. Unlike all messages that are handled by unique consumer, the JMS message properties Jmsxgroupid are used to differentiate message group. The message Groups feature guarantees that all messages with the same jmsxgroupid are distributed to the same consumer (as long as the consumer remains active). On the other hand, the message Groups feature is also a load balancing mechanism.
Before a message is distributed to consumer, broker checks the message Jmsxgroupid property first. If it exists, then broker checks to see if any consumer owns the message group. If not, then broker selects a consumer and associates it with this message group. Thereafter, this consumer will receive all messages from this message group until: consumer is closed. Message group is closed. By sending a message and setting the JMSXGROUPSEQ for this message to 0.

Starting with version 4.1, ACTIVEMQ supports a Boolean field Jmsxgroupfirstforconsumer. This field is set when the first message of a messages group is sent to consumer. If the customer uses failover transport to connect to broker. When customers reconnect to broker due to network problems, messages from the same message group may be distributed to different consumer, so the Jmsxgroupfirstforconsumer field is reset.

The following is an example of using message groups: Java code MESASGE message = Session.createtextmessage ("<foo>hey</foo>");    Message.setstringproperty ("Jmsxgroupid", "ibm_nasdaq_20/4/05"); ... producer.send (message);

Mesasge message = Session.createtextmessage ("<foo>hey</foo>");
Message.setstringproperty ("Jmsxgroupid", "ibm_nasdaq_20/4/05");
...
Producer.send (message);

2. 6. 3 JMS Selectors
The JMS selectors is used in subscriptions to filter messages based on message properties. The JMS selectors is defined by the SQL92 syntax. Here's an example of a selectors:
Java Code consumer = Session.createconsumer (destination, "Jmstype = ' car ' and weight > 2500");
Consumer = Session.createconsumer (destination, "Jmstype = ' car ' and weight > 2500");
In a JMS selectors expression, you can use in, don't in, like, and so on, for example:
Like ' 12%3 ' (' 123 ' true, ' 12993 ' true, ' 1234 ' false)
Like ' L_se ' (' Lose ' true, ' loose ' false)
Like '/_% ' ESCAPE '/' (' _foo ' true, ' Foo ' false)
It should be noted that the date and time in the JMS selectors expression need to use a standard long millisecond value. Properties in an expression are not automatically type-converted, for example:
Java code mymessage.setstringproperty ("Numberoforders", "2");
Mymessage.setstringproperty ("Numberoforders", "2");
"Numberoforders > 1" evaluates to False. For more information about JMS selectors, refer to Javax.jms.Message's Javadoc.
The message groups, described in the previous section, guarantees that messages with the same messaging group are processed in a unique consumer order, but it is not possible to determine which consumer is being processed. In some cases, message groups can work with the JMS selector, for example:
It is assumed that three consumers are a, B and C respectively. You can set three message groups for messages in producer, "A", "B" and "C" respectively. Then consumer a uses "Jmxgroupid = ' a '" as selector. B and C are the same. This ensures that message Group A messages are handled only by consumer a. It is to be noted that this practice has the following drawbacks:
Producer must know the consumers that is currently running, that is, producer and consumer are coupled together. If a consumer fails, the message that should be consumed by this consumer will be kept in the backlog of broker.

2. 6. 4 Pending message Limit strategy
    first briefly introduce the prefetch mechanism. Activemq through the prefetch mechanism to improve performance, which means that a certain number of messages may be cached in the client's memory. The number of cached messages is controlled by prefetch limit. When a consumer prefetch buffer has reached the upper limit, then broker does not distribute the message to consumer until consumer sends a confirmation of the message to broker. You can configure prefetch policy by setting the Activemqprefetchpolicy object on Activemqconnectionfactory or activemqconnection. Can also be configured by connection options or destination options. For example:
    tcp://localhost:61616?jms.prefetchpolicy.all=50
    tcp://localhost : 61616?jms.prefetchpolicy.queueprefetch=1
    queue = new Activemqqueue ("TEST". Queue?consumer.prefetchsize=10 "); The default values for the
    prefetch size are as follows: Persistent queues (default value:1000) non-persistent queues (default value: 1000) Persistent topics (default value:100) non-persistent topics (default value:short.max_value-1)

Slow consumers cause problems on non-persistent topics: Once the message backlog, it causes broker to keep a large number of messages in memory, and broker slows down. Future ACTIVEMQ may implement disk caching, but there are still performance issues. Currently ACTIVEMQ uses pending message Limit strategy to solve this problem. In addition to prefetch buffer, you have to configure the upper limit of the cached message, which, when the new message arrives, discards the old message. By configuring Pendingmessagelimitstrategy in the destination map of the configuration file, you can configure different policies for the topic namespace that are not available. There are currently two kinds of: Constantpendingmessagelimitstrategy. This policy uses constant limits.
For example: <constantpendingmessagelimitstrategy limit= "a"/> prefetchratependingmessagelimitstrategy. This strategy uses a multiple limit of prefetch size.
For example: <prefetchratependingmessagelimitstrategy multiplier= "2.5"/>

In both of these ways, setting 0 means that the message is no longer cached except for the prefetch, and setting-1 means that messages are not discarded.
In addition, you can configure the message discard policy, currently has the following two kinds: oldestmessageevictionstrategy. This policy discards the oldest message. Oldestmessagewithlowestpriorityevictionstrategy. This policy discards the oldest and lowest-priority messages.

The following is an example of a ACTIVEMQ configuration file: XML code<brokerPersistent= "false" Brokername= "${brokername}" xmlns= "http://activemq.org/config/1.0"><destinationPolicy><policyMap><policyEntries><policyentryTopic= "Prices.>"><!--seconds worth--><subscriptionRecoveryPolicy><timedsubscriptionrecoverypolicyRecoverduration= "10000"/></subscriptionRecoveryPolicy><!--lets force old messages to is discarded for slow consumers<pendingMessageLimitStrategy> <constantpendingmessagelimitstrategylimit= "10"/></pendingMessageLimitStrategy></policyEntry></policyEntries></policyMap></destinationPolicy>...</broker>

<broker persistent= "false" Brokername= "${brokername}" xmlns= "http://activemq.org/config/1.0" >
    < destinationpolicy>
      <policyMap>
        <policyEntries>
          <policyentry topic= "prices.>" >
            <!--  seconds worth-->
            <subscriptionRecoveryPolicy>
              < Timedsubscriptionrecoverypolicy recoverduration= "10000"/>
            </subscriptionRecoveryPolicy>
            
            <!- -Lets force messages to is discarded for slow consumers-->
            <pendingMessageLimitStrategy>
              <con Stantpendingmessagelimitstrategy limit= "Ten"/>
            </pendingMessageLimitStrategy>
          </ policyentry>
        </policyEntries>
      </policyMap>
    </destinationPolicy>
    ...
</broker>

2. 6. 5 Composite Destinations
From version 1.1, ACTIVEMQ supports composite destinations. It allows you to represent multiple destinations with a virtual destination. For example, you can send messages to 12 queue in one operation via composite destinations. In composite destinations, a "," partition is used between multiple destination. For example: Java code queue queue = new activemqqueue ("FOO. A,foo. B,foo. C ");

Queue queue = new Activemqqueue ("FOO. A,foo. B,foo. C ");

If you want to use different types of destination, you need to prefix such as queue://or topic://, for example: Java code queue queue = new activemqqueue ("FOO. A,topic://notify. Foo. A ");

The following is an example of configuring a ACTIVEMQ configuration file: XML code<destinationInterceptors><virtualDestinationInterceptor> <virtualDestinations><compositequeueName= "My. QUEUE "><forwardTo><queuePhysicalname= "FOO"/><topicPhysicalname= "BAR"/></forwardTo> </compositeQueue></virtualDestinations></virtualDestinationInterceptor></destinationInterceptors>

<destinationInterceptors>
  <virtualDestinationInterceptor>
    <virtualDestinations>
      <compositequeue name= "My. QUEUE ">
        <forwardTo>
          <queue physicalname=" FOO "/>
          <topic physicalname=" BAR "/>
        </forwardTo>
      </compositeQueue>
    </virtualDestinations>
  </ Virtualdestinationinterceptor>
</destinationInterceptors>

You can use the JMS selector to determine whether a message needs to be forwarded before forwarding, for example: XML code<destinationInterceptors><virtualDestinationInterceptor><virtualDestinations><compositequeueName= "My. QUEUE "><forwardTo><filtereddestinationselector= "odd = ' yes '" queue= "FOO"/><filtereddestinationselector= "i = 5" topic= "BAR"/></forwardTo></compositeQueue></virtualDestinations></virtualDestinationInterceptor></destinationInterceptors>

<destinationInterceptors>
  <virtualDestinationInterceptor>
    <virtualDestinations>
      <compositequeue name= "My. QUEUE ">
        <forwardTo>
          <filtereddestination selector=" odd = ' yes ' "queue=" FOO "/>
          < Filtereddestination selector= "i = 5" topic= "BAR"/>
        </forwardTo>
      </compositeQueue>
    </ virtualdestinations>
  </virtualDestinationInterceptor>
</destinationInterceptors>

2. 6. 6 Mirrored queues
Messages in each queue can only be consumed by one consumer. However, sometimes you may want to be able to monitor the flow of information between producers and consumers. You can forward messages to multiple queues by using virtual destinations to create a virtual queue. However, it can be cumbersome to make such a configuration for each queue in the system.
ACTIVEMQ supports mirrored queues. Broker forwards all messages sent to a queue to a topic with a similar name, so the monitor can subscribe to this mirrored queue topic. To enable mirrored queues, you first set the Usemirroredqueues property of Brokerservice to True, and then you can set other properties by destinationinterceptors, such as mirror Topic prefix, the default is "Virtualtopic.mirror." The following is an example of a ACTIVEMQ configuration file: XML code<brokerxmlns= "http://activemq.org/config/1.0" brokername= "MirroredQueuesBroker1" usemirroredqueues= "true"><transportConnectors><transportconnectorUri= "tcp://localhost:61616"/></transportConnectors> <destinationInterceptors><mirroredqueueCopymessage = "true" prefix= "Mirror.topic"/></destinationInterceptors>...</broker>

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.