1 导入依赖
<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>com.shi.rabbitmq</groupId>
<artifactId>rabbitmq-01</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<!-- RabbitMQ的客户端 -->
<dependency>
<groupId>com.rabbitmq</groupId>
<artifactId>amqp-client</artifactId>
<version>3.4.1</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.7</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.3.2</version>
</dependency>
<dependency>
<groupId>org.springframework.amqp</groupId>
<artifactId>spring-rabbit</artifactId>
<version>1.4.0.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
2 工具类
package com.shi.util;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
/**
* 创建工厂类--rabbitMq
* @author SHF
* @version 创建时间:2018年7月3日 上午9:49:57
*/
public class RabbitMqUtils {
private static String host = "127.0.0.1";
private static Integer port = 5672;
private static String userName = "shiye";
private static String pwd = "shiye";
private static String vhost = "/test";
public static Connection getConnection() throws IOException, TimeoutException{
//1 定义链接工厂
ConnectionFactory factory = new ConnectionFactory();
//2 设置服务器地址,端口,账户信息,用户名,密码,vhost
factory.setHost(host);
factory.setPort(port);
factory.setVirtualHost(vhost);
factory.setUsername(userName);
factory.setPassword(pwd);
//3 通过工厂获取链接
Connection connection = factory.newConnection();
return connection;
}
}
3 生产者
/**
* 生产者
* @author SHF
* @version 创建时间:2018年7月3日 上午10:08:57
* @throws TimeoutException
* @throws IOException
*/
@Test
public void producer() throws IOException, TimeoutException {
//1 获取链接以及mq通道
Connection connection = RabbitMqUtils.getConnection();
Channel channel = connection.createChannel();
//2 申明队列
DeclareOk queueOK = channel.queueDeclare(QUEUE_NAME, false, false, false, null);
//3 添加消息内容
String message = "Hello World; 施爷";
channel.basicPublish("", QUEUE_NAME, null, message.getBytes());
System.out.println( "sent: '" + message + "'");
//关闭通道和连接
channel.close();
connection.close();
}
4 消费者
/**
* 消费者
* @author SHF
* @version 创建时间:2018年7月3日 上午11:03:25
* @throws Exception
*/
@Test
public void Consum() throws Exception{
//1 获取链接及mq通道,
Connection connection = RabbitMqUtils.getConnection();
Channel channel = connection.createChannel();
//2 申明队列(如果存在就不创建)
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
//3 定义队列的消费者
QueueingConsumer cunusumer = new QueueingConsumer(channel);
//4 监听队列
channel.basicConsume(QUEUE_NAME, true,cunusumer);
//5 获取消息
while(true) {
Delivery delivery = cunusumer.nextDelivery();
String message = new String(delivery.getBody());
System.out.println(" received: " + message);
}
}