代码地址:https://github.com/heidaodageshiwo/ZqRabbitMq
1.添加虚拟机(/ems) 与用户 用户名:ems 密码:123
pom.xml
<dependency>
<groupId>com.rabbitmq</groupId>
<artifactId>amqp-client</artifactId>
<version>5.7.2</version>
</dependency>
工具类:
package org.example;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
/**
* java类简单作用描述
*
* @ProjectName: rabbitmq
* @Package: org.example
* @ClassName: RabbitMQUtils
* @Description: java类作用描述
* @Author: zhangq
* @CreateDate: 2020-12-10 17:12
* @UpdateUser: zhangq
* @UpdateDate: 2020-12-10 17:12
* @UpdateRemark: The modified content
* @Version: 1.0 *
*/
public class RabbitMQUtils {
private static ConnectionFactory connectionFactory;
static {
connectionFactory = new ConnectionFactory();
connectionFactory.setHost("192.168.56.200");
connectionFactory.setPort(5672);
connectionFactory.setVirtualHost("/ems");
connectionFactory.setUsername("ems");
connectionFactory.setPassword("123");
}
public static Connection getConnection() {
try {
return connectionFactory.newConnection();
} catch (IOException e) {
e.printStackTrace();
} catch (TimeoutException e) {
e.printStackTrace();
}
return null;
}
//关闭连接
public static void closeConnectionFactory(Channel channel,Connection connection){
if(channel!=null){
try {
channel.close();
} catch (IOException e) {
e.printStackTrace();
} catch (TimeoutException e) {
e.printStackTrace();
}
}
if(connection!=null){
try {
connection.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
测试发送消息:
package org.example.Helloword;
import com.rabbitmq.client.AMQP.BasicProperties;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;
import java.io.IOException;
import org.example.RabbitMQUtils;
import org.junit.Test;
/**
* java类简单作用描述
*
* @ProjectName: rabbitmq
* @Package: org.example.Helloword
* @ClassName: Provider
* @Description: java类作用描述
* @Author: zhangq
* @CreateDate: 2020-12-10 17:21
* @UpdateUser: zhangq
* @UpdateDate: 2020-12-10 17:21
* @UpdateRemark: The modified content
* @Version: 1.0 *
*/
public class Provider {
@Test
public void sendmessage() throws IOException {
//获取连接对象
Connection connection= RabbitMQUtils.getConnection();
//获取连接中通道
Channel channel = connection.createChannel();
//通道绑定对应消息队列
//参数1 队列名称如果队列不存在则自动创建
//参数2 用来定义队列特性是否要持久化 true持久化
//参数3 是否是独占队列 true 独占
//参数4 消费者消费完消息是否自动删除队列 true自动删除
//参数5 额外参数
channel.queueDeclare("hello",false,false,false,null);
//发布消息
//参数1 交换机名称 参数2队列名称 参数3参数 参数4 消息的具体内容
channel.basicPublish("","hello",null,"hello rabbitmq".getBytes());
RabbitMQUtils.closeConnectionFactory(channel, connection);
}
public static void main(String[] args) throws IOException {
Connection connection = RabbitMQUtils.getConnection();
Channel channel = connection.createChannel();
channel.queueDeclare("hello",false,false,false,null);
channel.basicConsume("hello",true, new DefaultConsumer(channel){
@Override
public void handleDelivery(String consumerTag, Envelope envelope, BasicProperties properties,
byte[] body) throws IOException {
// super.handleDelivery(consumerTag, envelope, properties, body);
System.out.println("=========="+new String(body));
}
});
}
}
运行通过:
运行:消费者
消费完消息没有了:
channel.queueDeclare("hello",false,false,false,null); // channel.queueDeclare("hello",true,false,false,null); //发布消息 //参数1 交换机名称 参数2队列名称 参数3参数 参数4 消息的具体内容 channel.basicPublish("","hello",null,"张强测试".getBytes()); // channel.basicPublish("","hello", MessageProperties.PERSISTENT_TEXT_PLAIN,"张强测试".getBytes());
MessageProperties.PERSISTENT_TEXT_PLAIN :持久化消息