pom文件
<dependency>
<groupId>com.rabbitmq</groupId>
<artifactId>amqp-client</artifactId>
<version>5.7.0</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>
生产者
package com.ghgcn.hello.produce;
import com.rabbitmq.client.*;
import java.io.IOException;
import java.util.Date;
import java.util.concurrent.TimeoutException;
/**
* @author 刘楠
* @since 2019/5/28
*/
public class Producer {
private static String user_name="root";
private static String password="root123";
private static String rounter_key="root_key_hello";
private static String queueName="queue_hello";
private static String exchangeName="exchange_hello";
private static String virtualHost="/";
private static String hostName="localhost";
private static int port=5672;
public static void main(String[] args) throws IOException, TimeoutException {
//连接工厂
ConnectionFactory factory = new ConnectionFactory();
//mq安装的服务器地址
factory.setHost(hostName);
//mq端口
factory.setPort(port);
//用户名
factory.setUsername(user_name);
//密码
factory.setPassword(password);
//host
// factory.setVirtualHost(virtualHost);
//创建连接
Connection conn = factory.newConnection();
//创建Channel
Channel channel = conn.createChannel();
//创建交换器
channel.exchangeDeclare(exchangeName, BuiltinExchangeType.DIRECT,true,false,null);
//声明队列
//@param queue the name of the queue
//@param durable true if we are declaring a durable queue (the queue will survive a server restart) 持久化队列
//@param exclusive true if we are declaring an exclusive queue (restricted to this connection) 是否独占
//@param autoDelete true if we are declaring an autodelete queue (server will delete it when no longer in use) 是否自动删除
//@param arguments other properties (construction arguments) for the queue 属性
channel.queueDeclare(queueName,true,false,false,null);
//绑定队列与交换器的关系
channel.queueBind(queueName,exchangeName,rounter_key);
//内容
byte[] messageBodyBytes = "Hello, world!".getBytes();
System.out.println("生产者发送消息 : "+new Date());
channel.basicPublish(exchangeName,rounter_key, MessageProperties.PERSISTENT_TEXT_PLAIN,messageBodyBytes);
//关闭资源
channel.close();
conn.close();
}
}
消费者
package com.ghgcn.hello.consume;
import com.rabbitmq.client.*;
import java.io.IOException;
import java.util.Date;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
/**
* @author 刘楠
* @since 2019/5/28
*/
public class Consumer {
private static String user_name="root";
private static String password="root123";
private static String rounter_key="root_key_hello";
private static String queueName="queue_hello";
private static String exchangeName="exchange_hello";
private static String virtualHost="/";
private static String hostName="localhost";
private static int port=5672;
public static void main(String[] args) throws IOException, TimeoutException, InterruptedException {
//连接工厂
ConnectionFactory factory = new ConnectionFactory();
//mq安装的服务器地址
factory.setHost(hostName);
//mq端口
factory.setPort(port);
//用户名
factory.setUsername(user_name);
//密码
factory.setPassword(password);
//host
// factory.setVirtualHost(virtualHost);
Address[] addresses = {new Address(hostName,port)};
//建立连接
Connection conn = factory.newConnection(addresses);
Channel channel = conn.createChannel();
//设置客户端最多接收多个少未ACK的消息
channel.basicQos(64);
//设置消费者
com.rabbitmq.client.Consumer consumer = new DefaultConsumer(channel){
@Override
public void handleDelivery(String consumerTag,
Envelope envelope, AMQP.BasicProperties properties,
byte[] body) throws IOException {
System.out.println("接收消息: "+new String(body)+" "+ new Date());
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
channel.basicAck(envelope.getDeliveryTag(),false);
}
};
channel.basicConsume(queueName,consumer);
TimeUnit.SECONDS.sleep(5);
channel.close();
}
}
https://github.com/ln0491/rabbitmq-learning/tree/master/rabbitmq-learning/hello-mq