Skip to content

rabbitmq实践

mvn依赖引入

xml

<dependency>
    <groupId>com.rabbitmq</groupId>
    <artifactId>amqp-client</artifactId>
    <version>5.20.0</version>
</dependency>

hello world 代码尝试

java
   ConnectionFactory connectionFactory = new ConnectionFactory();
   connectionFactory.setHost("localhost");
   connectionFactory.setPort(5672);
   connectionFactory.setUsername("admin");
   connectionFactory.setPassword("admin");
   Connection connection = connectionFactory.newConnection();
   Channel channel = connection.createChannel();
   channel.queueDeclare("simple_test",true,false,false,null);
   String message = "hello world";
   channel.basicPublish("","simple_test",null,message.getBytes());
   System.out.println("message = " + message);
   channel.close();
   connection.close();

work queue

PUB/SUB 发布/订阅模式