RabbitMQ

 

 

 Window安装RabbitMQ:

 

激活Rabbit的控制面板找到服务中的sbin目录使用cmd打开该目录然后输入命令

rabbitmq-plugins.bat enable rabbitmq_management

在浏览器中输入命令  http://localhost:15672 

 

 Linux安装RabbitMQ

 1. 下载esl-erlang_21.0-1~centos~7_amd64.rpm     Erlang运行环境RPM包

2. 下载rabbitmq-server-3.7.7-1.el7.noarch.rpm     rabbitmq服务器程序

3. mkdir /usr/local/temp

4. cd /usr/local/temp

5. 通过XFTP将文件上传至temp目录

6. rpm -ivh --nodeps esl-erlang_21.0-1~centos~7_amd64.rpm     安装RPM包

7. rpm -ivh --nodeps rabbitmq-server-3.7.7-1.el7.noarch.rpm

8. rabbitmq-plugins enable rabbitmq_management     启用控制台

9. chown rabbitmq:rabbitmq /var/lib/rabbitmq/.erlang.cookie     启用服务

10. rabbitmq-server

11. 防火墙放行5672/15672端口

 RabbitMQ的常用命令

 

rabbitmqctl add_user zhangyuexin 123456       创建用户  

rabbitmqctl delete_user zhangyuexin           删除用户

rabbitmqctl change_password guest 123         修改密码

rabbitmqctl set_user_tags zhangyuexin administrator     授予角色

rabbitmqctl set_permissions -p / zhangyuexin '.*' '.*' '.*'       ------------.*.*.*   这三个的意思分别为:1.配置权限 2.读权限 3.写权限

 

第一次MQ通信

 

 

 

 代码:

package cpm.pb.test;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class RabbitTest {
    public static void main(String[] args) throws IOException, TimeoutException {
        //创建链接rabbitmq工厂
        ConnectionFactory factory=new ConnectionFactory();
        factory.setUsername("ljj");
        factory.setPassword("123456");
        factory.setHost("192.168.200.166");
        factory.setPort(5672);
        factory.setVirtualHost("/demo");

        //创建物理连接
        Connection connection= factory.newConnection();
        //创建虚拟连接
        Channel channel= connection.createChannel();
        //通过Channel 来进行数据的传输操作
        //这里我们相mq 发送消息
        channel.queueDeclare("hello",false,false,false,null);
        String ser="发送mq";

        channel.basicPublish("","hello",null,ser.getBytes());

        System.out.println("消息发送成功");

        channel.close();

        connection.close();

    }
}

 

Consumer

package cpm.pb.test.consumer;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class Consumer {
    public static void main(String[] args) throws IOException, TimeoutException {
        ConnectionFactory factory=new ConnectionFactory();
        factory.setVirtualHost("/demo");
        factory.setPort(5672);
        factory.setHost("192.168.200.166");
        factory.setPassword("123456");
        factory.setUsername("ljj");


        Connection connection= factory.newConnection();
        Channel channel= connection.createChannel();

        channel.queueDeclare("hello",false,false,false,null);   // 第一个参数队列  第二个 是否持久化  第三个 是否私有化   第四个是否自动删除


        //开始接收消息
        //参数2 为是否自动签收 ,如果为true 表示自动如果为false表示不自动
        channel.basicConsume("hello",false,new MyConsumer(channel));
    }

}

class MyConsumer extends DefaultConsumer{
    private  Channel channel;

    public MyConsumer(Channel channel) {
        super(channel);
        this.channel=channel;
    }

    //签收

    @Override
    public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
        String msg= new String(body);
        System.out.println("接收到消息:"+msg);
        最后我么要做签收操作,目录是我们收到了消息就要把队列中的消息删除
        channel.basicAck(envelope.getDeliveryTag(), false);
    }
}

 

 封装工具类:

 

 

 工具类:(RabbitUtils )

package cpm.pb.test.utils;


import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * 封装connection
 */
public class RabbitUtils {
    //封装工厂
    private static ConnectionFactory factory;
    static  {
        factory = new ConnectionFactory();
        factory.setVirtualHost("/demo");
        factory.setPort(5672);
        factory.setHost("192.168.200.166");
        factory.setPassword("123456");
        factory.setUsername("ljj");
    }

    /**
     * 创建物理连接
     * @return
     */
    public static Connection getConnection() {
        Connection connection = null;
        try {
            connection = factory.newConnection();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (TimeoutException e) {
            e.printStackTrace();
        }
        return connection;
    }


}

 再次改造

package cpm.pb.test.consumer;

import com.rabbitmq.client.*;
import cpm.pb.test.utils.RabbitContent;
import cpm.pb.test.utils.RabbitUtils;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class Consumer {
    public static void main(String[] args) throws IOException, TimeoutException {
       /* ConnectionFactory factory=new ConnectionFactory();
        factory.setVirtualHost("/demo");
        factory.setPort(5672);
        factory.setHost("192.168.200.166");
        factory.setPassword("123456");
        factory.setUsername("ljj");*/


        Connection connection= RabbitUtils.getConnection();
        Channel channel= connection.createChannel();

        channel.queueDeclare(RabbitContent.QEUEU_HELLO,false,false,false,null);


        //开始接收消息
        //参数2 为是否自动签收 ,如果为true 表示自动如果为false表示不自动
        //channel.basicConsume(RabbitContent.QEUEU_HELLO,false,new MyConsumer(channel));
        channel.basicConsume(RabbitContent.QEUEU_HELLO,false,new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                String msg= new String(body);
                System.out.println("接收到消息:"+msg);
                最后我么要做签收操作,目录是我们收到了消息就要把队列中的消息删除
                channel.basicAck(envelope.getDeliveryTag(), false);
            }
        });
    }

}

/*class MyConsumer extends DefaultConsumer{
    private  Channel channel;

    public MyConsumer(Channel channel) {
        super(channel);
        this.channel=channel;
    }

    //签收

    @Override
    public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
        String msg= new String(body);
        System.out.println("接收到消息:"+msg);
        最后我么要做签收操作,目录是我们收到了消息就要把队列中的消息删除
        channel.basicAck(envelope.getDeliveryTag(), false);
    }
}*/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值