当前位置 主页 > 服务器问题 > Linux/apache问题 >

    java远程连接调用Rabbitmq的实例代码

    栏目:Linux/apache问题 时间:2019-10-26 10:18

    本文介绍了java远程连接调用Rabbitmq,分享给大家,希望此文章对各位有所帮助。

    打开IDEA创建一个maven工程(Java就可以了)。

    这里写图片描述 

    pom.xml文件如下

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
     <modelVersion>4.0.0</modelVersion>
    
     <groupId>com.zhenqi</groupId>
     <artifactId>rabbitmq-study</artifactId>
     <version>1.0-SNAPSHOT</version>
     <packaging>jar</packaging>
    
     <name>rabbitmq-study</name>
     <url>http://maven.apache.org</url>
    
     <properties>
      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
     </properties>
    
     <dependencies>
      <dependency>
       <groupId>junit</groupId>
       <artifactId>junit</artifactId>
       <version>4.12</version>
       <scope>test</scope>
      </dependency>
    
      <!-- https://mvnrepository.com/artifact/com.rabbitmq/amqp-client -->
      <dependency>
       <groupId>com.rabbitmq</groupId>
       <artifactId>amqp-client</artifactId>
       <version>4.1.0</version>
       <exclusions>
        <exclusion>
         <groupId>org.slf4j</groupId>
         <artifactId>slf4j-api</artifactId>
        </exclusion>
       </exclusions>
      </dependency>
    
      <dependency>
       <groupId>org.slf4j</groupId>
       <artifactId>slf4j-log4j12</artifactId>
       <version>1.7.21</version>
      </dependency>
    
      <dependency>
       <groupId>commons-lang</groupId>
       <artifactId>commons-lang</artifactId>
       <version>2.6</version>
      </dependency>
    
     </dependencies>
    </project>
    
    

    为了能远程访问rabbitmq,则需要编辑 /etc/rabbitmq/rabbitmq.conf,添加以下内容。

    [
      {rabbit, [{tcp_listeners, [5672]}, {loopback_users, ["asdf"]}]}
     ]

    添加administrator角色

    rabbitmqctl set_user_tags openstack administrator

    创建抽象队列 EndPoint.java

    package com.zhenqi;
    
    import com.rabbitmq.client.Channel;
    import com.rabbitmq.client.Connection;
    import com.rabbitmq.client.ConnectionFactory;
    
    
    /**
     * Created by wuming on 2017/7/16.
     */
    public abstract class EndPoint {
    
      protected Channel channel;
      protected Connection connection;
      protected String endPointName;
    
      public EndPoint(String endpointName) throws Exception {
    
        this.endPointName = endpointName;
    
        //创建一个连接工厂 connection factory
        ConnectionFactory factory = new ConnectionFactory();
    
        //设置rabbitmq-server服务IP地址
        factory.setHost("192.168.146.128");
        factory.setUsername("openstack");
        factory.setPassword("rabbitmq");
        factory.setPort(5672);
        factory.setVirtualHost("/");
    
        //得到 连接
        connection = factory.newConnection();
    
        //创建 channel实例
        channel = connection.createChannel();
    
        channel.queueDeclare(endpointName, false, false, false, null);
      }
    
      /**
       * 关闭channel和connection。并非必须,因为隐含是自动调用的。
       * @throws IOException
       */
      public void close() throws Exception{
        this.channel.close();
        this.connection.close();
      }
    }