2.RabbitMQ HelloWorld

在RabbitMQ的安装中,新建的用户admin是没有权限使用默认的虚拟主机“/”的,需要新建一个虚拟主机,如图:

创建一个helloworld的虚拟主机,如图:

代码部分:

1.信道的通用工具类

package com.study.soufang.rabbit.a001;

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

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

//获取信道的通用工具类

public class RabbitChannelUtil {

    private final static String RABBIT_HOST= "192.168.10.22";
    private final static int RABBIT_PORT=5672;
    
    public static Channel createChannel() throws IOException, TimeoutException{
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost(RABBIT_HOST);
        factory.setPort(RABBIT_PORT);
        factory.setUsername("admin");
        factory.setPassword("admin");
        factory.setVirtualHost("helloworld");//刚刚创建的虚拟主机
        Connection connection = null;
        Channel channel = null;
        connection = factory.newConnection();//连接
        channel = connection.createChannel();//信道
        return channel;
    }
    /**
     * 关闭连接后不会再监听
     * @param channel
     * @throws IOException
     * @throws TimeoutException
     */
    public static void closeChannel(Channel channel) throws IOException, TimeoutException{
        Connection connection = channel.getConnection();
        if(null != channel){
            channel.close();
        }
        if(null != connection){
            connection.close();
        }
    }
    
}

2.常量类

package com.study.soufang.rabbit.a001.helloworld;

public class ConstantOfHelloWorld {

    public static final String QUEUE_NAME = "hello";
    
}

 3.消息发送

package com.study.soufang.rabbit.a001.helloworld;

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

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.study.soufang.rabbit.a001.RabbitChannelUtil;

public class Send {

    public static void main(String[] args) throws IOException, TimeoutException {
        Channel channel = null;
        try {
            //取得信道
            channel = RabbitChannelUtil.createChannel();
            /**
             * 声明消息要发送到的队列
             */
            channel.queueDeclare(ConstantOfHelloWorld.QUEUE_NAME, false, false, false, null);
            String message = "helloworld";
            /**
             * 发送消息
             */
            channel.basicPublish("", ConstantOfHelloWorld.QUEUE_NAME, null, message.getBytes());
        } catch (IOException | TimeoutException e) {
            e.printStackTrace();
        }finally{
            RabbitChannelUtil.closeChannel(channel);
        }
        
    }
}

消息成功发送到队列hello

4.消费者

package com.study.soufang.rabbit.a001.helloworld;

import java.io.IOException;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;
import com.rabbitmq.client.AMQP.BasicProperties;

public class MyConsumer extends DefaultConsumer {

    public MyConsumer(Channel channel) {
        super(channel);
    }

    @Override
    public void handleDelivery(String consumerTag, Envelope envelope, BasicProperties properties, byte[] body)
            throws IOException {
        String message = new String(body, "UTF-8");
        System.out.println(" [x] Received '" + message + "'");
    }
    
}
 

5.消息消费

package com.study.soufang.rabbit.a001.helloworld;

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

import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Consumer;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;
import com.study.soufang.rabbit.a001.RabbitChannelUtil;

public class Recv {

    public static void main(String[] args) throws IOException, TimeoutException {
        Channel channel = null;
        try {
            channel = RabbitChannelUtil.createChannel();
            channel.queueDeclare(ConstantOfHelloWorld.QUEUE_NAME, false, false, false, null);
            Consumer consumer = new MyConsumer(channel);
            /**
             * autoAck:是否自动ack,如果不自动ack,需要使用channel.ack、channel.nack、channel.basicReject 进行消息应答
             */
            String result = channel.basicConsume(ConstantOfHelloWorld.QUEUE_NAME, true, consumer);
            System.out.println(result);
        } catch (IOException | TimeoutException e) {
            e.printStackTrace();
        }finally {
            RabbitChannelUtil.closeChannel(channel);
        }
    }
    
}

 

 

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值