![]() | CXXXVII. SAM - Simple Asynchronous MessagingIntroduzioneThis extension provides access to the functionality of messaging and queueing systems, such as the IBM WebSphere MQSeries family of products, from PHP scripts. The interface is designed to make it extremely simple to do the more commonly required tasks such as deliver simple text messages to queues while still allowing skilled users to do more complex messaging operations. For many users the complexities of setting up numerous options can be simply ignored. InstallazionePrerequisitesThe SAM extension interfaces to the IBM Messaging and Queuing middleware products using a set of libraries and some client side code referred to as XMS. This package is available as a free download in the guise of IBM support pack IA94. There is a description of this package and download links in the article Introducing XMS - The IBM Message Service API. If you intend to use SAM to access the Messaging and Queuing infrastructure within WebSphere MQ then you will also need to have installed a local MQ queue manager or installed the WebSphere MQ clients package. The clients package is freely available as a support pack (MQC6). If you are only aiming to experiment with sending messages to and from WebSphere Application Server queues using the WebSphere Platform Messaging protocol (WPM) then you do not need to install the MQC6 package. After installing these packages you will need to ensure the XMS binary and, if you are using it, the MQ client bin directory are included in the PATH environment variable so that Apache and PHP can find the dependent .DLLs/libraries. Linux installation stepsThe sam extension is supplied as a PECL module, which you should be able to download and install in one step as follows:
Make sure that the module is loaded by PHP, by adding following line to php.ini :
to your php.ini file. If you cannot use the PEAR installer, you can download the extension and build it manually:
To work with the very latest source, you'll need to extract it from cvs and build manually as above. Windows installation stepsCurrently you will need to build the sam extension for Windows as there are no pre-built binaries. The extension can be built using the standard Windows extension build procedures. You will need the PHP source tree for the version of PHP you wish to build the SAM extension against which you can obtain from php.net. This should be unpacked into a working directory of your choice. You will also need the libraries and headers used by PHP extensions available from http://www.php.net/extra/win32build.zip and this should be unzipped so that is in your working directory. You should have something like:
You will need a compiler such as the free version of Visual Studio C++ Express from the Microsoft web site. Also you need the Microsoft Windows Platform SDK which again can be downloaded from the Microsoft web site. Obtain the SAM extension source using pear (pear download sam) or by using CVS and copy the files to a new "sam" directory under the "ext" directory in your PHP source tree. To build the extension open a build environment window by going to the start menu->all programs->microsoft platform SDK for windows-> open build environment window->windows 200 build environment-> set windows 2000 build environment (retail) This should open a command prompt with all the environment variables set up to access the platform SDK etc. You then need to set the environment variables for Visual Studio by issuing the command "vcvars32.bat" in the window. Change directory to your working directory e.g. cd c:\php-build. Then make sure the win32build tools are accessible by adding them to the PATH environment variable:
Run the buildconf.bat command. This should rebuild the configure.js file. Run the cscript command:
The additional parameter passed for sam is the installation path to the XMS libraries and runtime that were installed as described under prerequisites at the top of this file. You can specify whatever other cscript parameters you require to include or exclude items from the php build or select options. Assuming all has gone well so far you can now finally run a make!
Additional steps for Visual Studio 2005If you build the SAM extension with the Microsoft Visual Studio 2005 compiler and tools you need to perform an additional step in the build process to ensure the php_sam.dll is able to link with the C runtime libraries at runtime. This step includes the dependancy manifest into the DLL. Switch to the directory where the php_sam.dll has been generated (usually Release_TS or Debug_TS below the php source directory) and issue the following magic incantation:
If you build the SAM extension using the compiler and libaries from Microsoft Visual Studio 2005 you will also need to ensure that the runtime components are installed on the system on which you intend to use SAM. This can be accomplished by installing Visual Studio 2005 or by using the freely distributable runtime package. API UsageConnectionsIn 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. 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.
MessagesMessages 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.
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:
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:
Properties may also be extracted from the header of a message.
Messaging operationsAll 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.
Publish/Subscribe and suscriptions to topicsSAM 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. 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. 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.
Error handlingAll 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.
Classi predefiniteSAMConnectionObject representing a connection to a Messaging Server Costruttori
Metodi
SAMMessageObject representing a message to be sent or received Costruttori
Costanti predefiniteQueste costanti sono definite da questa estensione e sono disponibili solo se l'estensione è stata compilata nel PHP o se è stata caricata dinamicamente a runtime.
| ![]() | |||||||||||||||||||||