ActiveMQ message reliability - Persistence

Three aspects to ensure reliability of the message

  1. news specific endurance of

  2. things

  3. sign

A: PERSISTENT: Persistence

    Parameter Description: 1. Persistence

         2. Non-endurance of

 

 

   Set inside the Java persistence and non-persistence

 

  Endurance of:

    The persistence is set to persist

     Before downtime, data is normal, not consumption

  

    After the server recovery , data still exists, the message is not consumed 3  

  Non-endurance of

    Setting a non-persistent

 

    Down before

     After the server is restored, messages are lost

 

 

  The above example specifies persistent and non-persistent, if not specified, the default is persistent

  These are the model for the queue (queue)

 

   Themes (Topic)

    Persistent themes are for subscribers, as producers under a subscription model to send a message out on no matter what, and if there are no subscribers, the message would be tantamount to waste news, does not make sense, so it should be in the consumer end Persistence

   experiment:

   1. First, consumers subscribe to: Figure a subscriber (online)

  Code:

package com.steak.activemq.test;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

import java.io.IOException;

public class Consumer {

private static final StringACTIVE_URL ="tcp://127.0.0.1:61616";

    private static final StringQUEUE ="topic_persist";

    public static void main(String[] args)throws JMSException, IOException {

//创建连接工厂

        ActiveMQConnectionFactory activeMQConnectionFactory =newActiveMQConnectionFactory (ACTIVE_URL); 

        // by connecting the factory to obtain connection 

        Connection Connection = activeMQConnectionFactory.createConnection (); 

        connection.setClientID ( "Liu card"); // subscribers 

        // create a session, the first thing a parameter called the second a sign called 

        the Session the session = connection.createSession ( false , Session.AUTO_ACKNOWLEDGE); 

        // create a destination 

        Topic Topic = session.createTopic (QUEUE); 

        // persistence subscribers 

        TopicSubscriber TopicSubscriber = session.createDurableSubscriber (Topic, "Use the remark" ); 

        connection.start (); 

        the Message Message= TopicSubscriber.receive (); 

        the while ( null =! Message) { 

TextMessage textMessage = (TextMessage) Message; 

            System.out.println ( "Topic message persistence" + textMessage.getText ()); 

            // if 1 second after the message is not received, automatically disconnected, corresponding to taking off 

            message = topicSubscriber.receive (1000L ); 

        } 

Session.close (); 

        Connection.close (); 

    } 

}

 

     2. Then start Producers (publishers): At this point release the three, subscribers received three

   Code:

Package com.steak.activemq.test; 

Import org.apache.activemq.ActiveMQConnectionFactory; 

Import the javax.jms *. ; 

public  class Producer { 

Private  static  Final StringACTIVE_URL = "TCP: //127.0.0.1: 61616" ; 

    Private  static  Final StringQUEUE = "topic_persist" ; 

    public  static  void main (String [] args) throws a JMSException { 

// create the connection factory 

        ActiveMQConnectionFactory activeMQConnectionFactory = new new ActiveMQConnectionFactory (ACTIVE_URL); 

        // via connection factory, connection is obtained

        Connection Connection = activeMQConnectionFactory.createConnection (); 

        // create the session 

        the Session the session = connection.createSession ( false , Session.AUTO_ACKNOWLEDGE); 

        // create a destination 

        Topic Topic = session.createTopic (QUEUE); 

        // create a message producer 

        MessageProducer MessageProducer = session.createProducer (Topic); 

            connection.start (); 

        // send a message to the production by using MessageProducer queue MQ 

        for ( int I = 0; I <. 3; I ++ ) { 

// create message 

            TextMessage textMessage= Session.createTextMessage ( "message" + I); 

            // send message via messageProducer 

            messageProducer.send (textMessage); 

        } 

// close the resource 

        messageProducer.close (); 

        Session.close (); 

        Connection.close (); 

        the System. Out.println ( "transmission completion message" ); 

    } 

}

 

  Since we set up after 1 second if not received the message on disconnected, so consumers from online goes offline

 

 

   If set to receive (), it has been the monitor (equivalent to micro-channel public number has been concerned, has been able to receive the message)

  Whether consumers are online, will receive, not online, then connect the next time, you will not receive the message are taken over (the equivalent of me to take off, can not receive the message, but I am concerned about this again, I can I also take this time off messages are received), registered a time premise, first registered before the news is certainly not receive

  

Guess you like

Origin www.cnblogs.com/steakliu/p/11590160.html