zoukankan      html  css  js  c++  java
  • RabbitMQ接口封装

    1.引用

    <dependency>
    <groupId>com.rabbitmq</groupId>
    <artifactId>amqp-client</artifactId>
    </dependency>

    2.代码
    package cn.piesat.task.util;

    import com.rabbitmq.client.AMQP;
    import com.rabbitmq.client.Channel;
    import com.rabbitmq.client.Connection;
    import com.rabbitmq.client.ConnectionFactory;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.stereotype.Component;

    /**
    * rabbitMQ收发消息工具类
    */
    @Component
    public class RabbitMQUtil {
    private static Logger rabbitMQUtilLogger = LoggerFactory.getLogger(RabbitMQUtil.class);

    /**
    * 发送持久化消息到指定的队列中
    *
    * @param uri 服务器连接uri参数
    * @param queueName 队列名称
    * @param msg 消息体 json 格式
    * @return true: 发送成功, false: 发送失败
    */
    public static boolean sendMsg(String uri, String queueName, String msg) {
    boolean isSuccess = false;
    Connection connection = null;
    Channel channel = null;
    try {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setUri(uri);
    connection = factory.newConnection();
    if (connection != null) {
    channel = connection.createChannel();
    if (channel != null) {
    AMQP.Queue.DeclareOk declareOk = channel.queueDeclare(queueName, true, false,
    false, null);
    if (declareOk != null) {
    // 构建消息属性
    int deliveryMode = 2; // 1:临时 2:持久化
    AMQP.BasicProperties basicProperties = new AMQP.BasicProperties(null, "text/json",
    null, deliveryMode,
    null, null,
    null, null,
    null, null,
    null, null,
    null, null);

    channel.basicPublish("", queueName, false, false, basicProperties, msg.getBytes());
    isSuccess = true;
    } else {
    rabbitMQUtilLogger.error("queue declare error");
    }
    } else {
    connection.close();
    rabbitMQUtilLogger.error("create channel error");
    }
    } else {
    rabbitMQUtilLogger.error("create connection error");
    }

    } catch (Exception e) {
    rabbitMQUtilLogger.error(e.toString());
    rabbitMQUtilLogger.error(e.getStackTrace().toString());
    isSuccess = false;
    } finally {
    try {
    if (channel != null) {
    channel.close();
    }

    if (connection != null) {
    connection.close();
    }
    } catch (Exception e) {
    rabbitMQUtilLogger.error("close connection error");
    rabbitMQUtilLogger.error(e.toString());
    rabbitMQUtilLogger.error(e.getStackTrace().toString());
    }

    return isSuccess;
    }
    }
    }
  • 相关阅读:
    SQL Azure (17) SQL Azure V12
    Microsoft Azure News(5) Azure新DV2系列虚拟机上线
    Azure Redis Cache (3) 在Windows 环境下使用Redis Benchmark
    Azure PowerShell (11) 使用自定义虚拟机镜像模板,创建Azure虚拟机并绑定公网IP(VIP)和内网IP(DIP)
    Windows Azure Virtual Machine (31) 迁移Azure虚拟机
    Windows Azure Web Site (16) Azure Web Site HTTPS
    Azure China (12) 域名备案问题
    一分钟快速入门openstack
    管理员必备的Linux系统监控工具
    Keepalived+Nginx实现高可用和双主节点负载均衡
  • 原文地址:https://www.cnblogs.com/runnerjack/p/12502787.html
Copyright © 2011-2022 走看看