File: examples.xml

package info (click to toggle)
php-doc 20100521-2
  • links: PTS, VCS
  • area: main
  • in suites: squeeze, wheezy
  • size: 59,992 kB
  • ctags: 4,085
  • sloc: xml: 796,833; php: 21,338; cpp: 500; sh: 117; makefile: 58; awk: 28
file content (357 lines) | stat: -rw-r--r-- 10,220 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision: 288721 $ -->

<chapter xml:id="sam.examples" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
 &reftitle.examples;
 <section xml:id='sam.connections'>
  <title>Connections</title>
  <para>
   In order to perform any messaging and queueing functions a connection
   must be established with a messaging server by creating a SAMConnection
   object and calling its "connect" method, with a set of connection
   properties, to connect the PHP script to the messaging server.  Until
   such time as the SAMConnection object is destroyed the connection
   will be maintained and available for use. All SAMConnection objects
   are destroyed when the PHP script exits.
  </para>
  <para>
   A set of default properties may be used in connecting to a messaging
   server but as a minimum the PHP script must specify a protocol to be
   used.
  </para>
  <para>
   <example>
    <title>Creating a connection and connecting to a remote WebSphere MQSeries Messaging Server</title>
    <programlisting role='php'>
<![CDATA[
<?php
$conn = new SAMConnection();
$conn->connect(SAM_WMQ, array(SAM_HOST => 'myhost.mycompany.com',
                              SAM_PORT => 1506,
                              SAM_BROKER => 'mybroker'));
?>
]]>
    </programlisting>
   </example>
  </para>
  <para>
   <example>
    <title>Creating a connection and connecting to a remote WebSphere Application Server</title>
    <programlisting role='php'>
<![CDATA[
<?php
$conn = new SAMConnection();
$conn->connect(SAM_WPM, array(SAM_ENDPOINTS => 'localhost:7278:BootstrapBasicMessaging',
                              SAM_BUS => 'Bus1',
                              SAM_TARGETCHAIN => 'InboundBasicMessaging'));
?>
]]>
    </programlisting>
   </example>
  </para>
  <para>
   <example>
    <title>Creating a connection and connecting to an MQTT server</title>
    <programlisting role='php'>
<![CDATA[
<?php
$conn = new SAMConnection();
$conn->connect(SAM_MQTT, array(SAM_HOST => 'myhost.mycompany.com',
                               SAM_PORT => 1883));
?>
]]>
    </programlisting>
   </example>
  </para>
 </section>  <!-- xml:id=sam.connections -->

 <section xml:id='sam.messages'>
  <title>Messages</title>
  <para>
   Messages sent to and received from queues are represented by the
   SAMMessage object. The SAMMessage object encapsulates the body of
   the message (if one exists) and the header properties associated with
   the message. A SAMMessage object is either supplied as a parameter to
   a messaging operation or returned as a result.
  </para>
  <para>
   <example>
    <title>Creating a message with a simple text body</title>
    <programlisting role="php">
<![CDATA[
<?php
$msg = new SAMMessage('This is a simple text message');
?>
]]>
    </programlisting>
   </example>
  </para>
  <para>
   Messages may have header properties associated with them that provide
   control over the transport of the message or further information to the
   receiving application. By default message properties are delivered to
   the underlying messaging system as strings and in this case they may be
   set with the following simple syntax:
  </para>
  <para>
   <example>
    <title>Setting a text format property using the default syntax</title>
    <programlisting role="php">
<![CDATA[
<?php
$msg->header->myPropertyName = 'textData';
?>
]]>
    </programlisting>
   </example>
  </para>
  <para>
   If it is desired to pass type information an alternative syntax may be
   used where the value and the type hint are passed in an associative
   array:
  </para>
  <para>
   <example>
    <title>Setting a property using a type hint</title>
    <programlisting role="php">
<![CDATA[
<?php
$msg->header->myPropertyName = array(3.14159, SAM_FLOAT);
?>
]]>
    </programlisting>
   </example>
  </para>
  <para>
   Properties may also be extracted from the header of a message.
  </para>
  <para>
   <example>
    <title>Retrieving a property from a message header</title>
    <programlisting role="php">
<![CDATA[
<?php
$myProperty = $msg->header->myPropertyName;
?>
]]>
    </programlisting>
   </example>
  </para>
 </section>  <!-- xml:id=sam.messages -->

 <section xml:id='sam.operations'>
  <title>Messaging operations</title>
  <para>
   All messaging operations are performed through calls to methods on the
   connection object.
   To add a message to a queue the "send" method is used, to obtain a
   message from a queue the "receive" method is used. Other methods
   provide publish and subscribe functionality and control of transaction
   boundaries.
  </para>
  <para>
   <example>
    <title>Adding a message to a queue and receiving a response</title>
    <programlisting role="php">
<![CDATA[
<?php
$msg = new SAMMessage('This is a simple text message');
$msg->header->SAM_REPLY_TO = 'queue://receive/test';
$correlid = $conn->send('queue://send/test', $msg);

if (!$correlid) {
  // The Send failed!
  echo "Send failed ($conn->errno) $conn->error";
} else {
  $resp = $conn->receive('queue://receive/test', array(SAM_CORRELID => $correlid));
}
?>
]]>
    </programlisting>
   </example>
  </para>
 </section>  <!-- xml:id=sam.operations -->

 <section xml:id='sam.pubsub'>
  <title>Publish/Subscribe and subscriptions to topics</title>
  <para>
   SAM allows messages to be sent either to queues or, for WebSphere MQ
   and WPM, to publish/subscribe topics.
   A topic destination 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.
  </para>
  <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.
  </para>
  <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.
  </para>
  <para>
   <example>
    <title>Creating a durable subscription to a topic</title>
    <programlisting role="php">
<![CDATA[
<?php

$subName = $conn->subscribe('topic://A');

if (!$subName) {
   echo "Subscribe failed";
} else {
   # Subscribe was OK
   // ...
}
?>
]]>
    </programlisting>
   </example>
  </para>
  <para>
   <example>
    <title>Subscribing to a topic using a WebSphere Platform Messaging (WPM) server</title>
    <programlisting role="php">
<![CDATA[
<?php
$conn = new SAMConnection();
// Note: For pub/sub on WPM, when connecting the name of a messaging engine
//   to hold the durable subscription (SAM_WPM_DUR_SUB_HOME) must be specified.
$conn->connect(SAM_WMQ, array(SAM_ENDPOINTS => 'localhost:7278:BootstrapBasicMessaging',
                              SAM_BUS => 'Bus1',
                              SAM_TARGETCHAIN => 'InboundBasicMessaging',
                              SAM_WPM_DUR_SUB_HOME => 'MyMachineNode01.server1-Bus1'));

$subName = $conn->subscribe('topic://A');

if (!$subName) {
   echo "Subscribe failed";
} else {
   # Subscribe was OK
   // ...
}
?>
]]>
    </programlisting>
   </example>
  </para>
  <para>
   <example>
    <title>Receiving published data using a durable subscription</title>
    <programlisting role="php">
<![CDATA[
<?php

$msg = $conn->receive($subName);
if ($msg) {
   echo "Received a message OK";
} else {
   echo "The receive failed";
}

?>
]]>
    </programlisting>
   </example>
  </para>
  <para>
   <example>
    <title>Deleting a durable subscription to a topic</title>
    <programlisting role="php">
<![CDATA[
<?php

if (!$conn->unsubscribe($subName)) {
   echo "Unsubscribe failed";
}

?>
]]>
    </programlisting>
   </example>
  </para>
 </section>  <!-- xml:id=sam.pubsub -->

 <section xml:id='sam.errors'>
  <title>Error handling</title>
  <para>
   All SAMConnection methods that provide access to messaging operations
   return &false; if an error occurred in processing the request.
   In addition the SAMConnection object has two properties, "errno"
   and "error", that provide respectively the error number and
   text description of the last error to occur on the connection.
  </para>
  <para>
   <example>
    <title>Handling an error from a method that returns no result</title>
    <programlisting role="php">
<![CDATA[
<?php
if (!$conn->commit()) {
    // The commit failed!
    echo "Commit failed ($conn->errno) $conn->error";
}
?>
]]>
    </programlisting>
   </example>
  </para>
  <para>
   <example>
    <title>Handling an error from a method that returns a result</title>
    <programlisting role="php">
<![CDATA[
<?php
$correlid = $conn->send('queue://send/test', $msg);

if (!$correlid) {
  // The Send failed!
  echo "Send failed ($conn->errno) $conn->error";
} else {
  /* ... */
}
?>
]]>
    </programlisting>
   </example>
  </para>
 </section>  <!-- xml:id=sam.errors -->

</chapter>

<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->