activemq总结--重发机制

在activemq中存在消息确认机制,即ACK机制,ACK (Acknowledgement),即确认字符,在数据通信中,接收站发给发送站的一种传输类控制字符。表示发来的数据已确认接收无误。JMS API中约定了Client端可以使用四种ACK_MODE,在javax.jms.Session接口中:
         AUTO_ACKNOWLEDGE = 1    自动确认
         CLIENT_ACKNOWLEDGE = 2    客户端手动确认   
         DUPS_OK_ACKNOWLEDGE = 3    自动批量确认
         SESSION_TRANSACTED = 0    事务提交并确认
         此外AcitveMQ补充了一个自定义的ACK_MODE:    INDIVIDUAL_ACKNOWLEDGE = 4    单条消息确认

Client端指定了ACK_MODE,但是在Client与broker在交换ACK指令的时候,还需要告知ACK_TYPE,ACK_TYPE表示此确认指令的类型,不同的ACK_TYPE将传递着消息的状态,broker可以根据不同的ACK_TYPE对消息进行不同的操作。

 比如Consumer消费消息时出现异常,就需要向broker发送ACK指令,ACK_TYPE为"REDELIVERED_ACK_TYPE",那么broker就会重新发送此消息。在JMS API中并没有定义ACT_TYPE,因为它通常是一种内部机制,并不会面向开发者。ActiveMQ中定义了如下几种ACK_TYPE(参看MessageAck类):

         DELIVERED_ACK_TYPE = 0    消息"已接收",但尚未处理结束
         STANDARD_ACK_TYPE = 2    "标准"类型,通常表示为消息"处理成功",broker端可以删除消息了
         POSION_ACK_TYPE = 1    消息"错误",通常表示"抛弃"此消息,比如消息重发多次后,都无法正确处理时,消息将会被删除或者DLQ(死信队列)
         REDELIVERED_ACK_TYPE = 3    消息需"重发",比如consumer处理消息时抛出了异常,broker稍后会重新发送此消息
         INDIVIDUAL_ACK_TYPE = 4    表示只确认"单条消息",无论在任何ACK_MODE下    
         UNMATCHED_ACK_TYPE = 5    BROKER间转发消息时,接收端"拒绝"消息
    到目前为止,我们已经清楚了大概的原理: Client端在不同的ACK_MODE时,将意味着在不同的时机发送ACK指令,每个ACK Command中会包含ACK_TYPE,那么broker端就可以根据ACK_TYPE来决定此消息的后续操作. 

  下面就简单的对activemq的重发机制做个案例说(重发机制也是基于确认机制上实现的)

一、消息生成者

package com.schooling.activemq.producer;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnectionFactory;

public class ProducerTest {

	private static final int SENDNUM = 10;
	public static void main(String[] args) throws Exception {
		ConnectionFactory connectionFactory;//连接工程
		Connection connection = null;//连接
		Session session;//会话 结束或签字发送消息的线程
		Destination destination;//消息的目的地
		MessageProducer messageProducer;//消息生产者
		try {
			
			//实例化连接工厂
			//connectionFactory = new ActiveMQConnectionFactory("admin", "admin", "failover:(tcp://115.159.89.80:51511,tcp://115.159.89.80:51512,tcp://115.159.89.80:51513)?randomize=false");
			connectionFactory = new ActiveMQConnectionFactory("admin", "admin", "tcp://115.159.89.80:61616");
			//通过连接工程获取连接
			connection = connectionFactory.createConnection();
			connection.start();//启动连接
			session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);//创建session
			destination = session.createQueue("testQueue");//创建队列
			messageProducer = session.createProducer(destination);//创建消息生产者
			
			sendMessage(session, messageProducer);
			session.commit();
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			if(connection != null)
				connection.close();
		}
		
	}
	
	public static void sendMessage(Session session,MessageProducer messageProducer) throws Exception{
		for (int i = 0; i < 5; i++) {
			TextMessage message = session.createTextMessage("ActiveMq 发送消息"+i);
			messageProducer.send(message);
		}
	}
}

二、消息消费者(这边基于Spring实现的,使用监听器)

1、spring-activemq.xml

 <!-- 将多个配置文件读取到容器中,交给Spring管理 -->
    <!-- 这里支持多种寻址方式:classpath和file -->
    <!-- 推荐使用file的方式引入,这样可以将配置和代码分离 -->
    <!--<value>file:mq.properties</value>-->
    <bean id="mqPropertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath:activemq.properties</value>
            </list>
        </property>
    </bean>

    <!-- 第三方MQ工厂: ConnectionFactory -->
    <bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
        <!-- ActiveMQ Address -->
        <property name="brokerURL" value="${activemq.brokerURL}"/>
        <property name="userName" value="${activemq.userName}"/>
        <property name="password" value="${activemq.password}"/>
        <!-- 是否异步发送 -->
        <property name="useAsyncSend" value="true"/>
        <!-- 引用重发机制 -->
        <property name="redeliveryPolicy" ref="activeMQRedeliveryPolicy" />
    </bean>

    <!--
        ActiveMQ为我们提供了一个PooledConnectionFactory,通过往里面注入一个ActiveMQConnectionFactory
        可以用来将Connection、Session和MessageProducer池化,这样可以大大的减少我们的资源消耗,要依赖于 activemq-pool包
     -->
    <bean id="pooledConnectionFactory" class="org.apache.activemq.pool.PooledConnectionFactory">
        <property name="connectionFactory" ref="targetConnectionFactory"/>
        <property name="maxConnections" value="${activemq.pool.maxConnections}"/>
    </bean>

    <!-- 定义ReDelivery(重发机制)机制 ,重发时间间隔是100毫秒,最大重发次数是3次 -->
    <bean id="activeMQRedeliveryPolicy" class="org.apache.activemq.RedeliveryPolicy">
        <!--是否在每次尝试重新发送失败后,增长这个等待时间 -->
        <property name="useExponentialBackOff" value="true"/>
        <!--重发次数,默认为6次   这里设置为1次 -->
        <property name="maximumRedeliveries" value="2"/>
        <!--重发时间间隔,默认为1秒 -->
        <property name="initialRedeliveryDelay" value="1000"/>
        <!--第一次失败后重新发送之前等待500毫秒,第二次失败再等待500 * 2毫秒,这里的2就是value -->
        <property name="backOffMultiplier" value="2"/>
        <!--最大传送延迟,只在useExponentialBackOff为true时有效(V5.5),假设首次重连间隔为10ms,倍数为2,那么第二次重连时间间隔为 20ms,
        第三次重连时间间隔为40ms,当重连时间间隔大的最大重连时间间隔时,以后每次重连时间间隔都为最大重连时间间隔。 -->
        <property name="maximumRedeliveryDelay" value="1000"/>
    </bean>

    <!-- Spring用于管理真正的ConnectionFactory的ConnectionFactory -->
    <bean id="connectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory">
        <!-- 目标ConnectionFactory对应真实的可以产生JMS Connection的ConnectionFactory -->
        <property name="targetConnectionFactory" ref="pooledConnectionFactory"/>
    </bean>

    <!--这个是目的地-->
    <bean id="msgQueue" class="org.apache.activemq.command.ActiveMQQueue">
        <constructor-arg value="${activemq.queueName}"/>
    </bean>

    <!-- Spring提供的JMS工具类,它可以进行消息发送、接收等 -->
    <!-- 队列模板 -->
    <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
        <!-- 这个connectionFactory对应的是我们定义的Spring提供的那个ConnectionFactory对象 -->
        <property name="connectionFactory" ref="connectionFactory"/>
        <property name="defaultDestinationName" value="${activemq.queueName}"/>
    </bean>

    <!-- 配置自定义监听:MessageListener -->
    <bean id="msgQueueMessageListener" class="com.schooling.activemq.consumer.MsgQueueMessageListener"/>

    <!-- 将连接工厂、目标对了、自定义监听注入jms模板 -->
    <bean id="sessionAwareListenerContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
        <property name="connectionFactory" ref="connectionFactory"/>
        <property name="destination" ref="msgQueue"/>
        <property name="messageListener" ref="msgQueueMessageListener"/>
        <!--应答模式是 INDIVIDUAL_ACKNOWLEDGE-->
        <property name="sessionAcknowledgeMode" value="4"/>
    </bean>

2、spring-context.xml

<!-- 注释配置 -->
    <context:annotation-config/>

    <!-- 扫描包起始位置 -->
    <context:component-scan base-package="com.schooling.activemq"/>

    <import resource="classpath:spring-activemq.xml"/>

.3、activemq.properties

# ActiveMQ Config
activemq.brokerURL=tcp://ip:61616
activemq.userName=admin
activemq.password=admin
activemq.pool.maxConnections=10
# queueName
activemq.queueName=testQueue

4、监听器

MsgQueueMessageListener

package com.schooling.activemq.consumer;

import org.springframework.jms.listener.SessionAwareMessageListener;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
import javax.jms.TextMessage;

/**
 * Created with IntelliJ IDEA.
 * User:Tab.
 * Date:2018/4/26.
 * <p>
 * 消费者
 */
public class MsgQueueMessageListener implements SessionAwareMessageListener<Message> {

    @Override
    public void onMessage(Message message, Session session) throws JMSException {

        if (message instanceof TextMessage) {

            String msg = ((TextMessage) message).getText();

            System.out.println("============================================================");
            System.out.println("消费者收到的消息:" + msg);
            System.out.println("============================================================");

            try {
                if ("我是队列消息002".equals(msg)) {
                    throw new RuntimeException("故意抛出的异常");
                }
                // 只要被确认后  就会出队,接受失败没有确认成功,会在原队列里面
                message.acknowledge();
            } catch (Exception e) {
                // 此不可省略 重发信息使用
                session.recover();
            }
        }
    }
}

注:这边message.acknowledge();是消费的确认,只要被确认后,消息就是出队,接受失败没有确认成功,会在原队列里面

     session.recover();是进行通知broker进行重发。

5、启动类

public class MQConsumer {
	private static final Log log = LogFactory.getLog(MQConsumer.class);

	public static void main(String[] args) {
		try {
			ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring-context.xml");
			context.start();
		} catch (Exception e) {
			log.error("==>MQ context start error:", e);
			System.exit(0);
		}
	}
}

运行结果:

============================================================
消费者收到的消息:我是队列消息000
============================================================
============================================================
消费者收到的消息:我是队列消息001
============================================================
============================================================
消费者收到的消息:我是队列消息002
============================================================
============================================================
消费者收到的消息:我是队列消息002
============================================================
============================================================
消费者收到的消息:我是队列消息002
============================================================
============================================================
消费者收到的消息:我是队列消息003
============================================================
============================================================
消费者收到的消息:我是队列消息004
============================================================
============================================================
消费者收到的消息:我是队列消息005
============================================================

猜你喜欢

转载自blog.csdn.net/qq_25011427/article/details/83958608