| package com.matrix.component.rabbitmq; | 
|   | 
| import com.matrix.core.tools.LogUtil; | 
| import com.rabbitmq.client.Connection; | 
| import com.rabbitmq.client.ConnectionFactory; | 
|   | 
| import java.io.IOException; | 
| import java.util.concurrent.TimeoutException; | 
|   | 
| /** | 
|  * mq消息模板工程 | 
|  * | 
|  * @Author jyy | 
|  */ | 
| public class RabiitMqTemplateFactory { | 
|   | 
|     private static RabiitMqTemplate rabiitMqTemplate = null; | 
|   | 
|     /** | 
|      * 构建mq消息模板,mq模板是单例的 | 
|      * | 
|      * @param host | 
|      * @param port | 
|      * @param username | 
|      * @param password | 
|      * @return | 
|      */ | 
|     public static RabiitMqTemplate buildTemplate(String host, int port, String username, String password) { | 
|   | 
|         if (rabiitMqTemplate == null) { | 
|             ConnectionFactory factory = new ConnectionFactory(); | 
|             factory.setHost(host); | 
|             factory.setPort(port); | 
|             factory.setUsername(username); | 
|             factory.setPassword(password); | 
|             /** | 
|              * 网络故障自动连接恢复 | 
|              */ | 
|             factory.setAutomaticRecoveryEnabled(true); | 
|             Connection connection = null; | 
|             try { | 
|                 connection = factory.newConnection(); | 
|             } catch (IOException | TimeoutException e) { | 
|                 LogUtil.error("RabiitMqTemplate初始化失败" + e.getMessage(), e); | 
|             } | 
|             rabiitMqTemplate = new RabiitMqTemplate(connection); | 
|         } | 
|         return rabiitMqTemplate; | 
|     } | 
| } |