4.3. 生成和消耗测试消息

启动代理后,您应该验证它是否在正确运行。这涉及生成一些测试消息,将它们发送到代理,然后消耗它们。

步骤

  1. 使用 artemis producer 命令生成一些测试消息,并将它们发送到代理。

    此命令将 100 个消息发送到 helloworld 地址,该地址会在代理上自动创建。制作者使用默认端口 61616 连接代理,后者接受所有支持的消息传递协议。

    $ /opt/redhat/amq-broker/amq-broker-7.2.0/bin/artemis producer --destination helloworld --message-count 100 --url tcp://localhost:61616
    Producer ActiveMQQueue[helloworld], thread=0 Started to calculate elapsed time ...
    
    Producer ActiveMQQueue[helloworld], thread=0 Produced: 100 messages
    Producer ActiveMQQueue[helloworld], thread=0 Elapsed time in second : 1 s
    Producer ActiveMQQueue[helloworld], thread=0 Elapsed time in milli second : 1289 milli seconds
  2. 使用 Web 控制台查看代理中存储的信息。

    1. 在 Web 浏览器中,导航到 http://localhost:8161
    2. 使用创建代理实例时创建的默认用户名和密码登录到控制台。

      此时会显示 Attributes 选项卡。

    3. Attributes 选项卡中,导航到 menu:[addresses > helloworld > queue > "anycast" > helloworld]。

      在上一步中,您会将消息发送到 helloworld 地址。这会创建一个带有队列(也称为 helloworld )的新广播 helloworld地址。Message count 属性显示发送到 helloworld 的所有 100 消息都存储在此队列中。

      图 4.1. 消息计数

      消息计数
  3. 使用 artemis consumer 命令,消耗代理上存储了 50 个消息。

    这个命令会消耗您发送到代理的 50 个信息。

    $ /opt/redhat/amq-broker/amq-broker-7.2.0/bin/artemis consumer --destination helloworld --message-count 50 --url tcp://localhost:61616
    
    Consumer:: filter = null
    Consumer ActiveMQQueue[helloworld], thread=0 wait until 50 messages are consumed
    Consumer ActiveMQQueue[helloworld], thread=0 Consumed: 50 messages
    Consumer ActiveMQQueue[helloworld], thread=0 Consumer thread finished
  4. 在 Web 控制台中,验证 消息数 现在是否为 50。

    消耗了 50 个消息,它保留了 helloworld 队列中存储 50 个消息。

  5. 停止代理,并验证 50 个剩余的消息仍然存储在 helloworld 队列中。

    1. 在运行代理的终端中,按 Ctrl+C 来停止代理。
    2. 重启代理。

      $ /var/opt/amq-broker/mybroker/bin/artemis run
    3. 在 Web 控制台中,返回 helloworld 队列,并验证队列中存储了 50 个消息。
  6. 使用剩余的 50 信息。

    $ /opt/redhat/amq-broker/amq-broker-7.2.0/bin/artemis consumer --destination helloworld --message-count 50 --url tcp://localhost:61616
    
    Consumer:: filter = null
    Consumer ActiveMQQueue[helloworld], thread=0 wait until 50 messages are consumed
    Consumer ActiveMQQueue[helloworld], thread=0 Consumed: 50 messages
    Consumer ActiveMQQueue[helloworld], thread=0 Consumer thread finished
  7. 在 Web 控制台中,验证 Message 计数 是否为 0。

    helloworld 队列中存储的所有消息都被消耗,队列现在为空。