RabbitMQ中的HelloWorld模型

RabbitMQ中的角色

ProducerProducing means nothing more than sending. A program that sends messages is a producer :img

queueA queue is the name for a post box which lives inside RabbitMQ. Although messages flow through RabbitMQ and your applications, they can only be stored inside a queue. A queue is only bound by the host’s memory & disk limits, it’s essentially a large message buffer. Many producers can send messages that go to one queue, and many consumers can try to receive data from one queue. This is how we represent a queue:img

Consumer:Consuming* has a similar meaning to receiving. A consumer is a program that mostly waits to receive messages:img

HelloWorld模型

img

这种模型只有一个生产者,一个消费者,一个队列。生产者向"hello"队列发送消息,消费者从该队列接收消息。

1.开发生产者

public void provider() throws IOException, TimeoutException {
    ConnectionFactory connectionFactory = new ConnectionFactory();
    //设置连接rabbitmq主机地址
    connectionFactory.setHost("192.168.1.18");
    //设置连接虚拟机
    connectionFactory.setVirtualHost("/ems");
    //设置连接的用户
    connectionFactory.setUsername("ems");
    //设置连接的用户的密码
    connectionFactory.setPassword("123");
    //设置连接的端口号
    connectionFactory.setPort(5672);

    Connection connection = connectionFactory.newConnection();
    //创建通道
    Channel channel = connection.createChannel();

    //参数一:队列名称,如果不存在则自动创建
    //参数二:用来定义队列是否持久化
    //参数三:是否是独占队列
    //参数四:是否在消费完成后自动删除队列
    //参数五:额外的附加参数
    channel.queueDeclare("hello",false,false,false,null);

    //参数一:交换机名称
    //参数二:队列名称
    //参数三:传递消息额外设置
    //参数四:消息的具体内容
    channel.basicPublish("","hello",null,"hello rabbitmq".getBytes());

    channel.close();
    connection.close();

}

2.开发消费者

public static void main(String[] args) throws IOException, TimeoutException {
    ConnectionFactory connectionFactory = new ConnectionFactory();
    connectionFactory.setHost("192.168.1.18");
    connectionFactory.setVirtualHost("/ems");
    connectionFactory.setPort(5672);
    connectionFactory.setUsername("ems");
    connectionFactory.setPassword("123");

    Connection connection = connectionFactory.newConnection();
    Channel channel = connection.createChannel();
    //参数一: 队列名称
    //参数二: 是否开启自动确认
    //参数三: 消费者 DefaultConsumer建立使用,重写其中的方法
    channel.basicConsume("hello", true, new DefaultConsumer(channel) {
        @Override
        public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
            System.out.println("=====" + new String(body));
        }
    });

    //channel.close();
    //connection.close();
}

3.RabbitMQ的工具类封装

package util;

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

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

/**
 * Created with IntelliJ IDEA
 * Description:
 * Author: sudi
 * Date: 2020/12/28
 * TIME: 18:40
 */
public class RabbitMQUtils {

    private static ConnectionFactory connectionFactory;
    static {
        connectionFactory = new ConnectionFactory();
    }
    public static Connection getConnection(String Host,String VirtualHost,int Port,String Username,String Password) throws IOException, TimeoutException {
        connectionFactory.setHost(Host);
        connectionFactory.setVirtualHost(VirtualHost);
        connectionFactory.setPort(Port);
        connectionFactory.setUsername(Username);
        connectionFactory.setPassword(Password);

        return connectionFactory.newConnection();
    }

    public static void closeConnectionAndChannel(Channel channel, Connection connection) {
        try {
            if (channel!= null) channel.close();
            if(connection!= null) connection.close();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (TimeoutException e) {
            e.printStackTrace();
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值