PHP uses ActiveMQ instances and phpactivemq instances

Source: Internet
Author: User
Tags stomp

PHP uses ActiveMQ instances and phpactivemq instances

Use Point-To-Point models

Point-to-Point model features:

  • Only one consumer can receive messages.
  • Repeated consumption is not allowed.

Producer. php code:

<? Phptry {// 1. establish a connection $ stomp = new Stomp ('tcp: // 47.52.119.21: 100'); // 2. instantiate class $ obj = new Stdclass (); // 3. get data for ($ I = 0; $ I <3; $ I ++) {$ obj-> username = 'test'; $ obj-> password = '2016 '; $ queneName = "/queue/userReg"; // 4. send a registration message to the queue $ stomp-> send ($ queneName, json_encode ($ obj) ;}} catch (StompException $ e) {die ('Connection failed :'. $ e-> getMessage ());}

Consumer 1consumer1. php code:

<? Php $ stomp = new Stomp ('tcp: // localhost: 100'); $ stomp-> subscribe ('/queue/userReg'); while (true) {// determine whether the read information is available. if ($ stomp-> hasFrame () {$ frame = $ stomp-> readFrame (); $ data = json_decode ($ frame-> body, true); var_dump ($ data); $ stomp-> ack ($ frame );}}

Consumer 2consumer2. php code:

<? Php $ stomp = new Stomp ('tcp: // localhost: 100'); $ stomp-> subscribe ('/queue/userReg'); while (true) {// determine whether the read information is available. if ($ stomp-> hasFrame () {$ frame = $ stomp-> readFrame (); $ data = json_decode ($ frame-> body, true); var_dump ($ data); $ stomp-> ack ($ frame );}}

The execution result is shown as follows:

Use the Publish/Subscribe Model

Features of the publishing/subscription model:

Multiple consumers can receive messages.
Repeated consumption
Producer. php code:

<? Phptry {// 1. establish a connection $ stomp = new Stomp ('tcp: // 47.52.119.21: 100'); // 2. instantiate class $ obj = new Stdclass (); // 3. get data for ($ I = 0; $ I <3; $ I ++) {$ obj-> username = 'test'; $ obj-> password = '2016 '; $ queneName = "/topic/userReg"; // 4. send a registration message to the queue $ stomp-> send ($ queneName, json_encode ($ obj) ;}} catch (StompException $ e) {die ('Connection failed :'. $ e-> getMessage ());}

Consumer 1consumer1. php code:

<? Php $ stomp = new Stomp ('tcp: // localhost: 100'); $ stomp-> subscribe ('/topic/userReg'); while (true) {// determine whether the read information is available. if ($ stomp-> hasFrame () {$ frame = $ stomp-> readFrame (); $ data = json_decode ($ frame-> body, true); var_dump ($ data); $ stomp-> ack ($ frame );}}

Consumer 2consumer2. php code:

? Php $ stomp = new Stomp ('tcp: // localhost: 100'); $ stomp-> subscribe ('/topic/userReg'); while (true) {// determine whether the read information is available. if ($ stomp-> hasFrame () {$ frame = $ stomp-> readFrame (); $ data = json_decode ($ frame-> body, true); var_dump ($ data); $ stomp-> ack ($ frame );}}

The execution result is shown as follows:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.