在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);
}
}
}