📄 sam.pubsub.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head> <title>Publish/Subscribe and suscriptions to topics</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> </head> <body><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="sam.operations.html">Messaging operations</a></div> <div class="next" style="text-align: right; float: right;"><a href="sam.errors.html">Error handling</a></div> <div class="up"><a href="sam.examples.html">Examples</a></div> <div class="home"><a href="index.html">PHP Manual</a></div></div><hr /><div id="sam.pubsub" class="section"> <h2 class="title">Publish/Subscribe and suscriptions to topics</h2> <p class="para"> SAM allows messages to be sent either to queues or, for WebSphere MQ and WPM, to publish/subscribe topics. A topic desintation is specified to SAM in the usual way, i.e. in the form 'topic://fred', rather than the form 'queue://AQUEUE' used for point to point operation. To use publish/subscribe it is simply necessary to specify the correct broker name on the SAMConnect "connect" call and the desired topic in the destination argument to the SAMConnect "send" and "receive" calls. The PHP interface is otherwise identical to the point to point model. </p> <p class="para"> By default, SAM creates non-durable subscriptions when using publish/subscribe. This means that if a client application is inactive when messages are published to a topic, then it will not receive them when it subsequently restarted. SAM does also allow durable subscriptions to be made to topics when using WPM or WebSphere MQ publish/subscribe. The purpose of these subscriptions is to allow data to be received by a client application even if that client was not active at the time the data was published. </p> <p class="para"> Durable subscriptions are specified by using the SAMConnect "subscribe" call. This method takes the destination topic as an input parameter and returns a subscription identifier that may be used on subsequent "receive" calls. When the subscription is no longer required the SAMConnection "unsubscribe" method should be used to delete the subscription. </p> <p class="para"> <div class="example"> <p><b>Example #1 Creating a durable subscription to a topic</b></p> <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?php<br /><br />$subName </span><span style="color: #007700">= </span><span style="color: #0000BB">$conn</span><span style="color: #007700">-></span><span style="color: #0000BB">subscribe</span><span style="color: #007700">(</span><span style="color: #DD0000">'topic://A'</span><span style="color: #007700">);<br /><br />if (!</span><span style="color: #0000BB">$subName</span><span style="color: #007700">) {<br /> echo </span><span style="color: #DD0000">"Subscribe failed"</span><span style="color: #007700">;<br />} else {<br /> </span><span style="color: #FF8000"># Subscribe was OK<br /> // ...<br /></span><span style="color: #007700">}<br /></span><span style="color: #0000BB">?></span></span></code></div> </div> </div> </p> <p class="para"> <div class="example"> <p><b>Example #2 Subscribing to a topic using a WebSphere Platform Messaging (WPM) server</b></p> <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?php<br />$conn </span><span style="color: #007700">= new </span><span style="color: #0000BB">SAMConnection</span><span style="color: #007700">();<br /></span><span style="color: #FF8000">// Note: For pub/sub on WPM, when connecting the name of a messaging engine<br />// to hold the durable subscription (SAM_WPM_DUR_SUB_HOME) must be specified.<br /></span><span style="color: #0000BB">$conn</span><span style="color: #007700">-></span><span style="color: #0000BB">connect</span><span style="color: #007700">(</span><span style="color: #0000BB">SAM_WMQ</span><span style="color: #007700">, array(</span><span style="color: #0000BB">SAM_ENDPOINTS </span><span style="color: #007700">=> </span><span style="color: #DD0000">'localhost:7278:BootstrapBasicMessaging'</span><span style="color: #007700">,<br /> </span><span style="color: #0000BB">SAM_BUS </span><span style="color: #007700">=> </span><span style="color: #DD0000">'Bus1'</span><span style="color: #007700">,<br /> </span><span style="color: #0000BB">SAM_TARGETCHAIN </span><span style="color: #007700">=> </span><span style="color: #DD0000">'InboundBasicMessaging'<br /> </span><span style="color: #0000BB">SAM_WPM_DUR_SUB_HOME </span><span style="color: #007700">=> </span><span style="color: #DD0000">'MyMachineNode01.server1-Bus1'</span><span style="color: #007700">));<br /><br /></span><span style="color: #0000BB">$subName </span><span style="color: #007700">= </span><span style="color: #0000BB">$conn</span><span style="color: #007700">-></span><span style="color: #0000BB">subscribe</span><span style="color: #007700">(</span><span style="color: #DD0000">'topic://A'</span><span style="color: #007700">);<br /><br />if (!</span><span style="color: #0000BB">$subName</span><span style="color: #007700">) {<br /> echo </span><span style="color: #DD0000">"Subscribe failed"</span><span style="color: #007700">;<br />} else {<br /> </span><span style="color: #FF8000"># Subscribe was OK<br /> // ...<br /></span><span style="color: #007700">}<br /></span><span style="color: #0000BB">?></span></span></code></div> </div> </div> </p> <p class="para"> <div class="example"> <p><b>Example #3 Receiving published data using a durable subscription</b></p> <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?php<br /><br />$msg </span><span style="color: #007700">= </span><span style="color: #0000BB">$conn</span><span style="color: #007700">-></span><span style="color: #0000BB">receive</span><span style="color: #007700">(</span><span style="color: #0000BB">$subName</span><span style="color: #007700">);<br />if (</span><span style="color: #0000BB">$msg</span><span style="color: #007700">) {<br /> echo </span><span style="color: #DD0000">"Received a message OK"</span><span style="color: #007700">;<br />} else {<br /> echo </span><span style="color: #DD0000">"The receive failed"</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">?></span></span></code></div> </div> </div> </p> <p class="para"> <div class="example"> <p><b>Example #4 Deleting a durable subscription to a topic</b></p> <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?php<br /><br /></span><span style="color: #007700">if (!</span><span style="color: #0000BB">$conn</span><span style="color: #007700">-></span><span style="color: #0000BB">unsubscribe</span><span style="color: #007700">(</span><span style="color: #0000BB">$subName</span><span style="color: #007700">)) {<br /> echo </span><span style="color: #DD0000">"Unsubscribe failed"</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">?></span></span></code></div> </div> </div> </p> </div><hr /><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="sam.operations.html">Messaging operations</a></div> <div class="next" style="text-align: right; float: right;"><a href="sam.errors.html">Error handling</a></div> <div class="up"><a href="sam.examples.html">Examples</a></div> <div class="home"><a href="index.html">PHP Manual</a></div></div></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -