pom
前提是Maven工程,用idea建
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.example</groupId> <artifactId>RabbitmqDemo</artifactId> <version>1.0-SNAPSHOT</version> <!-- RabbitMQ --> <dependencies> <dependency> <groupId>com.rabbitmq</groupId> <artifactId>amqp-client</artifactId> <version>4.2.1</version> </dependency> </dependencies> </project>
生产者class
package Producter; import com.rabbitmq.client.*; import java.io.IOException; import java.util.concurrent.TimeoutException; public class RabbitProducer { private static final String EXCHANGE_NAME = "exchange_demo"; private static final String ROUNTING_KEY = "rountingkey_demo"; private static final String QUEUE_NAME = "queue_demo"; private static final String IP_ADDRESS = "虚拟机或者windows的ip"; /** * Rabbitmq默认端口 * */ private static final int PORT = 5672; public static void main(String[] args) throws IOException, TimeoutException, InterruptedException { ConnectionFactory factory = new ConnectionFactory(); factory.setHost(IP_ADDRESS); factory.setPort(PORT); factory.setUsername("admin"); factory.setPassword("admin"); // 创建连接 Connection connection = factory.newConnection(); // 创建信道 Channel channel = connection.createChannel(); // 创建 持久化 非自动删除的交换器 channel.exchangeDeclare(EXCHANGE_NAME, "direct", true, false, null); // 创建持久化非排他,非自动删除的队列 channel.queueDeclare(QUEUE_NAME, true, false, false, null); // 交换器与队列通过路由键绑定 channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, ROUNTING_KEY); // send 持久化消息 helloworld! String message = "Hello World!"; channel.basicPublish(EXCHANGE_NAME, ROUNTING_KEY, MessageProperties.PERSISTENT_TEXT_PLAIN, message.getBytes()); // 关闭资源 channel.close(); connection.close(); } }
消费者class
package Consumer; import com.rabbitmq.client.*; import java.io.IOException; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; public class RabbitConsumer { private static final String QUEUE_NAME = "queue_demo"; private static final String IP_ADDRESS = "和上面一样"; /** * Rabbitmq默认端口 * */ private static final int PORT = 5672; public static void main(String[] args) throws IOException, TimeoutException, InterruptedException { Address[] addresses = new Address[]{ new Address(IP_ADDRESS, PORT) }; ConnectionFactory factory = new ConnectionFactory(); factory.setUsername("admin"); factory.setPassword("admin"); // 连接方式略有不同 // 创建连接 Connection connection = factory.newConnection(addresses); // 创建信道 final Channel channel = connection.createChannel(); // 客户端最多接受未被ack的消息个数 channel.basicQos(64); Consumer consumer = new DefaultConsumer(channel) { @Override public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException { System.out.println("recv message: " + new String(body)); try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); } channel.basicAck(envelope.getDeliveryTag(), false); } }; channel.basicConsume(QUEUE_NAME, consumer); TimeUnit.SECONDS.sleep(5); channel.close(); connection.close(); } }