RabbitMq学习笔记(一)—— HelloWorld

Maven:

<!-- rabbitmq -->

<dependency>

  <groupId>com.rabbitmq</groupId>

  <artifactId>amqp-client</artifactId>

  <version>4.0.0</version>

</dependency>

<dependency>

    <groupId>com.rabbitmq</groupId>

    <artifactId>rabbitmq-client</artifactId>

    <version>1.3.0</version>

</dependency>


配置连接工厂(ConnectionFactory)

ConnectionFactory factory = new ConnectionFactory();

方式一:

factory.setUri("amqp://admin:admin@172.16.41.31:5672");

方式二:

factory.setUsername("admin");

factory.setPassword("admin");

factory.setHost("172.16.41.31");

factory.setPort(5672);

factory.setVirtualHost("/");


开启连接(Connection)

Connection conn = factory.newConnection();


开启通道(Channel)

Channel channel = conn.createChannel();


声明队列名称

channel.queueDeclare("queue_hello", false, false, false, null);


构造消息

String message = "Hello World";


发送消息到队列中

channel.basicPublish("","queue_hello", null, message.getBytes("UTF-8"));


释放资源

conn.close()

channel.close()



评论

© dzxlovelar | Powered by LOFTER