02_ActiveMQ_消费者配置

同生产者类似,只需要在创建用户类型时改成Consumer

public static void main(String[] args) {
    //声明Connection变量
    Connection connection = null;
    //创建连接工厂
    ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory();
    //设置账密及地址
    connectionFactory.setUserName("admin");
    connectionFactory.setPassword("admin");
    connectionFactory.setBrokerURL("tcp://localhost:61616");

    try {
        //创建连接
        connection = connectionFactory.createConnection();
        //创建Session,第一个参数是否启用事务,第二个参数配置签收模式
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        //创建消息来源对象
        Queue queue = session.createQueue("test");
        //创建消费者
        MessageConsumer consumer = session.createConsumer(queue);
        while (true) {
            //这里循环来取队列数据进行消费
            TextMessage tms = (TextMessage) consumer.receive();
            System.out.println(tms.getText());
        }
    } catch (JMSException e) {
        e.printStackTrace();
    } finally {
        try {
            connection.close();
        } catch (JMSException e) {
        }
    }

}

猜你喜欢

转载自blog.csdn.net/butterballj/article/details/80879720
今日推荐