[关闭]
@adamhand 2019-01-02T16:37:30.000000Z 字数 2500 阅读 709

RabbitMQ系列(3)--第一个程序


简介

这是使用RabbitMQ写的第一个练习程序。生产者与RabbitMQ server连接之后,通过"direct"的方式发送一条"hello world"消息到队列中;消费者与RabbitMQ server连接之后就可以从队列中取出消息,然后打印。

要运行以下程序,首先通过rabbitmq-service start命令启动RabbitMQ server,然后运行RabbitMQProducer发送消息,接着运行RabbitMQConsumer就可以取出消息。

程序

Producer端的程序如下:

  1. public class RabbitMQProducer {
  2. private static final String EXCHANGE_NAME = "exchange_demo";
  3. private static final String ROUTING_KEY = "routingkey_demo";
  4. private static final String QUEUE_NAME = "queue_demo";
  5. private static final String IPADDRESS = "192.168.1.107";
  6. private static final int PORT = 5672; //RabbitMQ服务端默认端口号为5672
  7. public static void main(String[] args) throws IOException, TimeoutException {
  8. ConnectionFactory factory = new ConnectionFactory();
  9. factory.setHost(IPADDRESS);
  10. factory.setPort(PORT);
  11. factory.setUsername("root"); //要连接到RabbitMQ的用户名
  12. factory.setPassword("root"); //要连接到RabbitMQ的密码
  13. Connection connection = factory.newConnection(); //创建连接
  14. Channel channel = connection.createChannel(); //创建channel
  15. //创建一个交换类型为direct、持久化、非自动删除的交换器
  16. channel.exchangeDeclare(EXCHANGE_NAME, "direct", true, false, null);
  17. //创建一个持久化、非排他的、非自动删除的队列
  18. channel.queueDeclare(QUEUE_NAME, true, false, false, null);
  19. //将交换机与队列通过路由键绑定
  20. channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, ROUTING_KEY);
  21. //发送一条持久化消息:hello world
  22. String msg = "hello world";
  23. channel.basicPublish(EXCHANGE_NAME, ROUTING_KEY,
  24. MessageProperties.PERSISTENT_TEXT_PLAIN,
  25. msg.getBytes());
  26. //关闭资源
  27. channel.close();
  28. connection.close();
  29. }
  30. }

Consumer端的程序如下:

  1. public class RabbitMQConsumer {
  2. private static final String QUEUE_NAME = "queue_demo";
  3. private static final String IPADDRESS = "192.168.1.107";
  4. private static final int PORT = 5672;
  5. public static void main(String[] args) throws IOException, TimeoutException, InterruptedException {
  6. Address[] addresses = new Address[]{
  7. new Address(IPADDRESS, PORT)
  8. };
  9. ConnectionFactory factory = new ConnectionFactory();
  10. factory.setUsername("root");
  11. factory.setPassword("root");
  12. Connection connection = factory.newConnection(addresses);
  13. final Channel channel = connection.createChannel();
  14. channel.basicQos(64); //设置客户端最多接受未被ack的消息的个数
  15. Consumer consumer = new DefaultConsumer(channel){
  16. @Override
  17. public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
  18. System.out.println("receive msg "+new String(body)); //body代表消息实体
  19. try {
  20. TimeUnit.SECONDS.sleep(1);
  21. } catch (InterruptedException e) {
  22. e.printStackTrace();
  23. }
  24. channel.basicAck(envelope.getDeliveryTag(), false);
  25. }
  26. };
  27. //等待回调函数执行完毕之后,关闭资源
  28. channel.basicConsume(QUEUE_NAME, consumer);
  29. TimeUnit.SECONDS.sleep(5);
  30. channel.close();
  31. connection.close();
  32. }
  33. }

运行成功后,Consumer端会打印如下消息:

  1. receive msg hello world

可以通过登录localhost:15672来查看连接状态。



参考

《RabbitMQ实战指南》

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注