RabbitMQ学习(三)RabbitMQ基本操作

代码如下

public class RabbitmqTest {

    private static final String IP_ADDRESS = "192.168.1.190";
    private static final int PORT = 5672;
    private static final String USERNAME = "user";
    private static final String PASSWORD = "user";
    //生产者变量定义
    private static final String EXCAHNGE_NAME = "exchange";
    private static final String ROUTING_KEY = "rountingkey";
    private static final String QUEUE_NAME = "queue";

    public static void main(String[] args) throws IOException, TimeoutException, InterruptedException {
        Connection connection = getInstance();
        System.out.println("connection" + connection);
        //publisher(connection);
        consumer(connection);


    }

    /**
     * @return com.rabbitmq.client.Connection
     * @Description //获取连接
     * @Param []
     **/
    public static Connection getInstance() {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost(IP_ADDRESS);
        factory.setPort(PORT);
        factory.setUsername(USERNAME);
        factory.setPassword(PASSWORD);
        Connection conn = null;
        try {
            conn = factory.newConnection();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return conn;
    }

    /**
     * @return void
     * @Description //消费者客户端
     * @Param [connection]
     **/
    public static void publisher(Connection connection) throws IOException {
        //创建信道
        Channel channel = connection.createChannel();
        //创建交换器
        channel.exchangeDeclare(EXCAHNGE_NAME, "direct", true, false, null);
        //创建队列
        channel.queueDeclare(QUEUE_NAME, true, false, false, null);
        //交换器与队列通过路由键绑定
        channel.queueBind(QUEUE_NAME, EXCAHNGE_NAME, ROUTING_KEY);
        String message = "hello world";
        channel.basicPublish(EXCAHNGE_NAME, ROUTING_KEY, MessageProperties.PERSISTENT_TEXT_PLAIN, message.getBytes());
        try {
            channel.close();
            connection.close();
        } catch (TimeoutException e) {
            e.printStackTrace();
        }
    }

    /**
     * @return void
     * @Description //消费者客户端
     * @Param []
     **/
    public static void consumer(Connection connection) throws IOException, InterruptedException, TimeoutException {
        //创建信道
        Channel channel = connection.createChannel();
        channel.basicQos(64);
        DefaultConsumer consumer = new DefaultConsumer(channel) {
            @Override
            public void handleDelivery(String consumerTag,
                                       Envelope envelope,
                                       AMQP.BasicProperties properties,
                                       byte[] body)
                    throws IOException {
                System.out.println("recv message:" + new String(body));
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                channel.basicAck(envelope.getDeliveryTag(), false);
            }
        };
        channel.basicConsume(QUEUE_NAME, consumer);
        //回调执行完毕,关闭资源
        TimeUnit.SECONDS.sleep(5);
        channel.close();
        connection.close();


    }
}

你可能感兴趣的:(RabbitMQ学习(三)RabbitMQ基本操作)