RabbitMQ: One broker to queue them all | RabbitMQ RabbitMQ官网
SpringAmqp的官方地址:Spring AMQP
代码示例:对着代码看应该能看明白
publisher:消息发送者的代码示例
package cn.itcast.mq.helloworld;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import org.junit.Test;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
public class PublisherTest {
@Test
public void testSendMessage() throws IOException, TimeoutException {
// 1.建立连接
ConnectionFactory factory = new ConnectionFactory();
// 1.1.设置连接参数,分别是:主机名、端口号、vhost、用户名、密码
factory.setHost("192.168.221.131");
factory.setPort(5672);
factory.setVirtualHost("/");
factory.setUsername("itcast");
factory.setPassword("123321");
// 1.2.建立连接
Connection connection = factory.newConnection();
// 2.创建通道Channel
Channel channel = connection.createChannel();
// 3.创建队列
String queueName = "simple.queue";
channel.queueDeclare(queueName, false, false, false, null);
// 4.发送消息
String message = "hello, rabbitmq!";
channel.basicPublish("", queueName, null, message.getBytes());
System.out.println("发送消息成功:【" + message + "】");
// 5.关闭通道和连接
channel.close();
connection.close();
}
}
package cn.itcast.mq.helloworld;
import com.rabbitmq.client.*;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
public class ConsumerTest {
public static void main(String[] args) throws IOException, TimeoutException {
// 1.建立连接
ConnectionFactory factory = new ConnectionFactory();
// 1.1.设置连接参数,分别是:主机名、端口号、vhost、用户名、密码
factory.setHost("192.168.221.131");
factory.setPort(5672);
factory.setVirtualHost("/");
factory.setUsername("itcast");
factory.setPassword("123321");
// 1.2.建立连接
Connection connection = factory.newConnection();
// 2.创建通道Channel
Channel channel = connection.createChannel();
// 3.创建队列
String queueName = "simple.queue";
channel.queueDeclare(queueName, false, false, false, null);
// 4.订阅消息
channel.basicConsume(queueName, true, new DefaultConsumer(channel){
@Override
public void handleDelivery(String consumerTag, Envelope envelope,
AMQP.BasicProperties properties, byte[] body) throws IOException {
// 5.处理消息
String message = new String(body);
System.out.println("接收到消息:【" + message + "】");
}
});
System.out.println("等待接收消息。。。。");
}
}
消息接收者代码示例.
SpringAMQP:
简单消息队列模型:Basic Queue
什么是SpringAMQP:
SpringAMQP消息的发送案例:
引入依赖
<!--AMQP依赖,包含RabbitMQ-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
在相应的模块下配置yml文件:
spring:
rabbitmq:
host: 192.168.221.131
port: 5672
virtual-host: /
username: itcast
password: 123321
编写测试代码:
@RunWith(SpringRunner.class) @SpringBootTest public class test01 { @Autowired private RabbitTemplate rabbitTemplate;//rabbit的工具类 @Test public void test(){ String queueName="simple.queue";//消息队列的名字 String message="hello libucun";//发送的消息 rabbitTemplate.convertAndSend(queueName,message);//连接并且发送 } }
发送成功后在你虚拟机rabbit映射地址里能看到
注意:如果出现IO异常错误 检查yml文件port地址不要搞错了。如果连接超时,查看虚拟机的端口有没有变,没变的话可以重启虚拟机重新启动mq容器再试试。