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: