MQ--spring jms配置+负载与断线重连

1.依赖包


  <properties>
        <springversion>5.0.6.RELEASEspringversion>
    properties>


       <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-jmsartifactId>
            <version>${springversion}version>
        dependency>

application.yml内定义mq连接信息

# ========= MQ相关配置 =============
# 需要重连或负载请在地址前前加上:failover://
#负载配置:failover:(tcp://localhost:61616,tcp://remotehost:61616)
#注意,如果加上failover(故障转移),连接失败或发送消息失败默认会触发重连机制,而如果没有配置重连次数,将会无限发起重连,并且不会抛出异常,此时如果涉及业务执行就坑了,相当于卡死在这里
链接后加入配置可以关闭重连或自定义重连次数,但依然不会抛出异常,如果对负载没有特殊要求,建议不加此前缀。具体配置可以参考:http://activemq.apache.org/failover-transport-reference.html
activemq.broker-url: tcp://127.0.0.1:61616?jms.prefetchPolicy.queuePrefetch=1 activemq.username: admin activemq.password: admin #MQ消费者连接池连接数,视情况而定 activemq.pool.max-connections : 30

applicationContext_local.xml 加入mq配置


    <bean id="pooledConnectionFactory" class="org.apache.activemq.pool.PooledConnectionFactory">
        <property name="connectionFactory">
            <bean class="org.apache.activemq.ActiveMQConnectionFactory">
                <property name="brokerURL" value="${activemq.broker-url}"/>
                <property name="userName" value="${activemq.username}">property>
                <property name="password" value="${activemq.password}">property>
            bean>
        property>
        <property name="maxConnections" value="${activemq.pool.max-connections}"/>
    bean>
    
    <bean id="jmsTemplate" class="org.springframework.jms.core.JmsMessagingTemplate">
        
        <property name="connectionFactory" ref="pooledConnectionFactory"/>
    bean>

消息发送类

import com.nascent.ecrpsaas.core.service.mq.base.BaseMQProducer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.stereotype.Component;

import javax.jms.Destination;

/**
 * ActiveMQ消息实现
 *
 */
@Component
public class ActiveMessageProducer extends BaseMQProducer {

    @Autowired
    private JmsMessagingTemplate jmsTemplate;

    // 发送消息,destination是发送到的队列,message是待发送的消息
    @Override
    public void sendMessage(Destination destination, final String message){
        jmsTemplate.convertAndSend(destination, message);
    }

    // 发送消息,destination是发送到的队列,message是待发送的消息
    @Override
    public void sendMessage(String destination, final String message){
        jmsTemplate.convertAndSend(destination, message);
    }
}

BaseMQProducer基类

import javax.jms.Destination;

/**
 * MQ基类
 */
public abstract class BaseMQProducer {

    /**
     * 发送消息
     * @param destination MQ队列
     * @param message 消息内容
     */
    protected abstract void sendMessage(Destination destination, final String message);

    /**
     * 发送消息
     * @param destination MQ队列名称
     * @param message 消息内容
     */
    public abstract void sendMessage(String destination, final String message);

}

测试类(仅为展示)

import com.alibaba.fastjson.JSONObject;
import com.nascent.ecrpsaas.core.service.mq.base.BaseMQProducer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class Test {
    @Autowired
    private static BaseMQProducer baseMQProducer;

    public static void main(String[] args) {
        JSONObject message = new JSONObject();
        message.put("say", "hello");
        baseMQProducer.sendMessage("FistQueue", message.toString());
    }
}

 

转载于:https://www.cnblogs.com/nvsky/p/11157425.html

你可能感兴趣的:(MQ--spring jms配置+负载与断线重连)