java调用RabbitMQ消息队列

下载jar包

https://www.rabbitmq.com/java-client.html

调用说明 https://www.rabbitmq.com/api-guide.html

下载slf4j 日志包

https://www.rabbitmq.com/java-client.html

引入包

amqp-client-5.10.0.jar
slf4j-api-1.7.30.jar
slf4j-simple-1.7.30.jar
消息接受代码
public  static  void  main(String[] args) {
        String qname="demo";
        Connection  conn=null;
        Channel chn=null;
        try {
            ConnectionFactory factory = new ConnectionFactory();
            factory.setHost("localhost");
            factory.setPort(5672);
            factory.setUsername("guest");
            factory.setPassword("guest");
            factory.setVirtualHost("/");
            conn = factory.newConnection();
            chn = conn.createChannel();

            Consumer consumer = new DefaultConsumer(chn) {
                @Override
                public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties,
                                           byte[] body) throws IOException {
                    String message = new String(body, "UTF-8");
                    System.out.println(envelope.getExchange() + "," + envelope.getRoutingKey() + "," + message);
                }
            };
            // channel绑定队列,autoAck为true表示一旦收到消息则自动回复确认消息
            chn.basicConsume(qname, true, consumer);


        }catch(Exception ex){
            System.out.println(ex);
        }
    }

参考网址:
https://my.oschina.net/xiaozhiwen/blog/1926657