ActiveMQ message receipt mechanism

 Consumers client successfully receives a message signs are: This message is to sign.
         Consumers client successfully receiving a message generally includes three stages:
         1, the consumer receives a message, i.e. returned from the receive method MessageConsumer
         2, consumer handling message
         3, the message sign is
        Among them, the third phase of the sign may have ActiveMQ launch, consumers can also be initiated by the client, depending on whether the Session open transaction and receipt setting mode.
        In Session with a transaction, the filing of a consumer client-side transaction, the message automatically sign.
        In Session without a transaction, the message sign is depending on when and how the sign Session mode setting
       Non-Session transaction receipt can be provided the following several modes:
      
1.Session.AUTO_ACKNOWLEDGE
When a message is returned from the method MessageConsumer receive or return from the onMessage method MessageListener interface, the session automatically acknowledges the message receipt
2.Session.CLIENT_ACKNOWLEDGE
requires consumers to take the initiative to call a client method acknowledge receipt of the message, this model is really a sign carried Session level, the consumer has to sign a message will automatically sign all messages that have been consumed Session:
   for example a consumer spending of five messages in a Session, and then confirm that Article 3 messages, all messages will be 5 to sign
3.Session .DUPS_OK_ACKNOWLEDGE
This approach allows no rush JMS message received acknowledgment, to allow completion confirmation after receiving a plurality of messages, as compared with AUTO_ACKNOWLEDGE, confirmed this manner may be more effective in some cases, because there is no confirmation, when the system crashes or when the network fails, the message may be redelivered.
this approach will lead to duplicate message, but reduces the overhead of Session, so that only the client can tolerate a duplicate message before use. (If ActiveMQ transmitting the same message again, the message header JMSRedelivered is set to true)

Case with the transaction session

  Producers

    Must be submitted manually after the session finished production data

 
com.wn.ddd Package; 

Import org.apache.activemq.ActiveMQConnection; 
Import org.apache.activemq.ActiveMQConnectionFactory; 
Import the javax.jms *;. 

public class Producter { 
    public static void main (String [] args) {throws a JMSException 
        / / ConnectionFactory: connection factories, JMS use it to create a connection 
        ConnectionFactory connectionFactory = new new ActiveMQConnectionFactory (ActiveMQConnection.DEFAULT_USER, 
                ActiveMQConnection.DEFAULT_PASSWORD, "tcp: //127.0.0.1: 61616"); 
        // client-to-JMS Provider JMS connection 
        connection connection connectionFactory.createConnection = (); 
        // start connection 
        connection.start (); 
        // Session: a send or receive messages thread false: Representative without a transaction session AUTO_ACKNOWLEDGE: Representative automatic sign
        = Connection.createSession session the Session (Boolean.TRUE, Session.AUTO_ACKNOWLEDGE); 
        // Destination:. Destination of the message; the message is sent to whom 
        // Get the session Note parameter value my-queue Query is the name of 
        Queue queue = session.createQueue ( "My-Queue"); 
        // MessageProducer: create a message producer 
        MessageProducer producer = session.createProducer (Queue); 
        // settings do not persist pERSISTENT: represents the persistence NON_PERSISTENT: not representative of persistent 
        producer.setDeliveryMode (DeliveryMode.PERSISTENT ); 
        // send a message 
        for (int. 1 = I; I <=. 5; I ++) { 
            SENDMSG (the session, Producer, I); 
        }  
        System.out.println ( "successfully sent!");
        Session.commit (); 
        Session.close (); 
        Connection.close (); 
    }
    / ** 
     * on the specified session, a message is sent through the designated message producer 
     * 
     * @param the session 
     * Message Session 
     * @param Producer 
     * Manufacturer message 
     * / 
    public static void SENDMSG (the Session the session, the MessageProducer Producer, I int ) throws a JMSException { 
        // create a text message 
        TextMessage = session.createTextMessage message ( "the ActiveMQ the Hello!" + I); 
        // message sent by the message producer 
        producer.send (message); 
    } 
}
 

  consumer

    After completion of the consumption data submitted must be manually session

 
com.wn.ddd Package; 

Import org.apache.activemq.ActiveMQConnection; 
Import org.apache.activemq.ActiveMQConnectionFactory; 

Import the javax.jms *;. 

public class JMSReceiver { 
    public static void main (String [] args) {throws a JMSException 
        / / ConnectionFactory: connection factories, JMS use it to create a connection 
        ConnectionFactory connectionFactory = new new ActiveMQConnectionFactory (ActiveMQConnection.DEFAULT_USER, 
                ActiveMQConnection.DEFAULT_PASSWORD, "tcp: //127.0.0.1: 61616"); 
        // client-to-JMS Provider JMS connection 
        connection connection connectionFactory.createConnection = (); 
        connection.start (); 
        // the Session: a send or receive messages thread true: open transaction form AUTO_ACKNOWLEDGE: Representative automatic sign
        = Connection.createSession session the Session (of Boolean.TRUE, Session.AUTO_ACKNOWLEDGE); 
        // the Destination: destination of the message; message to whom. 
        // Get session parameter values noted xingbo.xu-queue is a queue server, shall be in ActiveMq console configurations 
        Queue Queue = session.createQueue ( "My-Queue"); 
        // consumer, the message recipient 
        a MessageConsumer consumer = session.createConsumer (Queue); 
        the while (to true) { 
            // the receive (): Get message 
            message = TextMessage (TextMessage) consumer.receive (); 
            IF (null = message!) { 
                System.out.println ( "receive the message:" + message.getText ()); 
                Session.commit (); 
            } the else { 
                BREAK ; 
            }
        }
        // resource recovery 
        Session.close (); 
        Connection.close (); 
    } 
}
 

Case without a transaction session of

  1. Automatic sign

    

  2. Manual sign

    Producers

 
com.wn.ddd Package; 

Import org.apache.activemq.ActiveMQConnection; 
Import org.apache.activemq.ActiveMQConnectionFactory; 
Import the javax.jms *;. 

public class Producter { 
    public static void main (String [] args) {throws a JMSException 
        / / ConnectionFactory: connection factories, JMS use it to create a connection 
        ConnectionFactory connectionFactory = new new ActiveMQConnectionFactory (ActiveMQConnection.DEFAULT_USER, 
                ActiveMQConnection.DEFAULT_PASSWORD, "tcp: //127.0.0.1: 61616"); 
        // client-to-JMS Provider JMS connection 
        connection connection connectionFactory.createConnection = (); 
        // start connection
        connection.start (); 
        // Session: a send or receive messages thread false: Representative without a transaction session AUTO_ACKNOWLEDGE: Representative automatic sign
       / * The Session = connection.createSession the session (of Boolean.TRUE, Session.AUTO_ACKNOWLEDGE); * / 
        the Session = connection.createSession the session (Boolean.FALSE The, Session.CLIENT_ACKNOWLEDGE); 
        // the Destination: destination of the message; message to whom. 
        // get the session Note parameter value my-queue Query is the name of 
        Queue Queue = session.createQueue ( "my-queue"); 
        // MessageProducer: create a message producer 
        MessageProducer producer = session.createProducer (Queue); 
        // not set persistence pERSISTENT: Representative persistence NON_PERSISTENT: Representative not persistent 
        producer.setDeliveryMode (DeliveryMode.PERSISTENT); 
        // send message  
        for (int i = 1; i <= 5; i ++) {
            SENDMSG (the session, Producer, I); 
        } 
        System.out.println ( "sent successfully!"); 
        the session .close ();
        Connection.close (); 
    } 
    / ** 
     * on the specified session, a message is sent through the designated message producer 
     * 
     * @param the session 
     * Message Session 
     * @param Producer 
     * Manufacturer message 
     * / 
    public static void SENDMSG (the Session the session, the MessageProducer producer, I int) throws a JMSException { 
        // create a text message 
        TextMessage = session.createTextMessage message ( "the ActiveMQ the Hello!" + I); 
        // message sent by the message producer 
        producer.send (message);
     message.acknowledge (); // manual submission
  } 
}
 

    consumer

 
com.wn.ddd Package; 

Import org.apache.activemq.ActiveMQConnection; 
Import org.apache.activemq.ActiveMQConnectionFactory; 
Import sun.plugin2.os.windows.SECURITY_ATTRIBUTES; 

Import the javax.jms *;. 

public class JMSReceiver { 
    public static void main (String [] args) throws a JMSException { 
        // the ConnectionFactory: connection factory, JMS connection with it to create 
        the ConnectionFactory The connectionFactory = new new ActiveMQConnectionFactory (ActiveMQConnection.DEFAULT_USER, 
                ActiveMQConnection.DEFAULT_PASSWORD, "TCP: //127.0.0.1: 61616"); 
        / / JMS client JMS Provider connection 
        connection connection = connectionFactory.createConnection ();
        connection.start();
        // Session: a send or receive messages thread true: Open Transaction form AUTO_ACKNOWLEDGE: Representative Auto sign 
        / * = connection.createSession the session the Session (of Boolean.TRUE, Session.AUTO_ACKNOWLEDGE); * / 
        the Session = connection.createSession the session (Boolean. FALSE, Session.CLIENT_ACKNOWLEDGE); 
        // the destination:. destination of the message; the message is sent to whom 
        // get the session parameters noted xingbo.xu-queue is a queue server, to be disposed in a console of ActiveMq 
        Queue queue = session .createQueue ( "My-Queue"); 
        // consumer, the message recipient 
        a MessageConsumer consumer = session.createConsumer (Queue); 
        the while (to true) { 
            // the receive (): Get message 
            TextMessage message = (TextMessage) consumer.receive (); 
            IF (! = null Message) {
                System.out.println ( "receive the message:" + message.getText ());
                message.acknowledge (); // manual submission 
            } the else { 
                BREAK; 
            } 
        } 
        // resource recovery 
        Session.close (); 
        Connection.close (); 
    } 
}

 

Guess you like

Origin www.cnblogs.com/lowerma/p/12317044.html